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> Overviewinterface ITextBox: IControl{void SetText(string text);}interface IListBox: IControl{void SetItems(string[] items);}interface IComboBox: ITextBox, IListBox {}the interface IComboBox inherits from both ITextBox and IListBox.Classes and structs can implement multiple interfaces. In the exampleinterface IDataBound{void Bind(Binder b);}public class EditBox: Control, IControl, IDataBound{public void Paint() {…}public void Bind(Binder b) {…}}the class EditBox derives from the class Control and implements both IControl and IDataBound.In the previous example, the Paint method from the IControl interface and the Bind method fromIDataBound interface are implemented using public members on the EditBox class. <strong>C#</strong> provides analternative way of implementing these methods that allows the implementing class to avoid having thesemembers be public. Interface members can be implemented using a qualified name. For example, theEditBox class could instead be implemented by providing IControl.Paint and IDataBound.Bindmethods.public class EditBox: IControl, IDataBound{void IControl.Paint() {…}void IDataBound.Bind(Binder b) {…}}Interface members implemented in this way are called explicit interface members because each memberexplicitly designates the interface member being implemented. Explicit interface members can only becalled via the interface. For example, the EditBox’s implementation of the Paint method can be calledonly by casting to the IControl interface.class Test{static void Main() {EditBox editbox = new EditBox();editbox.Paint(); // error: no such methodIControl control = editbox;control.Paint(); // calls EditBox’s Paint implementation}}8.10 DelegatesDelegates enable scenarios that some other languages have addressed with function pointers. However,unlike function pointers, delegates are object-oriented and type-safe.A delegate declaration defines a class that is derived from the class System.Delegate. A delegate instanceencapsulates one or more methods, each of which is referred to as a callable entity. For instance methods, acallable entity consists of an instance and a method on that instance. For static methods, a callable entityconsists of just a method. Given a delegate instance and an appropriate set of arguments, one can invoke allof that delegate instance’s methods with that set of arguments.43

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

Saved successfully!

Ooh no, something went wrong!