What is the difference between String.Empty and “” (empty string)?

后端 未结 17 1971
礼貌的吻别
礼貌的吻别 2020-11-22 03:25

In .NET, what is the difference between String.Empty and \"\", and are they interchangable, or is there some underlying reference or Localization i

17条回答
  •  忘掉有多难
    2020-11-22 04:12

    Everybody here gave some good theoretical clarification. I had a similar doubt. So I tried a basic coding on it. And I found a difference. Here's the difference.

    string str=null;
    Console.WriteLine(str.Length);  // Exception(NullRefernceException) for pointing to null reference. 
    
    
    string str = string.Empty;
    Console.WriteLine(str.Length);  // 0
    

    So it seems "Null" means absolutely void & "String.Empty" means It contains some kind of value, but it is empty.

提交回复
热议问题