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.

<strong>C#</strong> LANGUAGE SPECIFICATION}end example]public Message(): base() {}17.10.5 Private constructorsWhen a class declares only private instance constructors, it is not possible for other classes to derive from thatclass or to create instances of that class (an exception being classes nested within that class). [Example: Privateinstance constructors are commonly used in classes that contain only static members. For example:public class Trig{private Trig() {} // Prevent instantiationpublic const double PI = 3.14159265358979323846;public static double Sin(double x) {…}public static double Cos(double x) {…}public static double Tan(double x) {…}}The Trig class groups related methods and constants, but is not intended to be instantiated. Therefore, it declaresa single empty private instance constructor. end example] At least one instance constructor must be declared tosuppress the automatic generation of a default constructor.17.10.6 Optional instance constructor parameters[Note: The this(…) form of constructor initializer is commonly used in conjunction with overloading toimplement optional instance constructor parameters. In the exampleclass Text{public Text(): this(0, 0, null) {}public Text(int x, int y): this(x, y, null) {}public Text(int x, int y, string s) {// Actual constructor implementation}}the first two instance constructors merely provide the default values for the missing arguments. Both use athis(…) constructor initializer to invoke the third instance constructor, which actually does the work ofinitializing the new instance. The effect is that of optional constructor parameters:end note]Text t1 = new Text();// Same as Text(0, 0, null)Text t2 = new Text(5, 10);// Same as Text(5, 10, null)Text t3 = new Text(5, 20, "Hello");17.11 Static constructorsA static constructor is a member that implements the actions required to initialize a class. Static constructors aredeclared using static-constructor-declarations:static-constructor-declaration:attributes opt static-constructor-modifiers identifier ( ) static-constructor-bodystatic-constructor-modifiers:extern opt staticstatic extern optstatic-constructor-body:block;A static-constructor-declaration may include a set of attributes (§24) and an extern modifier (§17.5.7).262

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

Saved successfully!

Ooh no, something went wrong!