Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# struct, how to assign a null value?

I have a list:

List<int, SomeStruct> 

For some reason, it is not allowing me to assign null values to it. What do I do if I want to have no struct associated?

like image 200
Andy Hin Avatar asked Nov 10 '10 20:11

Andy Hin


1 Answers

Use nullable types:

List<int, SomeStruct?> 

You can't assign null to an element of the list because structs are value types, while null means an empty pointer, and so can only be assigned to reference type variables.

Also note that List as you're using it doesn't exist in .NET! Perhaps you want Dictionary?

like image 147
Will Vousden Avatar answered Oct 08 '22 02:10

Will Vousden