命名参数? [英] Named parameters?

查看:53
本文介绍了命名参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读关于属性类的内容,特别是命名与位置

参数。对于

的灵活性,还有其他原因,这是不是很有用,而是还有另外一个原因我错过了吗?


-Ben

I''m reading about attribute classes and specifically, named versus positional
parameters. Was this implimented instead of multiple constructors for
flexibility or is there another reason I''m missing?

-Ben

推荐答案



" Ben R." <成为** @ discussions.microsoft.com>在消息中写道

news:01 ********************************** @ microsof t.com ...

"Ben R." <Be**@discussions.microsoft.com> wrote in message
news:01**********************************@microsof t.com...
我正在阅读关于属性类的信息,特别是命名与
位置
参数。这是不是为了灵活性而不是多个构造函数,或者是否有其他原因我缺失了?
I''m reading about attribute classes and specifically, named versus
positional
parameters. Was this implimented instead of multiple constructors for
flexibility or is there another reason I''m missing?




我认为它比其他任何东西更灵活。考虑到一个属性具有的

选项集,如果不是不可能的话,多个构造函数会很复杂地说

。命名参数真的没有什么不同

标准:


X x = new X();

x.Property = 10;


除了必须以更精简的形式编写。


考虑你有两个属性类型为string,Name和描述


如果你只想设置其中一个,假设属性在构造函数中不需要

,当然,你可以。但是如果你使用多个

构造函数,那么就无法定义一组两个构造函数,它们将允许你设置一个且只有一个上面的属性。 />

现在,考虑一下你是否有20个属性,能够在构造函数中生成所有可能的属性组合的机会是

非常低,很可能你会遇到同样的问题。没有

提到构造者的数量可怕。



It is flexibility more than anything else, I think. Considering the sets of
options an attribute has, multiple constructors would be complicated to say
the least, if not impossible. Named parameters are really no different than
the standard:

X x = new X();
x.Property = 10;

except that they have to be written in a more condensed form.

Consider you have two properties of type string, Name and Description

If you only want to set one of those, assuming the attribute doesn''t require
both in the constructor, of course, you can. But if you use multiple
constructors there is no way to define a set of two constructors that will
allow you to set one and only one of the above properties.

Now, consider if you had 20 properties, the chances of being able to
generate all the possible combonations of properties in the constructor are
very very low, its likely you will run into the same problem somewhere. Not
to mention the hideous number of constructors.


嗨丹尼尔,


谢谢你非常好的反应。这是一个非常有趣的例子

,不能为名称和

描述字段设置2个不同的构造函数。我做了一些实验,它看起来像这样

功能仅对属性类启用。这准确吗?如果是这样,为什么呢?

另外,这里的关键是该字段需要有一个属性才能在命名参数中设置

或者可以做同样的事情公共领域?


谢谢...


-Ben


丹尼尔O''Connell [C#MVP]"写道:
Hi Daniel,

Thanks for the very thourough response. That was a very interesting example
with not being able to have 2 different constructors for the name and
description fields only. I did some experimenting and it looks like this
feature is only enabled for Attribute classes. Is this accurate? If so, why?
Also, is the key here that the field needs to have a property in order to be
settable in a named parameter or could one do the same with a public field?

Thanks...

-Ben

"Daniel O''Connell [C# MVP]" wrote:

Ben R. <成为** @ discussions.microsoft.com>在消息中写道
新闻:01 ********************************** @ microsof t.com。 ..

"Ben R." <Be**@discussions.microsoft.com> wrote in message
news:01**********************************@microsof t.com...
我正在阅读关于属性类的信息,特别是命名与
位置
参数。这是不是为了灵活性而不是多个构造函数,还是因为我缺少了另一个原因?
I''m reading about attribute classes and specifically, named versus
positional
parameters. Was this implimented instead of multiple constructors for
flexibility or is there another reason I''m missing?



我认为它比其他任何东西都更灵活。考虑到属性具有多组
选项,如果不是不可能的话,多个构造函数就会很复杂。命名参数与标准没有什么不同:

X x = new X();
x.Property = 10;

除此之外它们必须以更简洁的形式编写。

考虑你有两个类型字符串,名称和描述的属性

如果你只想设置其中一个,假设该属性在构造函数中既不需要
,当然也可以。但是如果你使用多个
构造函数,则无法定义一组两个构造函数,这些构造函数将允许您设置上述属性中的一个属性。

现在,考虑一下,如果你有20个属性,那么能够在构造函数中生成所有可能的属性组合的几率非常低,很可能你会在某个地方遇到同样的问题。没有提到构造者的数量可观。



It is flexibility more than anything else, I think. Considering the sets of
options an attribute has, multiple constructors would be complicated to say
the least, if not impossible. Named parameters are really no different than
the standard:

X x = new X();
x.Property = 10;

except that they have to be written in a more condensed form.

Consider you have two properties of type string, Name and Description

If you only want to set one of those, assuming the attribute doesn''t require
both in the constructor, of course, you can. But if you use multiple
constructors there is no way to define a set of two constructors that will
allow you to set one and only one of the above properties.

Now, consider if you had 20 properties, the chances of being able to
generate all the possible combonations of properties in the constructor are
very very low, its likely you will run into the same problem somewhere. Not
to mention the hideous number of constructors.





" cody" <德******** @ gmx.de>在消息中写道

news:ut ************** @ TK2MSFTNGP14.phx.gbl ...

"cody" <de********@gmx.de> wrote in message
news:ut**************@TK2MSFTNGP14.phx.gbl...
已有建议微软将命名的
参数作为一般语言特征包含在内,而不仅仅是作为属性允许的例外。
诀窍是找到最灵活,最高效的方法来实现它。
There are already suggestions made to microsoft to include named
parameters
as a general language feature and not only as an exception allowed for
Attributes.
The trick is to find the most flexible and performant way to implement it.




不,如果没有可选的

参数,诀窍是说服人们这是一个坏主意,这本身就是一个坏主意,恕我直言。属性没有

命名参数,他们只是拥有快速的属性设置者。


灵活性和性能不是问题,真正的麻烦是

一致性,这是你无法实现的。这种语言实际上并不是真的可以用来支持它



No, the trick is convincing people its a bad idea without optional
parameters, which are a bad idea themselves, IMHO. Attributes do not have
named arguments, they simply have convienent property setters.

Flexiblity and performance are not the issue, the real trouble is
consistency, which you will not achieve. The language just isn''t really
going to allow it


这篇关于命名参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆