I\'m sure I\'ve missed something here. With a certain project I need to check if a string is empty or null.
Is there an easier way of writing this?
i
To avoid null checks you can use ?? operator.
var result = value ?? "";
I often use it as guards to avoid sending in data that I don't want in methods.
JoinStrings(value1 ?? "", value2 ?? "")
It can also be used to avoid unwanted formatting.
string ToString()
{
return "[" + (value1 ?? 0.0) + ", " + (value2 ?? 0.0) + "]";
}
This can also be used in if statements, it's not so nice but can be handy sometimes.
if (value ?? "" != "") // Not the best example.
{
}