Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Trim() when string is empty or null

Tags:

c#

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.

like image 341
frenchie Avatar asked Dec 18 '11 20:12

frenchie


1 Answers

You can use elvis operator:

GetNullableString()?.Trim(); // returns NULL or trimmed string 
like image 185
TcKs Avatar answered Oct 14 '22 13:10

TcKs