![](/img/trans.png)
[英]How to convert a nullable datetime value to string.empty when datetime is null?
[英]Why System.Convert(object value) returns String.Empty not NULL when the value is NULL
我遇到了与System.Convert(Object value)
方法有关的问题,我的研究使我了解了此文档 。
我想知道C#团队做出此决定the reasons
是什么,因为这对我来说没有任何意义。
从我的角度来看,最好返回“ Null”,请注意以下示例:
static void Main(string[] args)
{
int? ID = null;
string result = Convert.ToString(ID);
Console.WriteLine("{0} ,{1}",result ,result.Length);
Console.ReadKey();
}
//result would be "" and the result.Length=0
如您所见, ID
为null
但convert(ID)
不为null
,这对我来说听起来很奇怪!
这是convert(object value)
源代码,我从问题的答案中得到了它。
public static string ToString(Object value) {return ToString(value,null);}
public static string ToString(Object value, IFormatProvider provider) {
IConvertible ic = value as IConvertible;
if (ic != null)
return ic.ToString(provider);
IFormattable formattable = value as IFormattable;
if (formattable != null)
return formattable.ToString(null, provider);
return value == null? String.Empty: value.ToString();
请注意last line
。 我只是想知道什么是rational
背后。
先感谢您。
在很多情况下, ID.Tostring()
根本没有用
例如 :
public System.Web.Mvc.ActionResult MyAction(int? Id)
{
//This line of code dosent show "Id has no value" at all
MyContetnt = (System.Convert.ToString(Id) ?? "Id has no value");
return Content(MyContetnt);
}
在此代码示例中,如果通过以下路径运行此Mvc示例:
http:// localhost:端口号/ ControllerName / MyAction
我的意思是without using ID in the path
您without using ID in the path
在屏幕上看到Nothing ,因为此处ID
为null
,而Convert.Tostring(ID)
是System.Empty
或“”。
我的意思是在这种情况下,无法使用null-coalescing-operator
来重构此代码,通过使用ID.tostring()
会引发nullrefrenceException
,这没有用,不是我的意图。 我知道这很容易说:
MyContent = Id.HasValue ? Id.Value.ToString() : "Id has no value"
// this line works fine;
但是,假设convert.ToString(Null)
的结果为Null
那么以null-coalescing-operator
为特征的第一个代码就可以正常工作。
所以我很好奇,这个想法的结果背后的原因是什么
Convert.tostring (object null)
是否为string.empty
不为null
?
所有的Convert
方法都以这种方式工作。 这就是为什么要首先使用它们的唯一原因。
例如,考虑Convert.ToInt32
。 如果传递null
,则将得到0
。 如果您不希望这样做,可以使用int.Parse
代替-这将抛出null
值。
同样, Convert.ToString
是从任何值(包括null
中获取字符串的安全方法。 如果要抛出null
,则可以使用object.ToString()
。 您可能会争辩说default(string)
是null
,而不是string.Empty
,但是从这个意义上说null
不是一个安全值-您不能进一步使用null
,除非通过显式检查null。 另一方面, string.Empty
仍然允许您连接或询问长度, IndexOf
或...。
关键在于, Convert
类可以防止使用null
值,而应返回默认值。 这不是技术原因,它可以简化编程(某些方法)。
return value == null? String.Empty: value.ToString();
在您的情况下,value == null,因此结果将为string.Empty。 与方法ID.ToString()相同
public override string ToString()
{
if (!this.HasValue)
return ""; //return empty string when value is null
else
return this.value.ToString();
}
但是,如果您想在id为null时返回null,请为此编写扩展名。
public static class Extension {
public static string ToStringWithChecking(this int? param) {
if (!param.HasValue) {
return null;// or throw exception here
}
return param.ToString();
}
}
您问题中的陈述不正确。 Convert.ToString(object value)
不会返回null
当值为null
。 这个
int? ID = null;
Console.WriteLine(Convert.ToString(ID) == string.Empty);
Console.WriteLine(Convert.ToString(null) == null);
object box = ID;
Console.WriteLine(Convert.ToString(box) == string.Empty);
写true true true。
在您的示例中,您没有传递null
,而是传递了对装箱的Nullable
的引用null
。
从您链接到的文档中(我必须阅读两次):
value的字符串表示形式;如果value是一个值为null的对象,则为String.Empty。 如果value为null,则该方法返回null。
注意:该问题未指定Framework版本,因此我假设为4.5。 显然,行为从4.0更改为4.5,因此问题的答案是:他们意识到这是个坏主意,因此将其更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.