T
trubar a
I’ve at the following link
http://books.google.com/books?id=VGT1_UJzjM0C&lpg=PP1&dq=pro c# 2008&pg=PA193#v=onepage&q&f=false
that instead of initializing inherited members ( _c1 in our example )
inside derived constructor:
class A
{
public int _c;
}
class B:A
{
public B(int c)
{
_c = c;
}
}
we should initialize them inside base class constructor, since that
way we reduce the calls to inherited members ( _c ):
class A
{
public A(int c)
{
_c = c;
}
public int _c;
}
class B:A
{
public B(int c)
: base(c)
{
}
}
If _c field is initialized inside base constructor, the order of
initialization is the following:
1) First the field initializers of derived class B are called
2) Then field initializers of base class A are called (at this point
_c is set to value 0)
3) B’s constructor is called, which in turn calls A’s custom
constructor
4) _c field gets set to value of a parameter c ( inside A’s custom
constructor )
5) Once A’s custom constructor returns, B’s constructor executes its
code.
If _c field is initialized inside B's constructor, the order of
initialization is the following:
1) First the field initializers of a derived class B are called
2) Then field initializers of a base class A are called(at this point
_c is set to value 0)
3) B’s constructor is called, which in turn calls A’s default
constructor
4) Once A’s custom constructor returns, B’s constructor sets _c field
to a value of parameter c
As far as I can tell, in both cases _c was called two times, so how
exactly did we reduce calls to inherited member _c?
thanx
http://books.google.com/books?id=VGT1_UJzjM0C&lpg=PP1&dq=pro c# 2008&pg=PA193#v=onepage&q&f=false
that instead of initializing inherited members ( _c1 in our example )
inside derived constructor:
class A
{
public int _c;
}
class B:A
{
public B(int c)
{
_c = c;
}
}
we should initialize them inside base class constructor, since that
way we reduce the calls to inherited members ( _c ):
class A
{
public A(int c)
{
_c = c;
}
public int _c;
}
class B:A
{
public B(int c)
: base(c)
{
}
}
If _c field is initialized inside base constructor, the order of
initialization is the following:
1) First the field initializers of derived class B are called
2) Then field initializers of base class A are called (at this point
_c is set to value 0)
3) B’s constructor is called, which in turn calls A’s custom
constructor
4) _c field gets set to value of a parameter c ( inside A’s custom
constructor )
5) Once A’s custom constructor returns, B’s constructor executes its
code.
If _c field is initialized inside B's constructor, the order of
initialization is the following:
1) First the field initializers of a derived class B are called
2) Then field initializers of a base class A are called(at this point
_c is set to value 0)
3) B’s constructor is called, which in turn calls A’s default
constructor
4) Once A’s custom constructor returns, B’s constructor sets _c field
to a value of parameter c
As far as I can tell, in both cases _c was called two times, so how
exactly did we reduce calls to inherited member _c?
thanx