Generics question

G

Guest

Hi there

I have a question about generics in C# 2.0.

I have a generic class, Foo of T, which defines a field of type T. This
class is overridden in several base classes which define explicit types (say,
one does this with int, and another with string).

I now want to create a collection class to hold these Foo objects,
presumably inheriting from List<T>, which will be strongly typed. Inside the
collection, I want to define a new method which returns the type that the
field in the Foo is defined as.

I've written this as actual code below for clarity. The question I've got
is, how do I specify the return value of the method GetFirstValue so that it
is strongly typed - i.e. when the collection holds instances of IntFoo, this
method will return int; when the collection holds instances of StringFoo, it
will return string, and when it's holding Foo<T> it will return T?

Or does this particular scenario not make sense?

Thanks very much

- John

------------------
public abstract class Foo<T>
{
public T MyValue;
}

public class IntFoo : Foo<int>
{
}

public class StringFoo : Foo<string>
{
}

public class FooCollection<T> : List<T> where T : Foo<T>
{
// how can I specify the return value will be typeof(T.MyValue)?
public xxxxxxxx GetFirstValue()
{
return this[0].MyValue;
}
}
------------------
 
M

Mattias Sjögren

The question I've got
is, how do I specify the return value of the method GetFirstValue so that it
is strongly typed - i.e. when the collection holds instances of IntFoo, this
method will return int; when the collection holds instances of StringFoo, it
will return string, and when it's holding Foo<T> it will return T?


public T GetFirstValue()
{
return this[0].MyValue;
}

But note that you will not be able to store both StringFoos and
IntFoos in the same collection.


Mattias
 
G

Guest

Hi Mattias

Thanks for your help. I'm still a little vague on why that works though -
isn't T going to be set to either IntFoo or StringFoo? I'm wanting to get out
the value from the object, not the object itself.

One thing I discovered yesterday was to use two type parameters on the
class, such as

public class FooCollection<T, T2> : List<T> where T : Foo<T2>
{
public T2 GetFirstValue { ... }
}

This does actually work, but it just seems a bit messy to me. (It's like I
have to redeclare the nature of the Foos I'm storing in the collection in T2,
when T already implictly contains that information.)

If you have any further thoughts, I'd love to hear them.

Thanks very much

- John

Mattias Sjögren said:
The question I've got
is, how do I specify the return value of the method GetFirstValue so that it
is strongly typed - i.e. when the collection holds instances of IntFoo, this
method will return int; when the collection holds instances of StringFoo, it
will return string, and when it's holding Foo<T> it will return T?


public T GetFirstValue()
{
return this[0].MyValue;
}

But note that you will not be able to store both StringFoos and
IntFoos in the same collection.


Mattias
 
M

Mattias Sjögren

Thanks for your help. I'm still a little vague on why that works though -
isn't T going to be set to either IntFoo or StringFoo?

Yep sorry I think I misread your original code.

One thing I discovered yesterday was to use two type parameters on the
class, such as

public class FooCollection<T, T2> : List<T> where T : Foo<T2>

Yeah if you want to keep storing IntFoos and StringFoos internally I
believe that's what you have to do.

But do they really provide any value beyond their base classes
Foo<int> and Foo<string>? If not, you could get rid of one of the type
parameters and store Foo<T>'s in the list instead, no?

public class FooCollection<T> : List<Foo<T>>


Mattias
 

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