If I declare a nullable (either via Nullable or the ? symbol) variable from a value type, does it still respect the rules of value types (i.e. pass by value by default, deallocated when it falls out of scope, not when the garbage collector runs) or does it turn into a reference type since it's actually of type Nullable?
I don't mean it in a critical way, but I think that some references would be helpful.. as Jacob did. They might lead the reader to more in-depth information, allowing him to perform some connections with other issues and so on.
lmsasu
2010-01-03 12:41:28
I tend to only provide references to articles that can't be accessed using google's i'm feeling lucky feature. Providing a link to google result number 1 is a pointless excercise.
Paul Creasey
2010-01-03 13:29:26
+1
A:
Nullable value types are essentially just a struct wrapper around a value type which allows them to be flagged as null.
something like this:
struct Nullable<T>
{
public T Value;
public bool HasValue;
}
I believe it's actually more complex than that under the hood, and the compiler does lots of nice stuff for you so you can write for example if(myInt == null)
. But yes, they are still value types.
fearofawhackplanet
2010-01-03 12:31:06
+5
A:
The documentation is here:
http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx
As you can see, the documentation describes this as the "nullable structure", indicating that it is a value type. Also, the documentation gives the first lines of the declaration of the type:
public struct Nullable<T> where T : struct, new()
again showing that the type is a value type.
Eric Lippert
2010-01-03 18:19:22