In C#, Is null, "", and string.Empty the exact same thing described 3 different ways?

Tom Knowlton
Tom Knowlton used Ask the Experts™
Is null, "" and string.Empty the exact same thing?  Please explain your answer.

CommonHelpers.LastWorkingProjectID == null || CommonHelpers.LastWorkingProjectID == "" || CommonHelpers.LastWorkingProjectID == string.Empty

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

A string is an object, and so it is passed around by pointers to the memory where it actually exists.  

Null means the pointer points to nothing, that the string doesn't reference any memory.  I.e. string b = null; var a = b.Length; throws an exception.  Since b is null, there are no methods that work off of it.

prior to .Net 2.0, there was a difference between .Empty and "", but now they operate the same.
Most Valuable Expert 2018
Distinguished Expert 2018
Null means Nothing. It doesn't exist. There is no such thing. String.Empty and "" means that it does exist. There is an Object in memory. It has a value, albeit an empty, zero-length value.

With an empty string, your variable will exist as an Object in memory, so you can call any of the normal string members on it: myVar.Length, myVar.ToUpper()

When it's set to Null, there is no object, so trying to call any members will give you a Null Reference Exception.

Your statement can be simplified to:

Tom KnowltonWeb developer


Thank you... this was very helpful.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial