-1

I just saw odd syntax after decompiling a dictionary in .net core 3.1 and I cant find any information about it

What does the ! mean in this context ? if (i >= 0) return _entries![i].value;

Here is the full code for the indexer for more context

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);
    }
}

I would appreciate any information / link about this

Riaan Walters
  • 2,587
  • 2
  • 18
  • 30
  • 2
    Does this answer your question? [What does exclamation mark mean before invoking a method in C# 8.0?](https://stackoverflow.com/questions/59230542/what-does-exclamation-mark-mean-before-invoking-a-method-in-c-sharp-8-0) – AlleXyS Mar 01 '20 at 20:21
  • I did do a search but did not find that question, thanks! (Will close my question shortly to avoid duplicates) – Riaan Walters Mar 01 '20 at 20:28

1 Answers1

2

This is null-forgiving operator ! which helps to explicitly tell the compiler that __entries can't be a nullable reference.

It was added in scope of introducing a nullable references in C# 8.0. When nullable context is enabled, all reference types are recognized as non-nullable ones. Compiler generates a warning when reference can be a null possibly. ! operator allows you to tell the compiler that particular reference can't be a null at the given context

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66