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 24 Attributesusing System;using System.Diagnostics;class Class1{[Conditional("DEBUG")]public static void M() {Console.WriteLine("Executed Class1.M");}}class Class2{public static void Test() {Class1.M();}}declares Class1.M as a conditional method. Class2's Test method calls this method. Since theconditional compilation symbol DEBUG is defined, if Class2.Test is called, it will call M. If the symbolDEBUG had not been defined, then Class2.Test would not call Class1.M. end example]It is important to understand that the inclusion or exclusion of a call to a conditional method is controlled bythe conditional compilation symbols at the point of the call. [Example: In the example// Begin class1.csusing System;using System.Diagnostics;class Class1{[Conditional("DEBUG")]public static void F() {Console.WriteLine("Executed Class1.F");}}// End class1.cs// Begin class2.cs#define DEBUGclass Class2{public static void G() {Class1.F();// F is called}}// End class2.cs// Begin class3.cs#undef DEBUGclass Class3{public static void H() {Class1.F();// F is not called}}// End class3.csthe classes Class2 and Class3 each contain calls to the conditional method Class1.F, which isconditional based on whether or not DEBUG is defined. Since this symbol is defined in the context of Class2but not Class3, the call to F in Class2 is included, while the call to F in Class3 is omitted. end example]The use of conditional methods in an inheritance chain can be confusing. Calls made to a conditionalmethod through base, of the form base.M, are subject to the normal conditional method call rules.[Example: In the example313

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

Saved successfully!

Ooh no, something went wrong!