string.Empty and null
-
String.Empty is a defined value for a string instance of zero length, it is not null. string a = null is an uninitialized object of type string, but since it is uninitialized, it is not valid to reference (cant't call any methods on it, or reference it without an exception being raised). string a = null; string b = string.Empty; a.Compare(b); //raises null ref exception b.Compare(a); //raises null reference exception string c = "x"; c.Compare(a); // null reference c.Compare(b); //valid statement Absolute faith corrupts as absolutely as absolute power Eric Hoffer All that is necessary for the triumph of evil is that good men do nothing. Edmund Burke
-
In my eye. string a = string.Empty; is the same as string a = "";
-
In my eye. string a = string.Empty; is the same as string a = "";
They are equal, but neither one is null. Both of your code samples represent an instantiated object of type string holding a value of zero length. You can call methods and get/set properties on instantiated objects. A string cal also be null, in which case it doesn't refer to any instantiated object. Therefore, you can't call any methods or modify any properties on it. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
In my eye. string a = string.Empty; is the same as string a = "";
Yes and no. String.Empty is a defined constant contained in the string class. "" is defining a new string value of zero length in your own class, which is equivalent to srting.empty. The compiler may well just use string.Empty here, or it may intern a new string value in your assembly. It is probably better practice to use the existing string constant (string.Empty) when initializing strings, since it is certain what happens. Absolute faith corrupts as absolutely as absolute power Eric Hoffer All that is necessary for the triumph of evil is that good men do nothing. Edmund Burke
-
In my eye. string a = string.Empty; is the same as string a = "";
-
Hot discussion! I have another topic: what's the difference between "==" and method "Equal". are they same? It is quite confusing! In jave language, "two object Equal" means the refs pointing to the same memory address. But in C#, I find it is different. You can use if( stringA == "aa" ) instead of if( stringA.Equal("aa" ); Any ideas? Radic Feng Beijing, China.
-
Hot discussion! I have another topic: what's the difference between "==" and method "Equal". are they same? It is quite confusing! In jave language, "two object Equal" means the refs pointing to the same memory address. But in C#, I find it is different. You can use if( stringA == "aa" ) instead of if( stringA.Equal("aa" ); Any ideas? Radic Feng Beijing, China.
* Operator equality in C# compares refs pointing while Equals compares values equality. BUT: * For strings Operator equality in C# performs value type equality meaning it actually compares the values in the addresses. (Partially it's due the fact that strings are immutable in C# and usually we want actually to compare the values and not the addresses of the instances). In C# Operator equality makes internal call to string.Equals (thus results in slightly poorer performance).
-
* Operator equality in C# compares refs pointing while Equals compares values equality. BUT: * For strings Operator equality in C# performs value type equality meaning it actually compares the values in the addresses. (Partially it's due the fact that strings are immutable in C# and usually we want actually to compare the values and not the addresses of the instances). In C# Operator equality makes internal call to string.Equals (thus results in slightly poorer performance).
Many thanks!
-
I think a better question would be.. wad's the difference between: string a=String.Empty; string a=""; /\ |_ E X E GG
The answer: nothing. String.Empty is defined as "" in System.String's static constructor, and it's a static readonly field. By the way, .NET 2.0 has a string.IsNullOrEmpty function, which is sorta cool.