Why int? set to null have instance properties?

I am curious why the following code works (executed under the VS debugger):

int? x = null; null x.HasValue false 

If x really null, which instance does HasValue ? Is HasValue implemented as an extension method, or is it a special case of the compiler to make it a magical way?

+7
source share
9 answers

Because x not a reference type. ? is just syntactic sugar for Nullable<T> , which is a struct (value type).

+9
source

int? actually is a Nullable<int> structure. Therefore, your x cannot be null, since it is always an instance of the structure.

+5
source

Manipulating answer: unmanaged structures are magic.

Longer answer: zero is not what the value represents. When you assign null to a null structure, what you see happening behind the scenes is different.

 int? val = null; // what you wrote Nullable<Int32> val = new Nullable<Int32>(); // what is actually there 

In this case, an instance of the structure is created for which the T Value set to the default value, and the bool HasValue property is false.

The only time you really get a null reference from a Nullable<T> will be if it is squared, like a Nullable<T> box directly on T or null, depending on whether the value is set.

+4
source

There are several null values.

One programming language that represents variables and memory in a pointer-based method (which includes C # links, although it hides some details) is "it doesn't point to anything."

Another means that "it does not matter."

With reference types, we often use the former to represent the latter. We could use string label = null to mean "no meaningful label". It remains, however, that the point is what happens in terms of where in memory and what points to it. However, this is pretty damn useful, what a shame we couldn't do it with int and DateTime in C # 1.1

What Nullable<T> suggests means to say "no meaningful value," but at a level below it is not null in the same way as a null string (unless indicated in the box). It was assigned a null value and it is null, so it is logically null and null in accordance with some other semantics, but it is not null in the difference "does not point to anything" between reference and type values.

It is only the attribute "does not point to anything" of the reference type that prevents you from invoking instance methods on it.

And in fact, even this is not entirely true. IL allows you to reference instance methods on a null reference, and if it does not interact with any fields, it will work. It cannot work if it (directly or indirectly) needs these fields because they do not exist in a null refernce, but it can call null.FineWithNull() if this method has been defined as:

 int FineWithNull() { //note that we don't actually do anything relating to the state of this object. return 43; } 

With C #, it was decided to prohibit this, but this is not the rule for all .NET (I think F # allows this, but I'm not sure, I know that unmanaged C ++ allowed this, and it was useful in some very rare cases).

+2
source

When using int? x = null int? x = null then x assigned a new instance of Nullable<int> , and the value of ist is null .

I don’t know the internal elements exactly, but I would suggest that the assignment operator itself is somewhat overloaded.

+1
source

A type with a null value is not really null , since it still does not bypass the fact that value types cannot be null . Instead, it is a reference to a Nullable<> structure (which is also a value type and cannot be null ).

More details here .

Basically, you always refer to an instance of something when using a type with a null value. The default information returned by this link is the stored value (or null if there is no stored value).

+1
source

Nullable is not a reference type, and its instance methods are one of the places where this manifests itself. In fact, it is a type of structure containing a logical flag and a value of the type by which it is valid. In some cases, various language operators (to cancel or consider (bool?) Null false in some cases] and a null literal, as well as boxing for special cases in runtime, but other than that, it's just a structure.

+1
source

This is a completely new type. Nullable is not T.

You have a general class something like this:

 public struct Nullable<T> { public bool HasValue { get { return Value != null; } } public T Value { get; set; } } 

I am sure there is more (especially in getter and setter, but in a nutshell.

+1
source

A type with a null value (in this case: nullable int) has a HasValue property, which is a boolean. If HasValue is True, a Value (of type T, in this case, int) will have a valid value.

0
source

All Articles