Context class - default!;

Dean Everhart 926 Reputation points

I'm using an older version of .net to model creating property sets in the context class of a core 6 project. I notice in the core 6 project they include "= default!;" at the end.

having a hard time finding an explanation of the "= default!;" online. One explanation is a default value for a property, but this is a property set in a context class.

what is the = default!; in core 6 context class?

        public DbSet<Media.Models.Reference> Reference { get; set; } = default!;
A set of technologies in the .NET Framework for building web applications and XML web services.
2,850 questions
A Microsoft web application framework that implements the model-view-controller (MVC) design pattern.
744 questions
No comments
{count} votes

Accepted answer
  1. AgaveJoe 20,226 Reputation points

    There are a couple things going on.

    Since C# 6 properties can be assigned an initial value.

    The default literal sets default value of a type. For a C# class that means null.

    If you try to set the property to default without the "!" then you'll see a warning. "CS8625 - Cannot convert null literal to non-nullable reference type."

    Adding the "!" (C# ! (null-forgiving) operator) overrides the warning.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful