Generic method & constraint

T

timor.super

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
 
J

Jon Skeet [C# MVP]

Hi group,

please look at my generic method :

static void Write<T>(IEnumerable<IEnumerable<T>> list)

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
 
M

Marc Gravell

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
 
T

timor.super

What constraint do you want to apply? Why do you want to apply it?

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<>, ...
 
J

Jon Skeet [C# MVP]

On Feb 11, 12:34 pm, (e-mail address removed) wrote:
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
 
T

timor.super

On Feb 11, 12:34 pm, (e-mail address removed) wrote:


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

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>> ?
 
M

Marc Gravell

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
 
M

Marc Gravell

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
 
M

Marc Gravell

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);
 
T

timor.super

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);

Ok, I understand better

thanks all for your answer

Best regard
 
M

Marc Gravell

(note to self: check what happens in VS2005 when people ask about
generics)

If you need more clarification, please let me know,

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top