In C#, should I use string.Empty or String.Empty or “” to intitialize a string?

前端 未结 30 2343
挽巷
挽巷 2020-11-22 02:06

In C#, I want to initialize a string value with an empty string.

How should I do this? What is the right way, and why?

string willi = string.Empty;
         


        
相关标签:
30条回答
  • 2020-11-22 02:35

    String.Empty and string.Empty are equivalent. String is the BCL class name; string is its C# alias (or shortcut, if you will). Same as with Int32 and int. See the docs for more examples.

    As far as "" is concerned, I'm not really sure.

    Personally, I always use string.Empty.

    0 讨论(0)
  • 2020-11-22 02:35

    Just about every developer out there will know what "" means. I personally encountered String.Empty the first time and had to spend some time searching google to figure out if they really are the exact same thing.

    0 讨论(0)
  • 2020-11-22 02:35

    Any of the above.

    There are many, many better things to pontificate. Such as what colour bark suits a tree best, I think vague brown with tinges of dulcet moss.

    0 讨论(0)
  • 2020-11-22 02:35

    I think the second is "proper," but to be honest I don't think it will matter. The compiler should be smart enough to compile any of those to the exact same bytecode. I use "" myself.

    0 讨论(0)
  • 2020-11-22 02:38

    string is synonym for System.String type, They are identical.

    Values are also identical: string.Empty == String.Empty == ""

    I would not use character constant "" in code, rather string.Empty or String.Empty - easier to see what programmer meant.

    Between string and String I like lower case string more just because I used to work with Delphi for lot of years and Delphi style is lowercase string.

    So, if I was your boss, you would be writing string.Empty

    0 讨论(0)
  • 2020-11-22 02:38

    I use the third, but of the other two the first seems less odd. string is an alias for String, but seeing them across an assignment feels off.

    0 讨论(0)
提交回复
热议问题