13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 17 Classesclass A{int x, y, count;}end example]public A() {x = 1;y = -1;object();count = 0;}public A(int n) {x = 1;y = -1;object();count = n;}class B: A{double sqrt2;ArrayList items;int max;public B(): this(100) {B(100);items.Add("default");}public B(int n): base(n – 1) {sqrt2 = Math.Sqrt(2.0);items = new ArrayList(100);A(n – 1);max = n;}}// Variable initializer// Variable initializer// Invoke object() constructor// Variable initializer// Variable initializer// Invoke object() constructor// Invoke B(int) constructor// Variable initializer// Variable initializer// Invoke A(int) constructor17.10.4 Default constructorsIf a class contains no instance constructor declarations, a default instance constructor is automatically provided.That default constructor simply invokes the parameterless constructor of the direct base class. If the direct baseclass does not have an accessible parameterless instance constructor, a compile-time error occurs. If the class isabstract then the declared accessibility for the default constructor is protected. Otherwise, the declaredaccessibility for the default constructor is public. [Note: Thus, the default constructor is always of the formorprotected C(): base() {}public C(): base() {}where C is the name of the class. end note][Example: In the exampleclass Message{object sender;string text;}a default constructor is provided because the class contains no instance constructor declarations. Thus, theexample is precisely equivalent toclass Message{object sender;string text;261

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!