C# null check chain in method call

前端 未结 4 630
心在旅途
心在旅途 2021-01-20 15:03

I suppose method call chain below.

void DoSomething()
{
    ObjectA a = CreateA();
    if (a != null)
    {
        a.Foo();
    }
}

ObjectA CreateA()
{
            


        
4条回答
  •  耶瑟儿~
    2021-01-20 15:35

    If you are using C# 6.0 or higher, you got an easy solution with Null conditional operators for this issue.

    see this link

    https://msdn.microsoft.com/en-au/library/dn986595.aspx?f=255&MSPPError=-2147217396&cs-save-lang=1&cs-lang=csharp#code-snippet-1

提交回复
热议问题