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

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

    I was just looking at some code and this question popped into my mind which I had read some time before. This is certainly a question of readability.

    Consider the following C# code...

    (customer == null) ? "" : customer.Name
    

    vs

    (customer == null) ? string.empty : customer.Name
    

    I personally find the latter less ambiguous and easier to read.

    As pointed out by others the actual differences are negligible.

提交回复
热议问题