Как обрабатывать нулевое значениеC#

Место общения программистов C#
Ответить
Anonymous
 Как обрабатывать нулевое значение

Сообщение Anonymous »


I have the following if statement:

if ((product.EmailType.Contains("cafe", StringComparison.OrdinalIgnoreCase) || product.EmailType.Contains("vendor", StringComparison.OrdinalIgnoreCase)) && product.Supplier.Supplier_Type.Contains("Support")) { // Do some stuff } Basically if EmailType contains a specific value or a different specific value, and the SupplierType is "Support", do something.

However, it is very common for the Supplier_Type to be null. With how it is above it throws an exception.

How do I account for it possibly being null? If it's null it should evaluate the whole if statement as false and move on.

I tried null coalescing on the final line but I get an error

Operator ?? cannot be applied to type bool and bool

if ((product.EmailType.Contains("cafe", StringComparison.OrdinalIgnoreCase) || product.EmailType.Contains("vendor", StringComparison.OrdinalIgnoreCase)) && (product.Supplier.Supplier_Type.Contains("Support") ?? false)) { // Do some stuff } I also tried null handling by writing the final line as the following, but get the same error:
&& product.Supplier.Supplier_Type?.Contains("Support")

Источник: https://stackoverflow.com/questions/781 ... null-value
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»