[英]What is the difference between [Required] and [Required()] in c#
I read some documents from github and found there was a difference while creating the object我从 github 阅读了一些文档,发现在创建 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
感谢帮助
What is different between [Required] and [Required()] in c#
c# 中的 [Required] 和 [Required()] 有什么不同
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.
对于不传递任何构造函数 arguments 的属性,括号是可选的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.