Please Login to Continue

How to check if string is null before replacing charecters in C-Sharp (C#)

Question: How do you catch for nulls in a variable before calling String.Replace()

Answer: If you have a variable that might be null at some point, just add StringVariable?.Replace(" ",","); What this line of code does is, It first looks into a String Variable called StringVariable and checks if it is null, if it is not null then the code goes ahead to replace the "Space" with a comma character. You might have seen a situation where an integer placed as a parameter has a question mark in front of it,  this handles the same scenario where the Integer might be null (not passed in with a value).