Re: constructor

From: Richard Blewett [DevelopMentor] (richardb_at_NOSPAMdevelop.com)
Date: 01/16/05


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]



Relevant Pages

  • Re: constructor
    ... Mixing field initializers and constructors may lead to the fields ... constructor, x will be initialized two times. ... public class Class ... > public class test ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Help with error
    ... * a copy constructor ... This class needs a destructor for deleting the allocated memory. ... can be declared private and be not implemented. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: what is the RIGHT THING to make a constructor of a subclass, using super()
    ... If you are left with that requirement as a necessary or desirable part of the design, then the usual approach is probably to write a static method in the class that you call as an argument to the call to superfrom your constructor. ... public class TestDerived extends TestBase ... public Customer(String firstName, String lastName) { ... public class SlashSeparatedCustomer extends Customer { ...
    (comp.lang.java.programmer)
  • Re: Help with error
    ... > * a copy constructor ... > Test(const Test& Arg) ... > struct Coord ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Do some computation before calling super constructor.
    ... I want to do some computation in my constructor before invoking the ... Now for some manipulations of automata, ... public class StateSet extends State { ...
    (comp.lang.java.help)

Loading