Sunday, December 1, 2013

How object is created using Type/Class? [ClassName] [ObjectName] = new [ClassName()];

Hello Mohan:


It should be explained by IL language, which looks more easily:


【Common Codes in C#】



namespace CSharp
{
public class MainTest
{
static void Main(string[] args)
{
MainTest mt = new MainTest();
}
}
}

【IL】



.class public auto ansi beforefieldinit CSharp.MainTest
extends [mscorlib]System.Object
{
.method public hidebysig specialname rtspecialname instance void .ctor () cil managed
{
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
}

.method private hidebysig static void Main (
string[] args
) cil managed
{
.entrypoint
.locals init (
[0] class CSharp.MainTest mt
)

IL_0000: nop
IL_0001: newobj instance void CSharp.MainTest::.ctor()
IL_0006: stloc.0
IL_0007: ret
}
}

It will generate a variable in the local init part, and then it will create a new instance of CSharp's MainTest class. Then pops this instance into local init [0].


See:http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.stloc_0.aspx




ASP.NET Questions

Other Discussions

FreeRice Donate

Issues to report



No comments:

Post a Comment