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.
If the serializer returns an empty string,
Trimwill do nothing.If the serializer returns
null, you will get aNullReferenceExceptionon the call toTrim.Your code would be better written (as far as initialization is concerned) like this:
There is no point in declaring and initializing the variable and the immediately assigning to it.
The following would be safest, if you don’t know what the serializer might return: