required vs [Required] in C# 11
Asked Answered
B

3

28

C# 11 has introduced, required modifier, which can be used a below:

    public required string FirstName { get; init; }
    public required string LastName { get; init; }
[Required]
public required string FirstName { get; init; }

Just wanted to understand the difference between [Required] attribute and required modifier. Read the documentation but did not understand.

Tried reading documentation

Baptistery answered 30/3, 2023 at 17:21 Comment(2)
[Required] attribute is a data validation attribute which can be triggered during runtime, required modifier is used to denote required property during initialization, it is explained quite well in the article - newdevsguide.com/2022/11/12/csharp-11-required-keywordKarylkarylin
I would like to know whether ASP.NET Core can look at required keyword on models and consider that for runtime validation.Ibex
C
22

The required keyword indicates that the field must be set during initialization. For example

public class User
{   
    public required string Username { get; set; }
    public required string Password{ get; set; }
}

If you try to set a new user User newuser=new User(); would create an error since you have two required fields but you haven't set them in initialization of the variable.

For the [Required] attribute

public class User
{   
    
    public string Username { get; set; }
    [Required]
    public string Password{ get; set; }
}

I would be able to create a new instance of User User newUser= new User();

But when a new user on a form is validated, the field must contain a value for Password.

Coign answered 11/9, 2023 at 17:9 Comment(1)
Agreed. [Required] is for data validation and in many cases work with ef, and required keyword is c# general.Butlery
K
2

The additional difference is the data binding using System.Text.Json (I haven't experimented with Newtonsoft.Json). The [Required] attribute is processed after the JSON has been parsed to a model, but the required keyword is processed during JSON parsing causing a JsonException if the field is missing. The parsing exception also results in a different validation error, rather than a data annotation validation error, because in such a case the entire model is not bound, and it looks like the request is missing body, which I think is extremely misleading at first glance.

Kondon answered 13/5, 2024 at 11:13 Comment(0)
C
0

Another apect regarding the model binding is that [Required] attribute on non-nullable struct (DateTime, int etc) or enum properties will not fire a validation error even if a value is not provided.

For example:

class MyObject
{
    [Required]
    public DateTime myDate { get; set; }
}

If a value is provided the model binding will assign it to myDate but if not, it will assign the default value for the type. Either cases, a vallidation error will not be raised. A way to overcome this bahaviour is to make the property type nullable.

However, the required keyword will raise an error even if the property stays non-nullable. Well, a little bit misleading error that the type cannot be created but still.

class MyObject
    {
        public required DateTime myDate { get; set; }
}
Candelariacandelario answered 24/7, 2024 at 6:16 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.