I created a simple program C#:
C#
class Program { static void Main(string[] args) { Int32? a = null; object x = a; } }
According to MSDN:
Objects based on types with a null value are placed only in the box , if the object is not equal to zero. If HasValue is false, the reference to the object is null instead of the box .
I tried my executable in ILDASMand noticed that the code was ILcalling the box method.
ILDASM
IL
.method private hidebysig static void Main(string[] args) cil managed { .entrypoint // Code size 17 (0x11) .maxstack 1 .locals init ([0] valuetype [mscorlib]System.Nullable`1<int32> a, [1] object x) IL_0000: nop IL_0001: ldloca.s a IL_0003: initobj valuetype [mscorlib]System.Nullable`1<int32> IL_0009: ldloc.0 IL_000a: box valuetype [mscorlib]System.Nullable`1<int32> IL_000f: stloc.1 IL_0010: ret } // end of method Program::Main
My question is: why was he called? Maybe I'm doing something wrong or misunderstanding?
, - x null, null. , MSDN , :
x
null
Int32? a = null; object x = a; object y = a; object.ReferenceEquals(x, y); // true
:
Int32? a = 3; object x = a; object y = a; object.ReferenceEquals(x, y); // false
- , , a null - a , , , ( null).
a
. Release, . :
.method private hidebysig static void Main (string[] args ) cil managed { // Method begins at RVA 0x2054 // Code size 11 (0xb) .maxstack 1 .locals init ( [0] valuetype [mscorlib]System.Nullable`1<int32> ) IL_0000: ldloca.s 0 IL_0002: initobj valuetype [mscorlib]System.Nullable`1<int32> IL_0008: ldloc.0 IL_0009: pop IL_000a: ret } // end of method C::Main
Int? null , Release box.
Int?
box
public void M(int? x) { object y = x; Console.WriteLine(y); } public void Main() { M(null); }
.method public hidebysig instance void M ( valuetype [mscorlib]System.Nullable`1<int32> x ) cil managed { // Method begins at RVA 0x2050 // Code size 12 (0xc) .maxstack 8 IL_0000: ldarg.1 IL_0001: box valuetype [mscorlib]System.Nullable`1<int32> IL_0006: call void [mscorlib]System.Console::WriteLine(object) IL_000b: ret } // end of method C::M .method public hidebysig instance void Main () cil managed { // Method begins at RVA 0x2060 // Code size 16 (0x10) .maxstack 2 .locals init ( [0] valuetype [mscorlib]System.Nullable`1<int32> ) IL_0000: ldarg.0 IL_0001: ldloca.s 0 IL_0003: initobj valuetype [mscorlib]System.Nullable`1<int32> IL_0009: ldloc.0 IL_000a: call instance void C::M(valuetype [mscorlib]System.Nullable`1<int32>) IL_000f: ret } // end of method C::Main
Source: https://habr.com/ru/post/1612089/More articles:Unable to load glade file using Gtk Builder. GTK3 # - c #ΠΠ°ΠΏΡΠΎΡ Objectify/datastore Ρ ΡΠΈΠ»ΡΡΡΠ°ΠΌΠΈ Π½Π΅ΡΠ°Π²Π΅Π½ΡΡΠ²Π° ΠΏΡΠΈ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΈΡ ΡΠ²ΠΎΠΉΡΡΠ²Π°Ρ Ρ ΡΠ°Π½ΠΈΠ»ΠΈΡΠ° Π΄Π°Π½Π½ΡΡ - javaWhat type of MySQL to use for java.time.ZonedDateTime - javaJQuery - ΠΏΡΠ΅Π΄ΠΎΡΠ²ΡΠ°ΡΠ°ΡΡ Π΄Π΅ΠΉΡΡΠ²ΠΈΠ΅ ΠΏΠΎ ΡΠΌΠΎΠ»ΡΠ°Π½ΠΈΡΠΏΡΠΈ ΠΏΠ΅ΡΠ²ΠΎΠΌ ΡΠ΅Π»ΡΠΊΠ΅, ΡΠ°Π·ΡΠ΅ΡΠ°ΡΡ Π΄Π΅ΠΉΡΡΠ²ΠΈΠ΅ ΠΏΠΎ ΡΠΌΠΎΠ»ΡΠ°Π½ΠΈΡ ΠΏΡΠΈ Π²ΡΠΎΡΠΎΠΌ ΡΠ΅Π»ΡΠΊΠ΅ - javascriptFind immediate children using PHP Simple DOM parser - domIs it possible to have a lazy version of `sequence` without using` unsafeIO `? - haskellΠ‘ΡΠ°ΡΠΈΡΠ΅ΡΠΊΠΈΠΉ ΠΏΠΎΡΡΠ΄ΠΎΠΊ ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΠΈ Π² ΠΈΠ΅ΡΠ°ΡΡ ΠΈΠΈ ΠΊΠ»Π°ΡΡΠ° - c++Visual Studio 2015 / Windows 10 SDK SignTool.exe error? - windows-10Definition from Objective-c if the Swift property is declared as dynamic - iosHow to separate the stream `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` ` fooobar.comAll Articles