简体   繁体   English

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

[英]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.

相关问题 需要C#和MySql解决方案之间的通信 - Communication between c# and MySql solution required 使用默认的Required选项嵌套事务与不进行全部操作之间有什么区别? - What is the difference between nesting a Transaction with the default Required option, and not doing it all? 在c#中的模型中,强制执行必填字段的最佳方法是什么? - What is the best way to enforce required fields are not null in a model in c#? 客户端C#应用程序需要哪些软件先决条件? - What software prerequisite are required for a C# application at client side? 将C#迁移到64位需要哪些代码更改? - What code changes are required to migrate C# to 64-bits? 在计算机上运行ac#winform程序需要什么 - What is required to run a c# winform program on a computer 有时,必填字段验证程序在C#中失败的情况是什么? - What are the conditions when sometimes Required field validators fails in C#? 将 C# 连接到 Oracle 数据库所需的最小客户端占用空间是多少? - What is the minimum client footprint required to connect C# to an Oracle database? 在 Linux 上运行/运送 C# 应用程序需要什么? - What is required to run / ship a C# application on Linux? 在计算机上运行ac#控制台应用程序需要什么 - What is required to run a c# console application on a computer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM