I have SortedList implemented as a generic one like this:

SortedList<int , string> myList = new SortedList<int ,string>(10);

What's the benefit of having int instead of just int as a key

Key can't be null so if I set:

int a = null;

myList.Add(a, "foo");

It will compile but still won't work. So should it be int I found it in a code but don't get why is there.


Re: Visual C# Language What's the difference ?

Figo Fei - MSFT

Hi, Jedrzej

int  is what called Nullable type, we can use nullable type more flexibly and with more convenience. (it has HasValue and Value properties)

So, it is used as a kind of Generics.

It is one of the "late breaking" features in C# 2.0. The details can be found in the C# 2.0 language spec.

Nullable types address the scenario where you want to be able to have a primitive type with a null (or unknown) value. This is common in database scenarios, but is also useful in other situations.

Please see: id=2371


Simply, in the condition you described, you dont need to use it.

However, it is needed in other senarios.

Thank you

Re: Visual C# Language What's the difference ?


I know what nullable type is. My question is more concerned if using it in this case won't introduce some bug Beacuse now I can compile SortedList when I have a null as a key. I wrote example of how to achive this.

So should I live it as it is or Maybe there is some gain of using int in this case


Re: Visual C# Language What's the difference ?

Figo Fei - MSFT


When you set null as a key it will generate a error at compile time if you don't use nullable type, otherwise error will occur at run time if you use nullable type.

In the case, it's no definite answer to decide which should be better.

So, we cannot discuss the advantages or shortages without specific examples.


Re: Visual C# Language What's the difference ?

Marcelo Guerra - MSFT

My vote goes for not using nullable int. Why did you used it in the first place