List of a generic type

D

Dustin Campbell

Is it possible to have a collection of a generic type?
for example:

class a<t> where t : class, new
{
}
class b
{
List<a<t>> p;
}

I know that the previous example does not work, but any ideas on how
to make that concept work?

I need a collection that stores objects that are of a generic type.

Sure, you can do that by defining the same generic parameter on class b:

class a<t> where t : class, new()
{
}
class b<t> where t : class, new()
{
List<a<t>> p;
}

Best Regards,
Dustin Campbell
Developer Express Inc.
 
J

Jorge Varas

Hi all,

Is it possible to have a collection of a generic type?

for example:

class a<t> where t : class, new
{
}

class b
{
List<a<t>> p;
}

I know that the previous example does not work, but any ideas on how to make
that concept work?

I need a collection that stores objects that are of a generic type.

Jorge Varas
 
J

Jorge Varas

Sorry, my bad, I didn't explained the problem with enough detail.

class a<t> where t: class, new
{
void DoSomething()
{
}
}

class b<u> where u: class, new
{
List<a<v>> list;

void setup()
{
if (typeof(u).FullName == "xxx")
{
list.Add(new a<int>());
list.Add(new a<string>());
}
}

void Cascade()
{
foreach(a<t> item in list)
item.DoSomething();
}
}

So u and v are different types, the fact that b is instanciated with a type t1 does not define that the list is going to be of type a<t1>. Actually, the list might contain instantiations of 'a' with different types under different cases

BTW. I know that the previous example does not work... but it ilustrate the problem that I have.
 
D

Dustin Campbell

Sorry, my bad, I didn't explained the problem with enough detail.
class a<t> where t: class, new
{
void DoSomething()
{
}
}
class b<u> where u: class, new
{
List<a<v>> list;
void setup()
{
if (typeof(u).FullName == "xxx")
{
list.Add(new a<int>());
list.Add(new a<string>());
}
}
void Cascade()
{
foreach(a<t> item in list)
item.DoSomething();
}
}
So u and v are different types, the fact that b is instanciated with a
type t1 does not define that the list is going to be of type a<t1>.
Actually, the list might contain instantiations of 'a' with different
types under different cases

BTW. I know that the previous example does not work... but it
ilustrate the problem that I have.

So, if I understand correctly, you want to be able to use a generic type
without actually closing it. E.g., like this:

class b<u> where u: class, new()
{
List<a<>> list;
}

Correct? If so, you can't do that with C#.

Best Regards,
Dustin Campbell
Developer Express Inc.
 
J

Joanna Carter [TeamB]

"Dustin Campbell" <[email protected]> a écrit dans le
message de news: (e-mail address removed)...

| Correct? If so, you can't do that with C#.

....unless you insert a non-generic base class before the generic one.

class b
{
}

class b<T> : b
{
}

then you can have a List<b>, but you will have to determine what types the
items really are before you deal with them.

Joanna
 
J

Jorge Varas

I can do it using objects and the checking if the type of the object was
constructed from the generic class a, but I hoped for a more elegant
solution.

Thanks for your clarification.
 
J

Jorge Varas

..Thanks! this works for my situation

public abstract class lc {
public abstract void Cascade();
}

public class lvc<T> : lc where T : class, new() {
public override void Cascade() {
}
}

public class a {

List<lc> list;

void SetUp() {
list.Add(new lvc<int>());
list.Add(new lvc<string>());
}

public void DoSomething() {
foreach(lc item in list)
item.Cascade();
}
}

I added the full solution so it is left in the archive... it bothers me to be looking for a solution to a problem just to find a posting saying "Thanks!, I solved" and nothing more...
 

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