Why can't I use SortedList<T, T>?

B

Brett Romero

I'd like to type a SortedList as very generic on class instantiation:
SortedList<T, T> _sl;

Then in two particular methods of the class that is called first(of
which only one or the other is used per instance), I will do

_sl = new SortedList<string,int>();

or

_sl = new SortedList<string,string>();

but I always get this error:
The type or namespace name 'T' could not be found (are you missing a
using directive or an assembly reference?)

Collections.Generic is included. Why is the above error being thrown?
Must I declare both types of list to get around this?

Thanks,
Brett
 
J

Jon Skeet [C# MVP]

Brett Romero said:
I'd like to type a SortedList as very generic on class instantiation:
SortedList<T, T> _sl;

Then in two particular methods of the class that is called first(of
which only one or the other is used per instance), I will do

_sl = new SortedList<string,int>();

or

_sl = new SortedList<string,string>();

but I always get this error:
The type or namespace name 'T' could not be found (are you missing a
using directive or an assembly reference?)

Collections.Generic is included. Why is the above error being thrown?
Must I declare both types of list to get around this?

You can't decide at *run-time* what the types will be - the point of
generics is to make that a *compile-time* decision.

It sounds like you want your containing class to be generic too:

class Foo<T>
{
SortedList<string,T> s1;
}

Then you'd create either a Foo<int> or a Foo<string>.

However, if you've got "two methods, of which only one or the other is
used per instance" that sounds like you should actually have two
different types.
 
B

Brett Romero

I'm doing the same thing with my list. One list compiles as <string,
int> and the other as <string, string>. How is that different than
what you have mentioned?

Thanks,
Brett
 
J

Jon Skeet [C# MVP]

Brett Romero said:
I'm doing the same thing with my list. One list compiles as <string,
int> and the other as <string, string>. How is that different than
what you have mentioned?

In my code, everything is fixed at compile-time. In your code, you're
trying to delay the decision to run-time.
 
B

Brett Romero

How so? In your code, you say to create either Foo<int> or a
Foo<string>. Isn't the decision to use either int or string being
delayed to run time? That's what I'm doing. I also start with "T" and
at some point it becomes either int or string, just as yours does.

Thanks,
Brett
 
J

Jon Skeet [C# MVP]

Brett Romero said:
How so? In your code, you say to create either Foo<int> or a
Foo<string>. Isn't the decision to use either int or string being
delayed to run time?

No. The compiler knows when it's creating Foo<int> or Foo<string>, and
That's what I'm doing. I also start with "T" and
at some point it becomes either int or string, just as yours does.

I suggest you try writing a small program with each way of doing
things, and you should see the difference.
 

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