Shortest way to check for null and assign another value if not

前端 未结 11 2299
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 01:32

I am pulling varchar values out of a DB and want to set the string I am assigning them to as \"\" if they are null. I\'m currently doi

11条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-13 02:00

    You are looking for the C# coalesce operator: ??. This operator takes a left and right argument. If the left hand side of the operator is null or a nullable with no value it will return the right argument. Otherwise it will return the left.

    var x = somePossiblyNullValue ?? valueIfNull;
    

提交回复
热议问题