IEnumerable <T> C # parameter

Why can't I use IEnumerable with parameters? Will it ever be fixed? I'm sorry that they did not rewrite the old libraries on the use of generics ...

+47
generics c # ienumerable params
Jan 24 '10 at 20:49
source share
3 answers

Why can't I use IEnumerable with parameters?

The question suggests that the development team should provide a reason not to add a function to the language. This assumption is incorrect.

Rather, in order for the function to be used by you, it needs to be taken into account, constructed, specified, implemented, tested, documented and sent. All of them have high costs.

The "params enumerable" function has been reviewed and designed. It has never been specified, implemented, tested, documented, or submitted.

Therefore, you cannot use this function.




UPDATE: At the time of this writing, the beginning of 2015 is now indicated, but implementation, testing, documentation, and delivery have been reduced for C # 6.0 in the second half of 2014. See Lucian's announcement here: http://roslyn.codeplex.com/discussions/568820 .

Since it has not yet been implemented, tested, documented, and submitted, there is no such function yet. Hopefully this will turn it into a hypothetical future version of C #.




UPDATE: I have to clarify what I mean by “function”, because maybe we all have different ideas about what a “feature” is. The function I'm talking about is letting you 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 transmitting a sequence of integers, or the "extended form" of Frob (10, 20, 30). If in extended form the compiler generates a call, as if you had told Frob (new int [] {10, 20, 30}), the same as for param-arrays. The essence of the function is that it often happens that a method never uses random access to an array, and therefore we could relax the requirement that params be an array. Parameters can be just a sequence.

You can do this today by doing an overload:

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

which is a little painful. We could just let you use IEnumerable as the type of the params argument and do with it.

Will this be fixed?

I hope so. This feature has been on the list for a long time. This would make many functions work much better with LINQ.

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

without having to write two different versions of Frob.

However, this is simply "little convenience"; in fact, this does not add much to the language. Therefore, he never made it high enough on the priority list to go to the “specification is written” stage.

I am very sorry that they did not rewrite the old libraries for the use of generics.

Comment marked.

+83
Jan 24 '10 at 21:06
source share

And, I think, now I understand what you mean. I think you want to declare a method as follows:

 public void Foo<T>(params IEnumerable<T> items) { } 

And then you can call it using the "normal" argument as follows:

 IEnumerable<string> existingEnumerable = ...; Foo(existingEnumerable); 

or with several parameters, for example:

 Foo("first", "second", "third"); 

Is that what you need? (Noting that you want the first form to use T=string , not T=IEnumerable<string> with one element ...)

If so, I agree that this might be useful - but easy enough:

 public void Foo<T>(params T[] items) { Foo((IEnumerable<T>) items); } public void Foo<T>(IEnumerable<T> items) { } 

I do not find myself doing this often enough to make the above a particularly ugly workaround.

Note that when invoking the above code, you need to explicitly specify a type argument to avoid the compiler preferring the params example. For example:

 List<string> x = new List<string>(); Foo<string>(x); 
+11
Jan 24 '10 at 20:55
source share

Params parameters are sent as an array, and IEnumerable<T> does not provide the random access required to operate as an array.

You need to create an array from IEnumerable when you call the method:

 TheMethod(theIEnumerable.ToArray()); 
+5
Jan 24
source share



All Articles