【问题标题】:Exclamation mark as part of an indexer? [duplicate]感叹号作为索引器的一部分? [复制]
【发布时间】:2020-06-14 04:34:26
【问题描述】:

我在 .net core 3.1 中反编译字典后看到奇怪的语法,但我找不到任何有关它的信息

! 在这种情况下是什么意思? if (i >= 0) return _entries![i].value;

这里是更多上下文的索引器的完整代码

public TValue this[TKey key]
{
    get
    {
        int i = FindEntry(key);
        if (i >= 0) return _entries![i].value;
        ThrowHelper.ThrowKeyNotFoundException(key);
        return default;
    }
    set
    {
        bool modified = TryInsert(key, value, InsertionBehavior.OverwriteExisting);
        Debug.Assert(modified);
    }
}

我将不胜感激任何有关此的信息/链接

【问题讨论】:

标签: c# .net-core


【解决方案1】:

这是null-forgiving operator !,它有助于明确告诉编译器__entries 不能是可为空的引用。

它是在 C# 8.0 中引入 nullable references 的范围内添加的。启用可为空的上下文时,所有引用类型都被识别为不可为空的。当引用可能是 null 时,编译器会生成警告。 ! 运算符允许您告诉编译器特定引用在给定上下文中不能是 null

【讨论】:

    猜你喜欢
    • 2012-03-06
    • 2017-01-21
    • 2019-07-28
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 2015-06-01
    • 2017-04-20
    相关资源
    最近更新 更多