Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# volatile and Nullable

Tags:

c#

According to the docs: http://msdn.microsoft.com/en-us/library/x13ttww7.aspx:

The volatile keyword can be applied to reference types.

So why is it illegal to use on a Nullable<T>.. a reference type!

Please note that I do not actually need volatile semantics on a Nullable<T> field, I encountered this error accidentally and am simply curious.

like image 978
Tergiver Avatar asked Nov 18 '11 23:11

Tergiver


1 Answers

Nullable<T> isn't a reference type. It's a value type:

public struct Nullable<T>
where T : struct, new()

Note the struct part.

Just because it has a null value doesn't make it a reference type... it's a nullable value type. See section 4.1.10 of the C# 4 language spec for more details.

like image 86
Jon Skeet Avatar answered Oct 19 '22 14:10

Jon Skeet