Click here to Skip to main content
15,886,006 members
Please Sign up or sign in to vote.
4.00/5 (4 votes)
See more:
C#
class a
{
int x=44;//how it is handling??

}


it is not possible in c++.
I think it is doing with the default constructor by CLR .Is it correct??

I just want to know, how the initialization of variable inside this class is working, like the same example??
there is no constructor ...
Posted
Updated 10-Apr-12 22:05pm
v3
Comments
wizardzz 10-Apr-12 13:38pm    
Very unclear. Sounds like hw, too.

Variables are assigned their values (if any) just before any constructor is called. Take a look at this: http://stackoverflow.com/questions/298183/c-sharp-member-variable-initialization-best-practice[^]
 
Share this answer
 
v3
Comments
samu4u 10-Apr-12 10:11am    
how the .net or c# is managing it rather than c++??
BillW33 10-Apr-12 10:29am    
Not sure what you are asking here. C# and C++ work differently for some things. In this case the C# compiler puts the instructions to initialize the instance variables just before the constructor is called.
samu4u 10-Apr-12 10:36am    
when we will create the object, the constructor will be called and initialize the values .Before of that how the variable will be call or initialize??
BillW33 10-Apr-12 12:27pm    
if a class has an variable declared that is initialized in the declaration, like "int a = 5;", then the compiler creates the assignment statement and places it just before the call to a constructor. When the object is instantiated CLR runs the instructions in the order in which the compiler placed them. So assignments to instance variables happen before any constructor is called including the default constructor. The way that this works is part of the C# language definition, not part of .NET.
samu4u 11-Apr-12 3:23am    
I think may be you are wrong with the concept,even i didn't confirmed....

what i did is that i made a sample class like,

namespace SampleClassInit
{
class SampleClass
{
int x = 55;
int y = 92;
public int MethodAdd()
{
Console.WriteLine("Sample class..");
return 1;
}
}
}

and then i checked the code in ildasm assembler for the low level IL code..
So i found this ....

//IL code...

.ctor:void() method //i think this is the default constructor..

when i explore this method i seen in this way...

.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 24 (0x18)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldc.i4.s 55 //In here X it is initializing
IL_0003: stfld int32 SampleClassInit.SampleClass::x
IL_0008: ldarg.0
IL_0009: ldc.i4.s 92 //In here Y it is initializing
IL_000b: stfld int32 SampleClassInit.SampleClass::y
IL_0010: ldarg.0
IL_0011: call instance void [mscorlib]System.Object::.ctor()
IL_0016: nop
IL_0017: ret
} // end of method SampleClass::.ctor

Initialization is happened inside this method..
Check it and give me the suggestion...\
Thank you..
Items in the Heap (objects) will have an initial value of null. For Value types (put on the stack), the value tends to be 0; false if it is a boolean. The values set for initialization will obviously change this, including values set in the constructor.
 
Share this answer
 
It is doing with the default constructor.
When we are initializing the fields, the CLR will use this assigned value into the default constructor.And when it will convert into MSIL code it will assign this value into the default constructor.

This initialization is not possible with C++ program because there is no CLR, to copy this value into default constructor..

IF you want to see,what is happening in background. You can see the Class( data and functions) through the ILDASM tool..
 
Share this answer
 
v2
Comments
BillW33 12-Apr-12 9:14am    
No, the program is not doing the initialization in the default constructor, the initialization occurs just before the call to any constructor, including the default constructor. The CLR does not create the IL code nor does it set the order of the instructions in the IL; the compiler does these tasks. The CLR translates the IL into machine language and manages the execution of the program. Initializations in other languages are set up by their compiler and have absolutely nothing to do with the presence or absence of a CLR. You should unmark this as the answer because you have your facts wrong.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900