What is the question mark operator mean in C#?

This question already has an answer here:

  • Null Conditional Operators 2 answers

  • This is the null conditional operator:

    Used to test for null before performing a member access (?.) or index (?[) operation.

    You method's code without the use of null conditional operator it could be written as below:

    public void DoSomething(Result result)
    {
        if(result!=null)
        {
            if(result.Actions!=null)
            {
                return result.Actions.Utterance;
            }
            else
            {
                return null;
            }
        }
        else
        {
            return null;
        }
    
    }
    

    该运算符是空条件if语句的简写形式:

    public void DoSomething(Result result)
    {
        if(result != null){
            if(result.Actions != null){
                return result.Actions.Utterance;
            }
        }
        return null;
    }
    
    链接地址: http://www.djcxy.com/p/9124.html

    上一篇: 继承自List <T>

    下一篇: C#中问号运算符的含义是什么?