Int I=7;
Int ? j= null;
Here we are able to assign a reference type(null) to a value type(j). As this nullable thing comes under System.Nullable namespace which is a structure. How this is possible.??
Int I=7;
Int ? j= null;
Here we are able to assign a reference type(null) to a value type(j). As this nullable thing comes under System.Nullable namespace which is a structure. How this is possible.??
int ? j= null
is syntactic sugar.
This:
private void Foo()
{
int? i = null;
}
this:
private void Foo()
{
Nullable<int> i = null;
}
and this:
private void Foo()
{
Nullable<int> i = new Nullable<int>();
}
compile into:
.method private hidebysig instance void Foo() cil managed
{
// Code size 10 (0xa)
.maxstack 1
.locals init ([0] valuetype [mscorlib]System.Nullable`1<int32> i)
IL_0000: nop
IL_0001: ldloca.s i
IL_0003: initobj valuetype [mscorlib]System.Nullable`1<int32>
IL_0009: ret
} // end of method Program::Foo
where
IL_0001: ldloca.s i
IL_0003: initobj valuetype [mscorlib]System.Nullable`1<int32>
means initializing an instance of a structure.
Thus there is no null-reference and an object exists. One can access its readonly property HasValue
without NullReferenceException
.
int? i = null;
Console.WriteLine(i.HasValue);
gives
False
and
Console.WriteLine(i.Value);
throws not NullReferenceException, but InvalidOperationException.
Though
Console.WriteLine(i.GetType());
throws NullReferenceException.
Long story short, for Nullable<T>
or T?
null
means not a null-reference
, but an instance of Nullable<T>
with HasValue
set to false.
BUT
If you are looking for a WAY
how a reference type can be assigned to a structure in c#
try this:
class Program
{
private static void Main(string[] args)
{
Struct<int> @struct = new Class<int>(-1);
Class<int> @class = new Struct<int>(-1);
}
}
class Class<T>
{
public T Value { get; }
public Class(T value) => Value = value;
public static implicit operator Struct<T>(Class<T> instance) => new Struct<T>(instance.Value);
}
struct Struct<T>
{
public T Value { get; }
public Struct(T value) => Value = value;
public static implicit operator Class<T>(Struct<T> instance) => new Class<T>(instance.Value);
}
Remember, it just looks as assignment, but before real assignment happens casting takes place.
The C# specification is clear on this point: the null
keyword represents an expression that has no type. It is neither reference type nor value type nor pointer type. However, that expression is convertible to all of those types. There is no requirement that the compiler classifies all expressions as having a type, and in fact, it does not.
Any type can contain null
1) For Reference Types:
If the reference type variable contains null
that means it is not pointing to anything on the heap. Reference type variable stored on the stack and may contain null
or a reference to an actual heap object.
2) For Value Types: If the nullable value type variable contains null
that means it does not hold any value within itself. Value type stored on the stack(in most cases) and can contain the direct value within itself or null
.