473,396 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Simple question about c-tor and initializing

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
Nov 30 '07 #1
4 1584
On Nov 30, 6:38 am, "TonyJ" <johansson.anders...@telia.comwrote:
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
Nov 30 '07 #2
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.

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"TonyJ" <jo*****************@telia.comwrote in message
news:uw**************@TK2MSFTNGP05.phx.gbl...
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


Nov 30 '07 #3
On 30 Nov., 12:38, "TonyJ" <johansson.anders...@telia.comwrote:
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.
Dec 3 '07 #4
On Nov 30, 2:52 pm, "Ignacio Machin \( .NET/ C# MVP \)" <machin TA
laceupsolutions.comwrote:
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
Dec 3 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
13
by: Michael B Allen | last post by:
Hi, I've tried to write the *simplest* memory allocator possible. I think it would be useful in many cases such as allocating memory on stack as a poor man's garbage collection perhaps. I was...
8
by: Beam_Us_Up_Scotty | last post by:
Hello all, I am trying to write a "simple" animation using C#, and I've tried many things but nothing seems to work for me without leaking memory. Here's a very simple piece of code that uses a...
2
by: Abel Chan | last post by:
Hi there, I just got an assignment to work on server maintenance. It is a weekly task and we have about 7 production servers running Win2K server. The tasks include but not limited to 1)...
7
by: abcd | last post by:
I am trying to set up client machine and investigatging which .net components are missing to run aspx page. I have a simple aspx page which just has "hello world" printed.... When I request...
10
by: serge calderara | last post by:
Dear all, I need to build a web application which will contains articles (long or short) I was wondering on what is the correct way to retrive those article on web page. In orther words, when...
10
by: Andrew | last post by:
Sorry about this but I'm new to ADO.NET (finally coming from simple ADO, bless it) and I'm trying to create a simple three tier program. Ie, User interface Layer / Business object layer / Database...
4
by: darrel | last post by:
We're a two person dev team with a handful of 'testers' that help us out. I'm looking for a simple issue tracker. I don't really want a full blown help desk/bug tracking system, but rather a...
14
by: Giancarlo Berenz | last post by:
Hi: Recently i write this code: class Simple { private: int value; public: int GiveMeARandom(void);
17
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.