c#语法??在控制语句中

本文关键字:控制语句 语法 | 更新日期: 2023-09-27 18:11:51

你好,我这里有一些代码我不明白

  public ObservableCollection<Packet> Items
    {
        get
        {
            this.items = this.items ?? this.LoadItems();
            return this.items;
        }
    }

??意味着什么?

c#语法??在控制语句中

??为空合并算子。只要左边的值不为空,就返回它。如果为空,则返回右边的值。

a = b ?? c;

等价于:

if (b != null)
    a = b;
else
    a = c;

这是空合并运算符。

它说;将items赋值给items,除非items为空,在这种情况下,调用LoadItems并赋值结果。它是

的简写
if( this.items == null )
    this.items = this.LoadItems();
return this.items;

如果??操作符不为空,则返回左操作数,否则返回右操作数。

参考。

它被称为"空合并运算符",文档在http://msdn.microsoft.com/en-us/library/ms173224.aspx.