PARAMS的IEnumerable< T> C# [英] Params IEnumerable<T> c#

查看:181
本文介绍了PARAMS的IEnumerable< T> C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不能我使用一个IEnumerable使用参数?这会不会永远是固定的?我真的希望他们能改写旧库使用泛型...


解决方案

为什么不能我使用一个IEnumerable使用参数?




这个问题的前提是,设计团队必须提供理由的的添加功能的语言。这个前提是错误的。



相反,为了使功能被你使用它需要被认为,设计说明,实施,测试,文档和发运。所有这些有很大的成本。



PARAMS枚举功能一直被认为和设计。它从来没有被指定,实施,测试,记录或运输。



因此​​,您不能使用该功能。



< HR>

更新:截至发稿 - 2015年初 - 现在已被指定,但实施,测试,文档和航运在2014年下半年被切割的C#6.0。见卢西恩的公告在这里: http://roslyn.codeplex.com/discussions/568820



由于它仍然没有得到执行,测试,记录和发货,但仍然没有这样的功能。 。希望这将使它成为C#的一个假想的未来版本






更​​新:我要澄清我的意思以功能,因为它很可能是我们都有不同的想法在我们的头脑是什么功能是。我谈论的特点就是让你这样说。



 无效FROB(PARAMS IEnumerable的< INT> X)
{
的foreach(INT的Y X)...
}

然后调用点可以是在通过整数序列,或FROB(10,20,30日)的扩展形式的范式。如果扩展形式,编译器生成调用,就像你说FROB(新INT [] {10,20,30}),同样因为它的参数数组。该特征的一点是,它常常是,该方法从不使用对阵列的随机存取,因此,我们可能会削弱该PARAMS是阵列要求的情况下。该PARAMS可能仅仅是一个序列而不是



您可以通过过载做到今天这个:

 无效FROB(PARAMS INT []×){FROB((IEnumerable的< INT>)x)的; } 

无效FROB(IEnumerable的< INT> X)
{
的foreach(在X INT Y)...
}

这是一个有点疼痛。我们可以简单地允许您使用的IEnumerable作为把params参数的类型和与它做。




这会不会永远是固定的?




我希望如此。这个功能已经在列表很长一段时间的。它将使很多功能使用LINQ工作更加好听。

  FROB(从C在客户挑选c.Age); 



而无需编写两个不同版本FROB的。



然而,这仅仅是一种小方便的功能;它实际上并没有一大堆新动力添加到语言。这就是为什么它从来就没有足够高的优先级列表上,使其以规范书写的阶段。




我真的希望他们将改写旧库使用泛型。




评论说。


Why cant I use an IEnumerable with params? Will this ever be fixed? I really wish they would rewrite the old libraries to use generics...

解决方案

Why cant I use an IEnumerable with params?

The question presupposes that the design team must provide a reason to not add a feature to the language. This presupposition is false.

Rather, in order for a feature to be used by you it needs to be thought of, designed, specified, implemented, tested, documented and shipped. All of these have large costs.

The "params enumerable" feature has been thought of and designed. It has never been specified, implemented, tested, documented or shipped.

Therefore, you cannot use the feature.


UPDATE: As of this writing -- early 2015 -- has now been specified, but implementation, testing, documentation and shipping were cut for C# 6.0 in the latter part of 2014. See Lucian's announcement here: http://roslyn.codeplex.com/discussions/568820.

Since it has still not been implemented, tested, documented and shipped, there is still no such feature. Hopefully this will make it into a hypothetical future version of C#.


UPDATE: I should clarify what I mean by "the feature" since it is possible we all have different ideas in our heads what "the feature" is. The feature I'm talking about is to allow you to say something like

void Frob(params IEnumerable<int> x)
{
    foreach(int y in x) ...
}

and then the call site can either be in the "normal form" of passing a sequence of integers, or the "expanded form" of Frob(10, 20, 30). If in the expanded form, the compiler generates the call as though you'd said Frob(new int[] { 10, 20, 30}), the same as it does for param arrays. The point of the feature is that it is often the case that the method never uses random access to the array, and therefore, we could weaken the requirement that the params be an array. The params could just be a sequence instead.

You can do this today by making an overload:

void Frob(params int[] x) { Frob((IEnumerable<int>)x); }

void Frob(IEnumerable<int> x)
{
    foreach(int y in x) ...
}

which is a bit of a pain. We could simply allow you to use IEnumerable as the type of the params argument and be done with it.

Will this ever be fixed?

I hope so. This feature has been on the list for a long time. It would make a lot of functions work much more nicely with LINQ.

Frob(from c in customers select c.Age);

without having to write two different versions of Frob.

However, it is a mere "small convenience" feature; it doesn't actually add a whole lot of new power to the language. That's why its never made it high enough on the priority list to make it to the "specification is written" stage.

I really wish they would rewrite the old libraries to use generics.

Comment noted.

这篇关于PARAMS的IEnumerable&LT; T&GT; C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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