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?
|
Use whatever you and your team find the most readable. Other answers have suggested that a new string is created every time you use Which you find more readable is a different matter, however. It's subjective and will vary from person to person - so I suggest you find out what most people on your team like, and all go with that for consistency. Personally I find The argument that |
|||||||||||||||||||||
|
Basically, There really is no difference from a performance and code generated standpoint. In performance testing, they went back and forth between which one was faster vs the other, and only by milliseconds.
In looking at the behind the scenes code, you really don't see any difference either. The only difference is in the IL, which string.Empty use the opcode " C# Code
IL Code
Assembly code
|
|||||||||||||||||||||
|
The best code is no code at all and consequently, less code is better code. |
|||||||||||||||||||||
|
I'd prefer |
|||||||||||||||||||||
|
One difference is that if you use a Look at this link for more info: string-empty-versus-empty-quotes |
|||||||||||||
|
I wasn't going to chime in, but I'm seeing some wrong info getting tossed out here. I, personally, prefer As some others have mentioned, there is no difference at all between 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. |
|||||
|
I personally prefer "" unless there is a good reason to something more complex. |
|||
|
String.Empty and string.Empty are equivalent. String is the BCL class name. string is the C#...shortcut if you will. Same as with Int32 and int. As far as "", nor really sure. Personally, I always use string.Empty. |
|||
|
Just about every developer out there will know what "" means. I personally encountered String.Empty the first time and had to spend some time searching google to figure out if they really are the exact same thing. |
|||||||||
|
I doesn't make a difference. The last one is the quickest to type though :) |
|||
|
I strongly prefer String.Empty, aside from the other reasons to ensure you know what it is and that you have not accidentally removed the contents, but primarily for internationalization. If I see a string in quotes then I always have to wonder whether that is new code and it should be put into a string table. So every time code gets changed/reviewed you need to look for "something in quotes" and yes you can filter out the empty strings but I tell people it is good practice to never put strings in quotes unless you know it won't get localized. |
|||
|
No one mentioned that in VisualStudio String is color coded differently then string. Which is important for readability. Also, lower case is usually used for vars and type, not a big deal but String.Empty is a constant and not a var or type. |
|||
|
Any of the above. There are many, many better things to pontificate. Such as what colour bark suits a tree best, I think vague brown with tinges of dulcet moss. |
|||
|
Values are also identical: I would not use character constant "" in code, rather Between So, if I was your boss, you would be writing |
|||
|
It doesn't matter - they are exactly the same thing. However, the main thing is that you must be consistent p.s. I struggle with this sort of "whats the right thing" all the time. |
|||||||||
|
I use the third, but of the other two the first seems less odd. string is an alias for String, but seeing them across an assignment feels off. |
|||
|
The compiler should make them all the same in the long run. Pick a standard so that your code will be easy to read, and stick with it. |
|||
|
Either of the first two would be acceptable to me. I would avoid the last one because it is relatively easy to introduce a bug by putting a space between the quotes. This particular bug would be difficult to find by observation. Assuming no typos, all are semantically equivalent. [EDIT] Also, you might want to always use either |
|||||
|
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: I always use the C# keywords when declaring new instances: 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. |
|||
|
I think the second is "proper," but to be honest I don't think it will matter. The compiler should be smart enough to compile any of those to the exact same bytecode. I use "" myself. |
|||
|
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. |
|||||
|
On http://blogs.msdn.com/b/brada/archive/2003/04/22/49997.aspx :
|
|||||||||
|
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. |
|||
|