简体   繁体   English

使用参数名称和冒号调用C#方法

[英]C# method call with parameter name and colon

I've begun to notice at times when I'm making method calls in C# that the names of the parameters for the method I'm calling will show up in the intellisense list appended with a colon, and that I can then format the method call thusly: 我开始注意到,当我在C#中进行方法调用时,我调用的方法的参数名称将显示在附加冒号的intellisense列表中,然后我可以格式化该方法这样打电话:

MethodCall(parameter1:value1, parameter2:value2);

Is this a new language feature? 这是一种新的语言功能吗? It reminds me of the way you can call stored procedures in SQL and specify parameter names like so: 它让我想起了在SQL中调用存储过程的方式,并指定参数名称,如下所示:

spDoSomeStuff @param1 = 1, @param2 = 'other param'

Is this a similar feature? 这是一个类似的功能吗? If so, to what end? 如果是这样,到底是什么? If not, what is it and what is it to be used for. 如果没有,它是什么以及它用于什么。

It's a new feature. 这是一个新功能。 See here: http://msdn.microsoft.com/en-us/library/dd264739.aspx Named parameters are standard in ObjectiveC for instance. 请参阅此处: http//msdn.microsoft.com/en-us/library/dd264739.aspx例如,命名参数是ObjectiveC中的标准参数。 It takes some time to get used to them but they are a good thing. 它需要一些时间来适应它们,但它们是一件好事。 Only from looking you can tell what a parameter is meant for. 只有从你看,你可以告诉参数是什么意思。

Named parameters allow you explicitly set the value of arguments in a custom order independent of the signature. 命名参数允许您以独立于签名的自定义顺序显式设置参数的值。 Method signatures are defined by the argument types, ie, Foo( int i, bool b ), which will only accept arguments of type int and bool in that order. 方法签名由参数类型定义,即Foo(int i,bool b),它只接受int和bool类型的参数。 Named arguments allow you to pass b first and i second. 命名参数允许您先传递b和第二个传递。

It is worth mentioning, unlike optional parameters, you can skip certain arguments and pass only the parameters you are interested in. 值得一提的是,与可选参数不同,您可以跳过某些参数并仅传递您感兴趣的参数。

public void Example(int required, string StrVal = "default", int IntVal = 0)
{
    // ...
}

public void Test()
{
    // This gives compiler error
    // Example(1, 10);

    // This works
    Example(1, IntVal:10);
}

Scott Gu has introduced this new feature in his blog: Scott Gu在他的博客中介绍了这个新功能:

Optional Parameters and Named Arguments in C# 4 C#4中的可选参数和命名参数

这是C#4附带的命名和可选参数

@Krumelur said that "Named parameters are standard in ObjectiveC for instance." @Krumelur说“例如,命名参数是ObjectiveC中的标准参数。”

That's not actually correct. 这实际上并不正确。 Objective-C uses an infix notation, so that this message call: Objective-C使用中缀表示法,以便此消息调用:

[foo setRed:255 Green:255 Blue:0];

is the setRed:Green:Blue: message (including those colons!) with the (255,255,0) arguments interspersed within the message name. setRed:Green:Blue:消息(包括那些冒号!),其中(255,255,0)个参数散布在消息名称中。

Although, granted, at first blush Objective-C's syntax gives the appearance that Objective-C uses named parameters. 尽管如此,乍一看,Objective-C的语法给出了Objective-C使用命名参数的外观。 But that is not actually correct, and misunderstanding the difference can be an impediment for learning Objective-C. 但这实际上并不正确,误解差异可能会成为学习Objective-C的障碍。

(I would have answered in a comment, but I somehow lost all my reputation points and I'm starting over. Drat. C'est la vie.) (我会在评论中回答,但我不知何故失去了所有的声望点,我重新开始.Drat.C'est la vie。)

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

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