In C#, should I use string.Empty or String.Empty o

2020-01-22 11:30发布

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;

or

string willi = String.Empty;

or

string willi = "";

or what?

30条回答
做自己的国王
2楼-- · 2020-01-22 11:53

This topic is pretty old and long, so excuse me if this behavior has been mentioned somewhere else. (And point me to the answer that covers this)

I have found a difference in the behavior of the compiler if you use string.Empty or double quotes. The difference shows itself if you don't use the string variable initialized with string.Empty or with double quotes.

In case of initialization with string.Empty then the Compiler Warning

CS0219 - The variable 'x' is assigned but its value is never used

is never emitted while in case of initialization with double quotes you get the expected message.

This behavior is explained in the Connect article at this link: https://connect.microsoft.com/VisualStudio/feedback/details/799810/c-warning-cs0219-not-reported-when-assign-non-constant-value

Basically, if I get it right, they want to allow a programmer to set a variable with the return value of a function for debugging purposes without bothering him with a warning message and thus they limited the warning only in case of costant assignments and string.Empty is not a constant but a field.

查看更多
走好不送
3楼-- · 2020-01-22 11:56

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

查看更多
聊天终结者
4楼-- · 2020-01-22 11:56

On http://blogs.msdn.com/b/brada/archive/2003/04/22/49997.aspx :

As David implies, there difference between String.Empty and "" are pretty small, but there is a difference. "" actually creates an object, it will likely be pulled out of the string intern pool, but still... while String.Empty creates no object... so if you are really looking for ultimately in memory efficiency, I suggest String.Empty. However, you should keep in mind the difference is so trival you will like never see it in your code...
As for System.String.Empty or string.Empty or String.Empty... my care level is low ;-)

查看更多
时光不老,我们不散
5楼-- · 2020-01-22 11:57

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.

查看更多
啃猪蹄的小仙女
6楼-- · 2020-01-22 11:57

I would favor string.Empty over String.Empty because you can use it without needing to include a using System; in your file.

As for the picking "" over string.Empty, it is personal preference and should be decided by your team.

查看更多
迷人小祖宗
7楼-- · 2020-01-22 11:58

I'd prefer string to String. choosing string.Empty over "" is a matter of choosing one and sticking with it. Advantage of using string.Empty is it is very obvious what you mean, and you don't accidentally copy over non-printable characters like "\x003" in your "".

查看更多
登录 后发表回答