What is the difference between [Required] and [Required()] in c#

Viewed 72

I read some documents from github and found there was a difference while creating the object

public class Item
{
    public int Id { get; set; }

    [Required]
    public string Description { get; set; }
}

and

public class Item
{
    public int Id { get; set; }

    [Required()]
    public string Description { get; set; }
}

What is the difference between them? Thanks for help

2 Answers

What is different between [Required] and [Required()] in c#

Nothing.

Attributes are classes which can be configured by a constructor. If there is a default constructor, you can either use the attribute with parenthesis or not, it's up to you.

[Required]
[Required()]

If it has a constructor you could use:

[SomeOtherAttribute("bob")]

There are no functional differences in your examples. The parentheses are optional for attributes where you are not passing any constructor arguments.

Related