Re: constructor
From: Richard Blewett [DevelopMentor] (richardb_at_NOSPAMdevelop.com)
Date: 01/16/05
- Next message: Dakkar: "Reading Registry Values"
- Previous message: W.G. Ryan eMVP: "Re: Performace problem with DataView.RowFilter"
- In reply to: Joakim Karlsson: "Re: constructor"
- Next in thread: Joakim Karlsson: "Re: constructor"
- Reply: Joakim Karlsson: "Re: constructor"
- Messages sorted by: [ date ] [ thread ]
To: microsoft.public.dotnet.languages.csharp Date: Sun, 16 Jan 2005 14:55:54 -0800
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
Richard Blewett [DevelopMentor] wrote:
> 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]
- Next message: Dakkar: "Reading Registry Values"
- Previous message: W.G. Ryan eMVP: "Re: Performace problem with DataView.RowFilter"
- In reply to: Joakim Karlsson: "Re: constructor"
- Next in thread: Joakim Karlsson: "Re: constructor"
- Reply: Joakim Karlsson: "Re: constructor"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|