簡體   English   中英

為什么當值是NULL時,System.Convert(對象值)返回String.Empty而不是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

如您所見, IDnullconvert(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 pathwithout using ID in the path在屏幕上看到Nothing ,因為此處IDnull ,而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。

http://ideone.com/jRsefy

在您的示例中,您沒有傳遞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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM