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

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

    It is totally a code-style preference, do to how .NET handles strings. However, here are my opinions :)

    I always use the BCL Type names when accessing static methods, properties and fields: String.Empty or Int32.TryParse(...) or Double.Epsilon

    I always use the C# keywords when declaring new instances: int i = 0; or string foo = "bar";

    I rarely use undeclared string literals as I like to be able to scan the code to combine them into reusable named constants. The compiler replaces constants with the literals anyway so this is more of a way to avoid magic strings/numbers and to give a little more meaning to them with a name. Plus changing the values is easier.

提交回复
热议问题