To check if a value is null and set a value if it is null in C#, you can use the null-coalescing operator (??
).
Here's an example:
main.cs128 chars7 lines
In the above example, the value
variable is assigned null
. We then use the null-coalescing operator (??
) to check if value
is null. If it is null, defaultValue
will be assigned to result
. Since value
is null, result
will be assigned the value of defaultValue
, which is "Default Value"
.
If value
is not null, result
will be assigned the value of value
itself.
The output of the above code will be:
main.cs14 chars2 lines
This way, you can easily check if a value is null and set a default value if it is null in C#.
gistlibby LogSnag