c#中的[Required]和[Required()]有什么区别

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

我阅读了 github 的一些文档,发现创建对象时存在差异

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

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

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

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

它们有什么区别?感谢帮助

您的示例在功能上没有差异。对于不传递任何构造函数参数的属性,括号是可选的。

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

没有。

属性是类,可以通过构造函数配置。如果有默认构造函数,您可以使用带括号的属性,也可以不使用,由您决定。

[Required]
[Required()]

如果它有构造函数,您可以使用:

[SomeOtherAttribute("bob")]