.NET Forum / Languages / C# / February 2008
Generic method & constraint
|
|
Thread rating:  |
timor.super@gmail.com - 11 Feb 2008 11:15 GMT Hi group,
please look at my generic method :
static void Write<T>(IEnumerable<IEnumerable<T>> list) { foreach (IEnumerable<T> ts in list) { foreach (T t in ts) { Console.WriteLine(t); } Console.WriteLine(); } }
static void Main() { List<List<int>> list = new List<List<int>>(new List<int>[] {new List<int>(new int[] {1, 2, 3}), new List<int>(new int[] {3, 4, 5}), new List<int>(new int[] {4, 5, 6})}); List<List<string>> list2 = new List<List<string>>(new List<string>[] { new List<string>(new string[] { "ab", "bc" }), new List<string>(new string[] { "aa" }), new List<string>(new string[] { "bb" }) }); Write(list.ToArray()); Write(list2.ToArray()); }
My purpose is to have an IEnumerable of IEnumerable (for example array of array) and write the content
But I'm not able to apply constraint. (should I)
Is it the good way to make such a method ? Is there a better way ?
Thanks for your help,
Best regard
Jon Skeet [C# MVP] - 11 Feb 2008 11:24 GMT On Feb 11, 11:15 am, timor.su...@gmail.com wrote:
> Hi group, > > please look at my generic method : > > static void Write<T>(IEnumerable<IEnumerable<T>> list) <snip>
> My purpose is to have an IEnumerable of IEnumerable (for example array > of array) and write the content > > But I'm not able to apply constraint. (should I) What constraint do you want to apply? Why do you want to apply it?
Jon
timor.super@gmail.com - 11 Feb 2008 12:34 GMT > On Feb 11, 11:15 am, timor.su...@gmail.com wrote: > [quoted text clipped - 14 lines] > > Jon Hi jon & marc,
I want a constraint that makes the use only of IEnumerable<IEnumerable> to my function.
In fact, I would like to do something harder than a simple for each, but what I want is to be able to iterate through a collection of collection, that are IEnumerable
That can be an string[] of string[] or a list<> of list<>, ...
Jon Skeet [C# MVP] - 11 Feb 2008 12:42 GMT On Feb 11, 12:34 pm, timor.su...@gmail.com wrote: <snip>
> I want a constraint that makes the use only of > IEnumerable<IEnumerable> to my function. Sorry, I still don't understand. You've already got a sort of constraint just because the method takes IEnumerable<IEnumerable<T>>.
What do you need to allow that isn't allowed at the moment, or what do you need to prohibit which isn't prohibited at the moment?
Jon
timor.super@gmail.com - 11 Feb 2008 12:56 GMT > On Feb 11, 12:34 pm, timor.su...@gmail.com wrote: > <snip> [quoted text clipped - 9 lines] > > Jon after more thoughts,
I think I would like to do something like this :
static void Write<T, T1>(T1 list) where T1 : IEnumerable<IEnumerable<T>> { foreach (IEnumerable<T> enumerable in list) { foreach (T t in enumerable) { Console.WriteLine(t); } Console.WriteLine(); } }
but ... how to call it with a List<List<int>> ?
Marc Gravell - 11 Feb 2008 13:09 GMT But *why* do you want to do that? What you have works!???
> how to call it with a List<List<int>> ? As per the code you posted originally? Or the slightly simpler C# 3 equiavalent.
Marc
Marc Gravell - 11 Feb 2008 13:36 GMT > What you have works!??? Oops; I've just tried in 2005 (I use 2008 normally) - and the type-inference differences (not to mention the LINQ "ToArray") are making a difference. Apologies for confusion if you are using 2005 (and the message that appears in 2005 is not necessarily obvious).
So; either specify the type-arguments manually, or use C# 3 which does a better job here...
With the signature: static void Write<TList,T>(IEnumerable<TList> list) where TList : IEnumerable<T> {...}
you can use (in VS2005):
Write<List<int>,int>(list); Write<List<string>,string>(list2);
timor.super@gmail.com - 11 Feb 2008 13:46 GMT > > What you have works!??? > [quoted text clipped - 14 lines] > Write<List<int>,int>(list); > Write<List<string>,string>(list2); Ok, I understand better
thanks all for your answer
Best regard
Marc Gravell - 11 Feb 2008 13:56 GMT (note to self: check what happens in VS2005 when people ask about generics)
If you need more clarification, please let me know,
Marc
Marc Gravell - 11 Feb 2008 12:58 GMT But why do you need a constraint? It is working fine as it is? By the way, the code can be written more cleanly in C# 3 (note I've mixed some List<T> and some T[] to check it works for any permutation):
static void Main() { // test with a list of arrys var list = new List<int[]> { new [] {1, 2, 3}, new [] {3, 4, 5}, new [] {4, 5, 6} }; // test with an array of lists var list2 = new [] { new List<string> { "ab", "bc" }, new List<string> { "aa" }, new List<string> { "bb" } }; Write(list.ToArray()); Write(list2.ToArray()); }
So: what isn't working? What is the issue?
Marc
Marc Gravell - 11 Feb 2008 11:53 GMT You can't use it "as is" without forcing the items to the IEnumerable<foo> first; by the way, either way you probably don't need to ToArray();
You can do something like below, but note that you can't use type inference with this approach; you'd need to specify the type-arguments manually. Personally I'd want to understand the use-case a bit more to see if it really needs this level of generics - it might be easier and clearer without the generics...
public static void Write<TInnerList, TItem>(IEnumerable<TInnerList> list) where TInnerList : IEnumerable<TItem> { foreach (IEnumerable<TItem> inner in list) { foreach (TItem item in inner) { Console.WriteLine(item); } } }
Marc
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|