Wednesday, December 21, 2022

[FIXED] What does "null!" default value of property mean?

Issue

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.



Answered By - misticos
Answer Checked By - Timothy Miller (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.