Presentation is loading. Please wait.

Presentation is loading. Please wait.

Effective C# 50 Specific Way to Improve Your C# Item 20, 21 Sephiroth.Wang2012/08/01.

Similar presentations


Presentation on theme: "Effective C# 50 Specific Way to Improve Your C# Item 20, 21 Sephiroth.Wang2012/08/01."— Presentation transcript:

1 Effective C# 50 Specific Way to Improve Your C# Item 20, 21 Sephiroth.Wang2012/08/01

2 Agenda Item 20: Distinguish Between Implementing Interfaces and Overriding Virtual Functions Item 20: Distinguish Between Implementing Interfaces and Overriding Virtual Functions Item 21: Express Callbacks with Delegates Item 21: Express Callbacks with Delegates

3 Item 20: Distinguish Between Implementing Interfaces and Overriding Virtual Functions Item 20: Distinguish Between Implementing Interfaces and Overriding Virtual Functions

4 Distinguish Between Implementing Interfaces and Overriding Virtual Functions Implementing an interface is very different from overriding a virtual function. Implementing an interface is very different from overriding a virtual function. Members declared in interfaces are not virtualat least Members declared in interfaces are not virtualat least Derived classes cannot override an interface member implemented in a base class. Interfaces can be explicitly implemented, which hides them from a class's public interface. They are different concepts with different uses. Derived classes cannot override an interface member implemented in a base class. Interfaces can be explicitly implemented, which hides them from a class's public interface. They are different concepts with different uses.

5 Distinguish Between Implementing Interfaces and Overriding Virtual Functions interface IMsg { void Message(); } public class MyClass : IMsg { public void Message() { Console.WriteLine("MyClass"); } public class MyDerivedClass : MyClass { public new void Message() { Console.WriteLine( "MyDerivedClass" ); } MyDerivedClass d = new MyDerivedClass( ); d.Message( ); // prints "MyDerivedClass". IMsg m = d as IMsg; m.Message( ); // prints "MyClass" The Message() method is part of MyClass's public interface. Message can also be accessed through the IMsg point that is part of the MyClass type.

6 Distinguish Between Implementing Interfaces and Overriding Virtual Functions But you often want to create interfaces, implement them in base classes, and modify the behavior in derived classes. But you often want to create interfaces, implement them in base classes, and modify the behavior in derived classes. You've got two options. If you do not have access to the base class, you can reimplement the interface in the derived class: You've got two options. If you do not have access to the base class, you can reimplement the interface in the derived class: public class MyDerivedClass : MyClass, IMsg { public new void Message() { Console.WriteLine( "MyDerivedClass" ); } MyDerivedClass d = new MyDerivedClass( ); d.Message( ); // prints "MyDerivedClass". IMsg m = d as IMsg; m.Message( ); // prints "MyDerivedClass"

7 MyDerivedClass d = new MyDerivedClass( ); d.Message( ); // prints "MyDerivedClass". IMsg m = d as IMsg; m.Message( ); // prints "MyDerivedClass" MyClass b = d; b.Message( ); // prints "MyClass" The base class version is still accessible through a reference to the base class: The base class version is still accessible through a reference to the base class: Distinguish Between Implementing Interfaces and Overriding Virtual Functions

8 public class MyClass : IMsg { public virtual void Message() { Console.WriteLine( "MyClass" ); } public class MyDerivedClass : MyClass { public override void Message() { Console.WriteLine( "MyDerivedClass" ); } Distinguish Between Implementing Interfaces and Overriding Virtual Functions Using the virtual function. Using the virtual function.

9 Summary Difference between “new” and “override”. Difference between “new” and “override”.

10 Item 21: Express Callbacks with Delegates Item 21: Express Callbacks with Delegates

11 Multicast Delegate public delegate void myDel(); static public void Main(string[] args) { myDel myDelA = new myDel(MethodA); myDel myDelB = new myDel(MethodB); myDel myMultiCast = (myDel)Delegate.Combine(myDelA, myDelB); myMultiCast.Invoke(); } static void MethodA() { Console.WriteLine(“Executing method A.”); } static void MethodB() { Console.WriteLine(“Executing method B.”); } Ref: http://www.codeproject.com/Articles/31400/NET-Multicast-Delegateshttp://www.codeproject.com/Articles/31400/NET-Multicast-Delegates

12 delegate void Del(string s); class TestClass { static void Hello(string s) { System.Console.WriteLine(" Hello, {0}!", s); } static void Goodbye(string s) { System.Console.WriteLine(" Goodbye, {0}!", s); } static void Main() { Del a, b, c, d; // Create the delegate object a that references // the method Hello: a = Hello; // Create the delegate object b that references // the method Goodbye: b = Goodbye; // The two delegates, a and b, are composed to form c: c = a + b; // Remove a from the composed delegate, leaving d, // which calls only the method Goodbye: d = c - a; System.Console.WriteLine("Invoking delegate a:"); a("A"); System.Console.WriteLine("Invoking delegate b:"); b("B"); System.Console.WriteLine("Invoking delegate c:"); c("C"); System.Console.WriteLine("Invoking delegate d:"); d("D"); } /* Output: Invoking delegate a: Hello, A! Invoking delegate b: Goodbye, B! Invoking delegate c: Hello, C! Goodbye, C! Invoking delegate d: Goodbye, D! */

13 Express Callbacks with Delegates Multicast delegates wrapall the functions that have been added to the delegate in a single function call. Two caveats apply to this construct: It is not safe in the face of exceptions, and the return value will be the return value of the last function invocation. Multicast delegates wrapall the functions that have been added to the delegate in a single function call. Two caveats apply to this construct: It is not safe in the face of exceptions, and the return value will be the return value of the last function invocation.

14 Express Callbacks with Delegates public delegate bool ContinueProcessing(); public void LengthyOperation( ContinueProcessing pred ) { foreach( ComplicatedClass cl in _container ) { cl.DoLengthyOperation(); // Check for user abort: if (false == pred()) return; } Check exception Check exception – In single or Multicast delegates is fine

15 Express Callbacks with Delegates A similar problem exists with return values A similar problem exists with return values – The value returned from invoking the delegate is the return value from the last function in the multicast chain – All other return values are ignored. The return from the CheckWithUser() predicate is ignored. ContinueProcessing cp = new ContinueProcessing ( CheckWithUser ); cp += new ContinueProcessing( CheckWithSystem ); c.LengthyOperation( cp );

16 Express Callbacks with Delegates public delegate bool ContinueProcessing(); public void LengthyOperation( ContinueProcessing pred ) { bool bContinue = true; foreach( ComplicatedClass cl in _container ) { cl.DoLengthyOperation(); foreach( ContinueProcessing pr in pred.GetInvocationList( )) bContinue &= pr(); if (false == bContinue) return; } You address both issues by invoking each delegate target yourself. Each delegate you create contains a list of delegates. To examine the chain yourself and call each one, iterate the invocation list yourself In this case, I've defined the semantics so that each delegate must be true for the iteration to continue.

17 Summary Reference to some of article from internet more than this book. Reference to some of article from internet more than this book.


Download ppt "Effective C# 50 Specific Way to Improve Your C# Item 20, 21 Sephiroth.Wang2012/08/01."

Similar presentations


Ads by Google