473,663 Members | 2,800 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(d ouble num);
private DoCalculate[] calculation;
private int calculationCoun ter;

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

class Test2
{
delegate double DoCalculation(d ouble num);
private DoCalculate[] calculation = new DoCalculation[10];
private int calculationCoun ter = 0;
}
//Tony
Nov 30 '07 #1
4 1601
On Nov 30, 6:38 am, "TonyJ" <johansson.ande rs...@telia.com wrote:
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(d ouble num);
private DoCalculate[] calculation;
private int calculationCoun ter;

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

}

class Test2
{
delegate double DoCalculation(d ouble num);
private DoCalculate[] calculation = new DoCalculation[10];
private int calculationCoun ter = 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.com wrote in message
news:uw******** ******@TK2MSFTN GP05.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(d ouble num);
private DoCalculate[] calculation;
private int calculationCoun ter;

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

class Test2
{
delegate double DoCalculation(d ouble num);
private DoCalculate[] calculation = new DoCalculation[10];
private int calculationCoun ter = 0;
}
//Tony


Nov 30 '07 #3
On 30 Nov., 12:38, "TonyJ" <johansson.ande rs...@telia.com wrote:
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(d ouble num);
private DoCalculate[] calculation;
private int calculationCoun ter;

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

}

class Test2
{
delegate double DoCalculation(d ouble num);
private DoCalculate[] calculation = new DoCalculation[10];
private int calculationCoun ter = 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 calculationCoun ter 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
2700
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
5725
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 hoping the clc crowd had some ideas for making it even simpler! In-lined below the full program (132 lines). It's a circular singular linked list of "cells" inspired by the one in Plauger's text
8
3687
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 timer to set the image of a label from an ImageList every 100ms or so. This is what was being used when I inherited this piece of code, and I thought it was OK, until I changed the size of the label to do animation on a larger piece of screen...
2
2370
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) Find out drive free space for each server’s disk drives (C:, D:, E:, and etc) 2) Report critical errors from the event logs (Application, System, replication and etc) for each server.
7
2281
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 that page like http://machinename/dir1/hellp.aspx instead of running that page it starts downloding ...whats missing here ....why the aspx engine not running the page....
10
2367
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 there is such information to be displayed are they coming from imported files, database ? Where and how this type of information is stored ? What is the way to retrieve such information in order to display it in page ?
10
1937
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 layer I've got a simple program that is activilly populating and writing my satasets. They are currently only mapping the database tables.So 1 dataset, 5 datatables and several relationships. In this program I can't seem to hide the database...
4
2063
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 simple one. I'm thinking a wiki might be a good tool for this. Really, it's just a place for a few of us to jot our notes in a central location. If we have a list of bugs, it likely won't need to be anything more complex than a bulleted list of...
14
2974
by: Giancarlo Berenz | last post by:
Hi: Recently i write this code: class Simple { private: int value; public: int GiveMeARandom(void);
17
5807
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: _________________________________________________________________ /* Simple Thread Object ______________________________________________________________*/ #include <pthread.h> extern "C" void* thread_entry(void*);
0
8858
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8771
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8634
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7371
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5657
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4182
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.