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

前端 未结 30 2566
挽巷
挽巷 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:43

    While difference is very, VERY little, the difference still exist.

    1) "" creates object while String.Empty does not. But this object will be created once and will be referenced from the string pool later if you have another "" in the code.

    2) String and string are the same, but I would recommend to use String.Empty (as well as String.Format, String.Copy etc.) since dot notation indicates class, not operator, and having class starting with capital letter conforms to C# coding standards.

提交回复
热议问题