473,327 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Generic method & constraint

Hi group,

please look at my generic method :

static void Write<T>(IEnumerable<IEnumerable<T>list)
{
foreach (IEnumerable<Tts 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
Feb 11 '08 #1
9 4458
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
Feb 11 '08 #2
On 11 fév, 12:24, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
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
Hi jon & marc,

I want a constraint that makes the use only of
IEnumerable<IEnumerableto 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<>, ...

Feb 11 '08 #3
On Feb 11, 12:34 pm, timor.su...@gmail.com wrote:
<snip>
I want a constraint that makes the use only of
IEnumerable<IEnumerableto 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
Feb 11 '08 #4
On 11 fév, 13:42, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Feb 11, 12:34 pm, timor.su...@gmail.com wrote:
<snip>
I want a constraint that makes the use only of
IEnumerable<IEnumerableto 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
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<Tenumerable in list)
{
foreach (T t in enumerable)
{
Console.WriteLine(t);
}
Console.WriteLine();
}
}

but ...
how to call it with a List<List<int>?
Feb 11 '08 #5
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<Tand 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
Feb 11 '08 #6
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
Feb 11 '08 #7
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<TListlist) where TList :
IEnumerable<T{...}

you can use (in VS2005):

Write<List<int>,int>(list);
Write<List<string>,string>(list2);
Feb 11 '08 #8
On 11 fév, 14:36, "Marc Gravell" <marc.grav...@gmail.comwrote:
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<TListlist) 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
Feb 11 '08 #9
(note to self: check what happens in VS2005 when people ask about
generics)

If you need more clarification, please let me know,

Marc
Feb 11 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
6
by: cpnet | last post by:
There is probably an easy answer to this, but I haven't been able to find it. I'm defining a method with 2 generic parameters like void MyMethod<T>(T i1, T i2) I want to be able to use the +...
4
by: Jethro Guo | last post by:
C++ template use constraint by signature,It's very flexible to programmer but complex for complier, and at most time programmer can not get clear error message from complier if error occur. C#...
2
by: Brian Richards | last post by:
I'm trying to write a generic function (List<TPanelType> GetGenericPanels<TPanelType, TObjectType>()) that returns all UserControls that derive from T and and implement an interface...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
2
by: gilbert | last post by:
Hello. I am trying to use c# generic to define a class. class MyClass<Twhere T: new(){ } In this definition, MyClass can create T objects with a default constructor. Is there any way to...
3
by: Anders Borum | last post by:
Hi I need to invoke a generic method determined at runtime. The method has two arguments, a string and a generic type that is constrained to a struct: public void Add<T>(string key, T value)...
0
by: =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?= | last post by:
"Anders Borum" wrote: Hi Anders, I'm afraid the GetMethod() does not currently support filtering on generic parameters so you will have to loop through the existing methods using...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.