In C#, Should I Use String.Empty or String.Empty or "" to Intitialize a String

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

Use whatever you and your team find the most readable.

Other answers have suggested that a new string is created every time you use "". This is not true - due to string interning, it will be created either once per assembly or once per AppDomain (or possibly once for the whole process - not sure on that front). This difference is negligible - massively, massively insignificant.

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 "" easier to read.

The argument that "" and " " are easily mistaken for each other doesn't really wash with me. Unless you're using a proportional font (and I haven't worked with any developers who do) it's pretty easy to tell the difference.

String.Empty versus

It's not different.

http://msdn.microsoft.com/en-us/library/system.string.empty.aspx:

The value of this field is the zero-length string, "".

In application code, this field is most commonly used in assignments to initialize a string variable to an empty string. To test whether the value of a string is either null or String.Empty, use the IsNullOrEmpty method.

What is the difference between String.Empty and (empty string)?

In .NET prior to version 2.0, "" creates an object while string.Empty creates no objectref, which makes string.Empty more efficient.

In version 2.0 and later of .NET, all occurrences of "" refer to the same string literal, which means "" is equivalent to .Empty, but still not as fast as .Length == 0.

.Length == 0 is the fastest option, but .Empty makes for slightly cleaner code.

See the .NET specification for more information.

string.Empty vs null.Which one do you use?

null and Empty are very different, and I don't suggest arbitrarily switching between them. But neither has any extra "cost", since Empty is a single fixed reference (you can use it any number of times).

There is no "pollution" on the stack caused by a ldsfld - that concern is.... crazy. Loading a null is arguably marginally cheaper, but could cause null-reference exceptions if you aren't careful about checking the value.

Personally, I use neither... If I want an empty string I use "" - simple and obvious. Interning means this also has no per-usage overhead.


At the IL level, the difference here between "" and Empty is just ldstr vs ldsfld - but both give the same single interned string reference. Furthermore, in more recent .NET versions the JIT has direct interception of these, yielding the empty string reference without actually doing a static field lookup. Basically, there is exactly no reason to care either way, except readability. I just use "".

What is the difference between string.Empty and null in memory

There's no difference. The compiler interns string literals, so you're not creating a new string with the call, just referencing an existing string.

The heap and the stack are implementation details in C#. There is some behaviour that depends on the runtime, but the only real contract is that the runtime provides as much memory as you ask for, and guarantees the memory is still there if you access it in the future.

If you do care about the implementation details of the current desktop .NET runtimes, reference types are never passed on the stack. String is a reference type, so it is always passed by reference, and never by value. However, arguments aren't even required to be on the stack in the first place - the reference can also be passed in a register.

In general, in a managed language like C#, you should only care about what exactly happens in memory if you have a good reason it affects the characteristics of your program. The default case should always be thinking about the semantics. Should an empty string mean "no value"? Should a null string mean "no value"? That depends on the semantics of your program. Until you have a good reason to believe the decision is e.g. performance critical, just go with the most clear option, least prone to mistakes, and easiest to read and modify.

Is there a reason to initialize a string in C#?

Assuming the question means "Why should you initialize strings" and not "Which method is best for initializing strings"...

Yes, it is a good idea to initialize strings to minimize the potential for null reference exceptions in your code. It is easy to create a code path where the string is never set and when you call a method on that string it will error.

Default string initialization: NULL or Empty?

+1 for distinguishing between "empty" and NULL. I agree that "empty" should mean "valid, but blank" and "NULL" should mean "invalid."

So I'd answer your question like this:

empty when I want a valid default value that may or may not be changed, for example, a user's middle name.

NULL when it is an error if the ensuing code does not set the value explicitly.

What is the difference between String.Empty and “” and null?

public sealed class String {
//...
public static readonly String Empty = "";
//...
}

Use null when you want to represent that there is no value;

Use String.Empty when you want to represent that there is a value, but the value is a blank string.



Related Topics



Leave a reply



Submit