Search code examples
c#syntaxpropertiesoperators

What does "null!" default value of property mean?


I have seen this below code in somewhere. There is a TodoItems property in the TodoContext class and it has a default value of "null!". I know this is a default value set for intended property but what does the exclamation mark(!) do at the end of the "null" value?

public class TodoContext : DbContext
{
    public TodoContext(DbContextOptions<TodoContext> options)
        : base(options)
    {
    }

    public DbSet<TodoItem> TodoItems { get; set; } = null!;
}

Solution

  • null! basically means null but the ! symbol suppresses the warning since your intentions are that it will be changed in a constructor or other places.