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

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

    I wasn't going to chime in, but I'm seeing some wrong info getting tossed out here.

    I, personally, prefer string.Empty. That's a personal preference, and I bend to the will of whatever team I work with on a case-by-case basis.

    As some others have mentioned, there is no difference at all between string.Empty and String.Empty.

    Additionally, and this is a little known fact, using "" is perfectly acceptable. Every instance of "" will, in other environments, create an object. However, .NET interns its strings, so future instances will pull the same immutable string from the intern pool, and any performance hit will be negligible. Source: Brad Abrams.

提交回复
热议问题