简体   繁体   English

命名参数与可选参数

[英]Named Parameters vs Optional Parameters

The optional parameters are nothing new in C# and I've known about that since the release of C# 5.0 but there is something I just came across. 可选参数在C#中并不是什么新鲜事物,自C#5.0发布以来,我就已经知道了这一点,但是我刚刚遇到了一些问题。 When I use Data Annotations for my MVC models such as the Required attribute I see this: 当我为MVC模型使用数据注释(例如Required属性)时,会看到以下内容:

在此处输入图片说明

So I can do: 所以我可以做:

[Required(ErrorMessage="Something"]

However when I create my own methods with optional parameters like: 但是,当我使用可选参数创建自己的方法时,例如:

void Test(String x = null, String y = null) {}

I can pass arguments in both these ways: 我可以通过以下两种方式传递参数:

Test(x = "Test") OR Test(x: "Test")

this while in the Required attribute I always have to use the = and if I use the : it will cause error. Required属性中,我总是必须使用= ;如果使用: ,它将导致错误。 for example: 例如:

 Required(ErrorMessage:"Something") --> Compile time error

So what I see is that those Named Parameters are created differently than what I already knew about. 因此,我看到的是这些命名参数的创建方式与我已经知道的方式不同。 And my question is How to make them for a method (How to create such Named Parameters like in the Required attribute). 我的问题是如何使它们成为一种方法(如何在Required属性中创建此类命名参数)。

An attribute has its own syntax. 属性具有自己的语法。 It uses the name=value form for named parameters. 它对命名参数使用name=value形式。

For a normal method you can't use that form, you are stuck with the name:value form. 对于通常的方法,您不能使用该形式,但是会被name:value形式所困扰。

It would not be possible to use the name=value form for normal methods. 普通方法不可能使用name=value形式。 The compiler would not be able to tell if you were trying to use a named parameter or if you were trying to assing a value to a variable and use the assignment as a parameter value. 编译器将无法告诉您是要使用命名参数还是将值赋给变量并将赋值用作参数值。

Despite this syntax looking like a method call: 尽管此语法看起来像方法调用:

[Required(ErrorMessage="Something")]

An Attribute is a class , not a method. 属性是 ,而不是方法。 You aren't specifying an argument in the line above, you are initializing a property. 您没有在上面的行中指定参数,而是在初始化属性。 See the example on the Attribute base class documentation to see what I mean. 请参阅Attribute基类文档上的示例,以了解我的意思。

The Attribute-specifying syntax is therefore similar to C#'s class initialization syntax: 因此,指定属性的语法类似于C#的类初始化语法:

new RequiredAttribute { ErrorMessage = "Something" };

There is currently no equivalent syntax in C# for specifying a named argument to a method. 当前在C#中没有用于为方法指定命名参数的等效语法。

If you do something like: 如果您执行以下操作:

string y; 
Test(y = "Test")

You can call a function with that syntax. 您可以使用该语法调用函数。 But be careful... the y = "Test" is actually setting the variable y , and then passing that to the function! 但是要小心... y = "Test"实际上是在设置变量y ,然后将其传递给函数! There is a side-effect there which is probably undesirable. 有一个副作用,这可能是不希望的。 Also "Test" is getting passed into parameter x of the Test function, not y because it's going in as the first parameter. 同样, "Test"也被传递给Test函数的参数x ,而不是y因为它作为第一个参数进入。

In short, you should always avoid using this syntax when calling a function, because it doesn't do what you're expecting. 简而言之,在调用函数时,应始终避免使用此语法,因为它不能满足您的期望。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM