|
Subject:
|
ch9:Constructor Execution Sequence
|
|
Posted By:
|
legency
|
Post Date:
|
6/11/2006 9:46:04 PM
|
I purchased this book yesterday, when i read Constructor Execution Sequence in chapter 9. in this book write these: If you use a nondefault constructor of a class, then the default behavior is to use a constructor on the base class that matches the signature of this constructor. If none is found, then the default constructor for the base class is used (which will always happen for the ultimate root System.Object, because this class has no nondefault constructors). and write my code just as in the book: class BaseClass { public BaseClass() { Console.WriteLine("BaseClass()..."); } public BaseClass(int i) { Console.WriteLine("BaseClass(int i)..."); }
} class DerivedClass:BaseClass { public DerivedClass(int i) { Console.WriteLine("DerivedClass(int i)..."); } }
class TestConstructor { public static void Main() { DerivedClass dc1 = new DerivedClass(1); } } run,while the output is: BaseClass()... DerivedClass(int i)... not the result expected as in the book: BaseClass(int i)... DerivedClass(int i)...
Am I doing something wrong? Or the author doesn't run their own example? Help will be really appreciated.
|
|
Reply By:
|
Karli
|
Reply Date:
|
10/30/2006 3:01:58 PM
|
Hi,
You are correct, the defaut behavior is to call the default constructor for the base class, as is decribed in the C# language specification 10.10.1.
I can only apologise for this, and assure you that at some point in the past the behaviour was as written here. I'm sorry if this has caused any difficulties in your understanding of this topic.
Please let me know of any more mistakes like this that you find, and I'll do my best to erradicate them all from future editions.
Cheers,
Karli
---------- www.karliwatson.com IT Specialist & Author Technical Director, www.3form.net
|