Alex Rivera | Logout

.Trim() when string is empty or null

Asked 2011-12-18T20:46:00.180
37

I'm receiving some data from the client in the form of json. I'm writing this:

string TheText; // or whould it be better string TheText = ""; ?
TheText = ((serializer.ConvertToType<string>(dictionary["TheText"])).Trim());

If the variable that's being parsed from json comes back empty, does this code crash when I call the .Trim() method?

Thanks.

c#
Edit
Report

1 Answer

18

Calling Trim() on an empty string will result in an empty string. Calling Trim() on null will throw NullReferenceException

answered 2011-12-18T20:48:30.733

Your Answer