X hits on this document

902 views

0 shares

0 downloads

0 comments

267 / 396

C# LANGUAGE SPECIFICATION

public A() { x = 1;// Variable initializer y = -1;// Variable initializer object();// Invoke object() constructor count = 0; }

public A(int n) { x = 1;// Variable initializer y = -1;// Variable initializer object();// Invoke object() constructor count = n; } }

class B: A { double sqrt2; ArrayList items; int max;

public B(): this(100) { B(100);// Invoke B(int) constructor items.Add("default"); }

public B(int n): base(n – 1) { sqrt2 = Math.Sqrt(2.0);// Variable initializer items = new ArrayList(100);// Variable initializer A(n – 1);// Invoke A(int) constructor max = n; } }

10.10.4 Default constructors

If 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 base class does not have an accessible parameterless instance constructor, a compile-time error occurs. If the class is abstract then the declared accessibility for the default constructor is protected. Otherwise, the declared accessibility for the default constructor is public. Thus, the default constructor is always of the form

protected C(): base() {}

or

public C(): base() {}

where C is the name of the class.

In the example

class Message { object sender; string text; }

a default constructor is provided because the class contains no instance constructor declarations. Thus, the example is precisely equivalent to

254Copyright Microsoft Corporation 1999-2003. All Rights Reserved.

Document info
Document views902
Page views902
Page last viewedTue Dec 06 19:10:48 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments