?: ?? Operators Instead Of IF|ELSE

前端 未结 9 1639
粉色の甜心
粉色の甜心 2020-11-28 02:09
public string Source
{
    get
    {
        /*
        if ( Source == null ){
            return string . Empty;
        } else {
            return Source;
                


        
9条回答
  •  醉梦人生
    2020-11-28 02:55

    Refering to ?: Operator (C# Reference)

    The conditional operator (?:) returns one of two values depending on the value of a Boolean expression. Following is the syntax for the conditional operator.

    Refering to ?? Operator (C# Reference)

    The ?? operator is called the null-coalescing operator and is used to define a default value for a nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand.

    That means:

    [Part 1]

    return source ?? String.Empty;
    

    [Part 2] is not applicable ...

提交回复
热议问题