Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does [Required] do?

Tags:

c#

I found nothing on the web what [Required] actually does. The msdn-article is not explorative at all.

static class Program
{
    public static Main()
    {
        var vustomer = new CustomerClass();
    }
}

public class CustomerClass
{
    public string m_FirstName;
    [Required]
    public string m_LastName;
}

As far as i understand, that should throw an exception since m_LastName is required, but not set. But i don't get one. I don't get what it's good for and what this actually does.

like image 551
user2762996 Avatar asked Dec 16 '22 02:12

user2762996


1 Answers

RequiredAttribute, like all other attributes, does nothing by itself other than annotate something (in this case, a field of a type). It is entirely up to the application that consumes the type to detect the presence of the attribute and respond accordingly.

Your sample program does not do this, so the attribute does not have any visible effect. Certain frameworks such as ASP.NET MVC and WPF do check for and respond to the presence of the attribute.

like image 114
Jon Avatar answered Dec 22 '22 00:12

Jon