Possible minus if the operator (? :) in C #?

It seems that I remembered something about the exception of the Immediate If ( ?:) Operator in C #, but I don’t know where I am reading this and what it was. I think this is due to the fact that both the true and the false parts are fulfilled before a decision is made on the initial condition. It's right? Or is it so in VB.Net?

+3
source share
4 answers

It is actually called a conditional statement and is referred to as "?:" On MSDN. This is basically a shortened notation for if-else, except that it is actually an expression, not a statement. Since it is equivalent to if, there are no caveats for this operator.

, , , Iif VB.NET. , ,

Dim s As String = Iif(person Is Nothing, String.Empty, person.FirstName)

NullReferenceException.

+22

,

Console.WriteLine(MyBool ? "It true!" : "Nope");

, .

+4

, ?: "" if-else - ; , if-else , ?:, , , , ?: , if-else, if-else .

+2

Futher to Anton , ( LINQ-to- [some db] ..).

The downside is that if you are testing a few things, this can be confusing. See the discussion here for an example covering type testing with / without predicates.

+1
source

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


All Articles