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// Author Bnamespace B{class Derived: A.Base // version 2a: new}{}new public virtual void F() {System.Console.WriteLine("Derived.F");}On the other hand, Derived’s author might investigate further, and decide that Derived’s F shouldoverride Base’s F. This intent can be specified by using the override keyword, as shown below.// Author Anamespace A{public class Base // version 2{public virtual void F() { // added in version 2System.Console.WriteLine("Base.F");}}}// Author Bnamespace B{class Derived: A.Base // version 2b: override}{}public override void F() {base.F();System.Console.WriteLine("Derived.F");}The author of Derived has one other option, and that is to change the name of F, thus completely avoidingthe name collision. Although this change would break source and binary compatibility for Derived, theimportance of this compatibility varies depending on the scenario. If Derived is not exposed to otherprograms, then changing the name of F is likely a good idea, as it would improve the readability of theprogram—there would no longer be any confusion about the meaning of F.8.14 Attributes<strong>C#</strong> is an imperative language, but like all imperative languages it does have some declarative elements. Forexample, the accessibility of a method in a class is specified by declaring it public, protected,internal, protected internal, or private. <strong>C#</strong> generalizes this capability, so that programmers caninvent new kinds of declarative information, attach this declarative information to various program entities,and retrieve this declarative information at run-time. Programs specify this additional declarativeinformation by defining and using attributes (§24).For instance, a framework might define a HelpAttribute attribute that can be placed on program elementssuch as classes and methods, enabling developers to provide a mapping from program elements todocumentation for them. The exampleusing System;[AttributeUsage(AttributeTargets.All)]public class HelpAttribute: Attribute{public HelpAttribute(string url) {this.url = url;}public string Topic = null;private string url;48

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

Saved successfully!

Ooh no, something went wrong!