C# 11 Features - Required Members

A new keyword required is introduced in C# 11 where it will be decorated to the instance property and field declaration within a class/record/struct.

Whenever a class is declared with a property or field with the required keyword, the caller is forced to initialize in the object initializer scope.

C#11 Features - Required Members

In the above example, when the caller tries to create the Employee object, the compiler throws an error to initialize the required properties. The following line is a valid object initialization in this case. 

Employee employee = new() { FirstName = "Sai", LastName = "Kumar" };

Why Required Members? 

The required member(s) can resolve some of the well-known limitations with constructors. 

  • The caller should maintain the order or position of the parameters on parameter constructors.
  • The caller should pass default values even if he/she doesn’t need those parameters within their implementation scope.
  • Need to have multiple parameter constructors with different combinations of parameters.
  • If there is any new parameter introduced, it will affect all the caller's implementations.

With required members, no need to have multiple constructors declared as well, and no limitations to the caller on the position of the parameters while object initialization. While using these members, the compiler will enforce the caller to initialize these members in the object initialization scope.  

Limitations of Required members 

Following are the limitations on using the required keyword

  • A required keyword cannot be used for static property or field as this is designed at object initialization scope.
  • A required keyword cannot be applied for private members as these are not visible to the caller.
  • A required keyword cannot be applied for read-only members as we can assign only within the constructor.

Required with init-only properties 

As we are aware init-only properties where allow these properties to be initialized at the time of object creation in the constructor and cannot be assigned outside the constructor scope. 

C#11 Features - Required Members

When we have a property with required and init-only, we need to initialize this property in the constructor or object initialization as below. 

Employee employee = new() { FirstName = "Sai", LastName = "Kumar" };

This will ensure that the property value never is updated from the caller's initialized value. 

Required Members and Constructor

Let's understand the below example,

C#11 Features - Required Members

In the above example, both the below statements got full-filled.

  • Init-only members need to initialize at the time of object initialization.
  • Required members need to initialize at the time of object initialization. 

But still, we can see the error thrown by the compiler at the caller's end. The reason is, the compiler can doesn't attempt to detect does the constructor actually assigned it or not. So, the problem here is, even if we initialized the required members still caller need to assign some value while the object initialization scope. 

To overcome such issues, since .NET 7 we got introduced to the following attribute.

System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttribute

When we decorate any constructor with [SetRequiredMembers], the compiler will assume that the required members already got assigned with default values in the constructor and no need to force the callers to initialize while object initialization scope. 

C#11 Features - Required Members

We can see there are no errors now once we decorated the constructor with the [SetsRequiredMembers] attribute. But, if we comment on the default assigning lines in a constructor, a compiler will show a warning message as below. 

C#11 Features - Required Members

If we comment on the attribute, this warning will be removed, and can see the error at the caller's end.

The required keyword is very much useful if we want to force the caller to send some information to the object while initialization.

Happy Coding :)


Similar Articles