命名参数?

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

问题描述

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

参数。对于

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


-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

推荐答案




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

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

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

标准:


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.




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

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.






不,如果没有可选的

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

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


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

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



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


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

05-28 19:35
查看更多