X hits on this document

754 views

0 shares

0 downloads

0 comments

325 / 396

C# LANGUAGE SPECIFICATION

the classes Class2 and Class3 each contain calls to the conditional method Class1.F, which is conditional based on whether or not DEBUG is defined. Since this symbol is defined in the context of Class2 but not Class3, the call to F in Class2 is included, while the call to F in Class3 is omitted.

The use of conditional methods in an inheritance chain can be confusing. Calls made to a conditional method through base, of the form base.M, are subject to the normal conditional method call rules. In the example

File class1.cs:

using System; using System.Diagnostics;

class Class1 { [Conditional("DEBUG")] public virtual void M() { Console.WriteLine("Class1.M executed"); } }

File class2.cs:

using System;

class Class2: Class1 { public override void M() { Console.WriteLine("Class2.M executed"); base.M();// base.M is not called! } }

File class3.cs:

#define DEBUG

using System;

class Class3 { public static void Test() { Class2 c = new Class2(); c.M();// M is called } }

Class2 includes a call to the M defined in its base class. This call is omitted because the base method is conditional based on the presence of the symbol DEBUG, which is undefined. Thus, the method writes to the console “Class2.M executed” only. Judicious use of pp-declarations can eliminate such problems.

17.4.3 The Obsolete attribute

The attribute Obsolete is used to mark types and members of types that should no longer be used.

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

Document info
Document views754
Page views754
Page last viewedFri Dec 02 20:58:31 UTC 2016
Pages396
Paragraphs9401
Words133190

Comments