C
C#6mo ago
tama

✅ Required property

Hello there. I am creating a class with some required properties. I have decorated them with the [Required] thing, however, it's still complaining. Are you supposed to have both [Required] and the required keyword? Example:
C#
public class A
{
[Required]
public string Name { get; set; }
}
C#
public class A
{
[Required]
public string Name { get; set; }
}
Should it be this instead?:
C#
public class A
{
[Required]
public required string Name { get; set; }
}
C#
public class A
{
[Required]
public required string Name { get; set; }
}
2 Replies
Angius
Angius6mo ago
[Required] is an attribute that, say, EF Core can use to create a column that is not nullable required keyword is enforced by the compiler. And also provides that information to EF Actually, with nullable reference types being enabled, the type being a string and not string? already gives EF that info So required is mostly just for compiler enforcement
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server