Shorthand for create new instance if null?

后端 未结 5 1670
庸人自扰
庸人自扰 2021-01-12 13:49

In Javascript I can do this:

var myVar = returnNull() || new MyObject();

In C# I am currenly doing this:

var myVar = returnObjec         


        
5条回答
  •  南笙
    南笙 (楼主)
    2021-01-12 14:36

    Use the null-coalescing operator:

    var myvar = returnObjectOrNull() ?? new MyObject();
    

提交回复
热议问题