constructor

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

It seems to me (using C# 2005 beta and NET 2 beta) that both of these are
the same:

public class test
{
int t = 500;
}

pulbic class test
{
int t;
public test()
{
t = 500;
}
}

Meaning in such case you dont need the constructor. Is that right ? Any
avantages or disavantages ?
 
These two are equivalent. There are a couple of differences bwteen the to but your example doesn;t exhibit either of them:

Field initializers (like in the first example) always run before the constructor body)
FIeld initializers run before the base class cstructor runs, the construcor body rus after hte base class constructor runs

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi,

It seems to me (using C# 2005 beta and NET 2 beta) that both of these are
the same:

public class test
{
int t = 500;
}

pulbic class test
{
int t;
public test()
{
t = 500;
}
}

Meaning in such case you dont need the constructor. Is that right ? Any
avantages or disavantages ?

--
rgds, Wilfried
http://www.mestdagh.biz

--
No virus found in this incoming message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.6.13 - Release Date: 16/01/2005



[microsoft.public.dotnet.languages.csharp]
 
Mixing field initializers and constructors may lead to the fields
getting initialized several time during the construction of an object.

Consider the following class where the field x is considered to have the
default value 5. Each time the object is constructed using the second
constructor, x will be initialized two times.

public class Class
{
private int x = 5;

public Class() {
}

public Class(int x) {
this.x = x;
}

....
}

Regards,
Joakim
 
1) fortunately no-one was suggensting that

2) frankly its not big a deal - 3 machine instructions? The key thing fr me is the potential cnfusionin the cide over what value the field takes

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


Mixing field initializers and constructors may lead to the fields
getting initialized several time during the construction of an object.

Consider the following class where the field x is considered to have the
default value 5. Each time the object is constructed using the second
constructor, x will be initialized two times.

public class Class
{
private int x = 5;

public Class() {
}

public Class(int x) {
this.x = x;
}

...
}

Regards,
Joakim
These two are equivalent. There are a couple of differences bwteen the to but your example doesn;t exhibit either of them:

Field initializers (like in the first example) always run before the constructor body)
FIeld initializers run before the base class cstructor runs, the construcor body rus after hte base class constructor runs

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi,

It seems to me (using C# 2005 beta and NET 2 beta) that both of these are
the same:

public class test
{
int t = 500;
}

pulbic class test
{
int t;
public test()
{
t = 500;
}
}

Meaning in such case you dont need the constructor. Is that right ? Any
avantages or disavantages ?

--
rgds, Wilfried
http://www.mestdagh.biz

--
No virus found in this incoming message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.6.13 - Release Date: 16/01/2005



[microsoft.public.dotnet.languages.csharp]

--
No virus found in this incoming message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.6.13 - Release Date: 16/01/2005



[microsoft.public.dotnet.languages.csharp]
 
Richard said:
1) fortunately no-one was suggensting that

I thought the original poster asked about field initializers vs
constructors in general, and the code snippet provided was there to show
what he was asking about, without knowing the terminology.
2) frankly its not big a deal - 3 machine instructions? The key thing fr me is the potential cnfusionin the cide over what value the field takes

Again, if we are discussing the particular code snippet provided, I
agree. If the field initializers reference something a bit heavier to
create than a primitive, then performance could be a factor too. It depends.

But I agree with you that the code getting less intuitive is the key factor.

Regards,
Joakim
 
Back
Top