Simple question about c-tor and initializing

T

TonyJ

Hello!

Assume I have the two classes Test1 and Test2. They are very similar.
Test1 initialize the instance variable in the C-tor but
Test2 initialize the instance variable without using the C-tor.

Both classes can be seen below.
In this simple example it doesn't matter because Test1 is the same as Test2.
Do you agree with me.

class Test1
{
delegate double DoCalculation(double num);
private DoCalculate[] calculation;
private int calculationCounter;

public Test()
{
calculationCounter = 0;
calculation = new DoCalculation[10];
}
}

class Test2
{
delegate double DoCalculation(double num);
private DoCalculate[] calculation = new DoCalculation[10];
private int calculationCounter = 0;
}
//Tony
 
J

John Duval

Hello!

Assume I have the two classes Test1 and Test2. They are very similar.
Test1 initialize the instance variable in the C-tor but
Test2 initialize the instance variable without using the C-tor.

Both classes can be seen below.
In this simple example it doesn't matter because Test1 is the same as Test2.
Do you agree with me.

class Test1
{
delegate double DoCalculation(double num);
private DoCalculate[] calculation;
private int calculationCounter;

public Test()
{
calculationCounter = 0;
calculation = new DoCalculation[10];
}

}

class Test2
{
delegate double DoCalculation(double num);
private DoCalculate[] calculation = new DoCalculation[10];
private int calculationCounter = 0;}

//Tony

Hi Tony,
In the case of your Test2 class, a default constructor will be
generated for you, which does the initialization of your member
variables. It's probably worthwhile to take a look at the code
generated in the two cases by using a tool like ILDASM... you'll see
that there is almost no difference in the generated IL. The only
difference I saw was whether or not the System.Object ctor was called
before or after the initialization of your members.

John
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


Take a look at the code generated you will see it's rather similar, if no
constructor is provided one is generated for you. In any case the framework
assure you that the variables will be initialized BEFORE any constructor
and/or access to any method.

Jon Skeet has a nice discussion of BeforeInit in his website, take also a
look at it.
 
A

Arne Vajhøj

TonyJ said:
Assume I have the two classes Test1 and Test2. They are very similar.
Test1 initialize the instance variable in the C-tor but
Test2 initialize the instance variable without using the C-tor.

Both classes can be seen below.
In this simple example it doesn't matter because Test1 is the same as Test2.
Do you agree with me.

class Test1
{
delegate double DoCalculation(double num);
private DoCalculate[] calculation;
private int calculationCounter;

public Test()
{
calculationCounter = 0;
calculation = new DoCalculation[10];
}
}

class Test2
{
delegate double DoCalculation(double num);
private DoCalculate[] calculation = new DoCalculation[10];
private int calculationCounter = 0;
}

There are no functional difference here.

You need to do that type of stuff in the constructor if you
need to execute code before or in between initializations.

I would always do it in the constructor if the order of
initialization matters.

In general I prefer the constructor for complex stuff and
the other for very simple initializations.

Arne
 
K

KWienhold

Hello!

Assume I have the two classes Test1 and Test2. They are very similar.
Test1 initialize the instance variable in the C-tor but
Test2 initialize the instance variable without using the C-tor.

Both classes can be seen below.
In this simple example it doesn't matter because Test1 is the same as Test2.
Do you agree with me.

class Test1
{
delegate double DoCalculation(double num);
private DoCalculate[] calculation;
private int calculationCounter;

public Test()
{
calculationCounter = 0;
calculation = new DoCalculation[10];
}

}

class Test2
{
delegate double DoCalculation(double num);
private DoCalculate[] calculation = new DoCalculation[10];
private int calculationCounter = 0;}

//Tony

I normally set my instance fields to some sensible value (as long as
that is possible) and use the constructor to set instance-specific
values.
So, if calculationCounter and DoCalculate have fixed values when a new
instance is created, I'd leave it out of the constructor.
In the example you posted the behavior is the same though anyway.
 
J

Jon Skeet [C# MVP]

Take a look at the code generated you will see it's rather similar, if no
constructor is provided one is generated for you. In any case the framework
assure you that the variables will be initialized BEFORE any constructor
and/or access to any method.

Jon Skeet has a nice discussion of BeforeInit in his website, take also a
look at it.

Just to clarify, beforefieldinit is relevant to discussions of static
constructors, not instance constructors.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top