Short combination If statement without another

I am trying to have a short hand for the if statement, since I am creating an expression query, and if the test is null, then the accessor throws an error.

test != null ? test.Contains("mystring") : NO_VLAUE 

I'm looking for:

 test != null ? test.Contains("mystring") otherwise ignore. 

I know what I can use ?? for is null , but is there a converse.

Thanks in advance.

+6
source share
2 answers

It looks like you want test != null && test.Contains("mystring")

Your assignment question does not make sense. All expressions, including the conditional operator, must have a value. What would you expect this expression to evaluate if test is null?

You probably want this to be false if the test is null.
In other words, you want this to be true if the test is not null and contains mystring .

+8
source

It looks like you want:

 test != null && test.Contains("mystring") 

This will evaluate to false , if test is null - is that what you want? Basically you need to say what you want if test is NULL, because otherwise it cannot be used as an expression.

+7
source

Source: https://habr.com/ru/post/901620/


All Articles