User Tools

Site Tools


notes:csharp:delegates

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
notes:csharp:delegates [2017/06/01]
leszek [Func and Action]
notes:csharp:delegates [2020/01/17] (current)
leszek [Generic Delegate Type Parameter Variance]
Line 152: Line 152:
  
  
-====Delegate Type Variance==== 
  
-  * **Covariance** specifies return type compatibility. It permits a method that has a return type that is more derived than that defined in the delegate. 
-  * **Contravariance** specifies parameter compatibility. It permits a method that has parameter types that are less derived than those in the delegate type. 
- 
-An example of covariance: ​ 
-<code csharp> 
-using System.IO; 
- 
-namespace ConsoleTest 
-{ 
-    // TextWriter is a base class for both StreamWriter and StringWriter. 
-    public delegate TextWriter MyDelegate();​ 
- 
-    class Program 
-    { 
-        // StreamWriter derives from TextWriter. 
-        public static StreamWriter Method1() { return null; } 
- 
-        // StringWriter derives from TextWriter. 
-        public static StringWriter Method2() { return null; } 
- 
-        static void Main() 
-        { 
-            MyDelegate d; 
- 
-            // Covariance makes the following assignments possible. 
-            d = Method1; // StreamWriter --> TextWriter 
-            d = Method2; // StringWriter --> TextWriter 
-        } 
-    } 
-} 
-</​code>​ 
- 
-An example of contravariance:​ 
- 
-A single method //​MyMethod//​ that accepts //​EventArgs//​ is invoked by two different delegates, one passing a //​MouseEventArgs//​ and the other passing a //​KeyEventArgs//​. 
- 
-<code csharp> 
-namespace ConsoleTest 
-{ 
-    public delegate void MouseDelegate(MouseEventArgs args); 
-    public delegate void KeyDelegate(KeyEventArgs args); 
- 
-    class Program 
-    { 
-        // EventArgs is less derived than MouseEventArgs and KeyEventArgs. 
-        public static void MyMethod(EventArgs args) { } 
- 
-        static void Main() 
-        { 
-            MouseDelegate d1; 
-            KeyDelegate d2; 
- 
-            // Both delegates can represent MyMethod. 
-            d1 = MyMethod; // EventArgs --> MouseEventArgs 
-            d2 = MyMethod; // EventArgs --> KeyEventArgs 
-        } 
-    } 
-} 
-</​code>​ 
  
  
Line 303: Line 243:
  
  
-====Generic Delegate Type Parameter Variance==== 
  
-  * Mark a type parameter used only on the return value as covariant (out). 
-  * Mark any type parameters used only on parameters as contravariant (in). 
- 
-<code csharp> 
-namespace ConsoleTest 
-{ 
-    // A delegate that supports covariance. 
-    public delegate T Delegate1<​out T>(); 
- 
-    // A delegate that supports contravariance. 
-    public delegate void Delegate2<​in T>(T arg); 
- 
-    class Program 
-    { 
-        // handlers 
-        public static T Method1<​T>​() { T n = default(T); return n; } 
-        public static void Method2<​T>​(T arg) { } 
- 
-        static void Main() 
-        { 
-            // A delegate that supports covariance. 
-            Delegate1<​string>​ d1 = Method1<​string>;​ 
- 
-            // Covariance enables this assignment. 
-            Delegate1<​object>​ o1 = d1; 
- 
-            // ... 
- 
-            // A delegate that supports contravariance. 
-            Delegate2<​object>​ d2 = Method2<​object>;​ 
- 
-            // Contravariance enables this assignment. 
-            Delegate2<​string>​ o2 = d2; 
-        } 
-    } 
-} 
-</​code>​ 
  
  
notes/csharp/delegates.1496331906.txt.gz ยท Last modified: 2017/06/01 by leszek