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 8 <strong>Language</strong> Overview}A a = b;a.F();// Treat a B as an A}shows a class B that derives from A. The class B inherits A’s F method, and introduces a G method of its own.Methods, properties, and indexers can be virtual, which means that their implementation can be overriddenin derived classes. The exampleusing System;class A{public virtual void F() { Console.WriteLine("A.F"); }}class B: A{public override void F() {base.F();Console.WriteLine("B.F");}}class Test{static void Main() {B b = new B();b.F();A a = b;a.F();}}shows a class A with a virtual method F, and a class B that overrides F. The overriding method in B containsa call, base.F(), which calls the overridden method in A.A class can indicate that it is incomplete, and is intended only as a base class for other classes, by includingthe modifier abstract. Such a class is called an abstract class. An abstract class can specify abstractmembers—members that a non-abstract derived class must implement. The exampleusing System;abstract class A{public abstract void F();}class B: A{public override void F() { Console.WriteLine("B.F"); }}class Test{static void Main() {B b = new B();b.F();A a = b;a.F();}}introduces an abstract method F in the abstract class A. The non-abstract class B provides an implementationfor this method.8.8 StructsThe list of similarities between classes and structs is long—structs can implement interfaces, and can havethe same kinds of members as classes. Structs differ from classes in several important ways, however:41

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

Saved successfully!

Ooh no, something went wrong!