csharp String.Length

//The following results in a "object not set to an instance of an object error" and could occur with null data from the database:
string test = null;//NOTE: String.Empty doesn't result in an error
Console.WriteLine(test.Length);

//The solution? Check if string is null/empty to begin with:
string test = null;
if (test != null && test != string.Empty)
	Console.WriteLine(test.Length);
else
	Console.WriteLine(0);
Example of String.Length usage and exceptions

Updated: Monday 8th August 2011, 07:34pm

There are 0 comments

Leave a comment of your own

Comments are currently closed.