C # review 6,
C # review 6
June 19, 2016
23: 46
Main Interfaces & Delegates interface and Delegation
1. Basic interface syntax
public interface IList : ICollection, IEnumerable {int Add (object value); // methodsbool Contains (object value);...bool IsReadOnly { get; } // property...object this [int index] { get; set; } // indexer}
The interface is equivalent to an abstract class, and only the signature is not implemented;
Interface = purely abstract class; only signatures, no implementation.
The interface may contain methods, attributes, indexers, and time (no field, no constant, no constructor, no destructor, no operators, and no cascading type)
May contain methods, properties, indexers and events
(No fields, constants, constructors, destructors, operators, nested types ).
Abstract or virtual keywords are hidden in interface members.
Interface members are implicitly public abstract (virtual ).
The interface member cannot be static.
Interface members must not be static.
Interfaces can be inherited from other interfaces.
Interfaces can inherit from other interfaces.
Class and struct can implement multiple interfaces
Classes and structs may implement multiple interfaces.
2. Implemented by Classes and Structs class and struct implementation Interface
class MyClass : MyBaseClass, IList, ISerializable {public int Add (object value) {...}public bool Contains (object value) {...}...public bool IsReadOnly { get {...} }...public object this [int index] { get {...} set {...} }}
A class can inherit only one class but can implement multiple interfaces.
A class can inherit from a single base class, but can implement multiple interfaces.
A struct cannot inherit from other classes or struct, but multiple interfaces can be implemented.
A struct cannot inherit from any type, but can implement multiple interfaces.
Each member in the interface must be implemented
Every interface member (method, property, indexer) must be implemented or inherited from a base class.
Methods included in the implementation interface do not need to be declared as override
Implemented interface methods need not be declared as override.
Methods included in the implementation interface can be declared as abstract methods.
Implemented interface methods can be declared as abstract (I. e. an interface can be implemented by an abstract class ).
If the Add method in the subclass MyClass should be overwritten, it should be declared as virtual although the virtual keyword has been hidden in the IList.
If Add () shocould be overridden in a subclasses of MyClass it must be declared as virtual (although Add () is already implicitly virtual in IList ).
3. Working with Interfaces
Example:
interface ISimpleReader {int Read();}interface IReader : ISimpleReader {void Open(string name);void Close();}class Terminal : ISimpleReader {public int Read() { ... }}class File : IReader {public int Read() { ... }public void Open(string name) { ... }public void Close() { ... }}ISimpleReader sr = null; // null can be assigned to any variable of an interface typesr = new Terminal();sr = new File();IReader r = new File();sr = r;
4. Delegate by Delegate
Delegate Declaration: delegate void Notifier (string sender); // composition: add the keyword delegate to the signature of a Common Function
Declare the delegate variable: Notifier greeting;
Assign the method to the delegate variable:
void SayHello(string sender) {Console.WriteLine("Hello from " + sender);}greetings = new Notifier(SayHello); // or just:greetings = SayHello; // since C# 2.0
Call the delegate variable: greeting ("John ");
5. Different Methods for allocation
Each method can be assigned to a delegate variable:
void SayGoodBye(string sender) {Console.WriteLine("Good bye from " + sender);}greetings = SayGoodBye;greetings("John"); // SayGoodBye("John") => "Good bye from John"
Note: The delegate variable can be assigned a null value;
If the delegate variable is null, the delegate variable cannot be called; otherwise, an exception occurs;
Delegate variables are actually a type of data structure that can be stored and parameters can be passed
Creating a Delegate Value:
M = obj. Method; // or in long form: m = new DelegateType (obj. Method );
A delegate variable can store a Method and Its receiver, not a parameter: greetings = myObj. SayHello;
If obj is this, it can be omitted: greetings = SayHello;
The method can be static, but you need to use the class name for the instance, greetings = MyClass. StaticSayHello;
Methods cannot be abstract, but virtual, override, new
The signature of the method must match the signature of the delegate.
There are the same number of parameters;
Has the same parameter type, including the return type;
With the same parameter modifier (value, ref/out)
6. Multicast delegate Multicast Delegates
One delegate variable can master multiple methods at the same time;
Notifier greetings;greetings = SayHello;greetings += SayGoodBye;greetings("John"); // "Hello from John"// "Good bye from John"greetings -= SayHello;greetings("John"); // "Good bye from John"
Note:
If the multicast delegate is a function, the return value is the method called last time;
If the multicast delegate is an out-modified parameter type, the parameter should be the type of the. ref modified parameter returned by the last call and should be passed through the method used.
Java implements the above functions:
7. Events
An event is a Special Delegate domain;
Events are different from delegate variables:
Only the declared event class can release the event;
Other classes can only change the event domain and can only use + = or-=
class Model {public event Notifier notifyViews;public void Change() { ... notifyViews("Model"); }}class View {public View(Model m) { m.notifyViews += Update; }void Update(string sender) { Console.WriteLine(sender + " was changed"); }}class Test {static void Main() {Model model = new Model();new View(model); new View(model); ...model.Change();}}
How to handle events in. NET Library
public delegate void KeyEventHandler (object sender, KeyEventArgs e);public class KeyEventArgs : EventArgs {public virtual bool Alt { get {...} } // true if Alt key was pressedpublic virtual bool Shift { get {...} } // true if Shift key was pressedpublic bool Control { get {...} } // true if Ctrl key was pressedpublic bool Handled { get{...} set {...} } // indicates if event was already handledpublic int KeyValue { get {...} } // the typed key code...}class MyKeyEventSource {public event KeyEventHandler KeyDown;...KeyDown(this, new KeyEventArgs(...));...}class MyKeyListener {public MyKeyListener(...) { keySource.KeyDown += HandleKey;}void HandleKey (object sender, KeyEventArgs e) {...}}