using System; using System.Collections.Generic; using System.Text; class Program { static void Main(string[] args) { Console.WriteLine("Generic Interfaces"); BasicMath m = new BasicMath(); Console.WriteLine("1 + 1 = {0}", m.Add(1, 1)); Console.ReadLine(); } } public interface IBinaryOperations<T> { T Add(T arg1, T arg2); T Subtract(T arg1, T arg2); T Multiply(T arg1, T arg2); T Divide(T arg1, T arg2); } public class BasicMath : IBinaryOperations<int> { public int Add(int arg1, int arg2) { return arg1 + arg2; } public int Subtract(int arg1, int arg2) { return arg1 - arg2; } public int Multiply(int arg1, int arg2) { return arg1 * arg2; } public int Divide(int arg1, int arg2) { return arg1 / arg2; } }
Demonstrate a generic interface
using System;
public interface ISequence
T getNext();
void reset();
void setStart(T v);
}
class ByTwos
T start;
T val;
public delegate T IncByTwo(T v);
IncByTwo incr;
public ByTwos(IncByTwo incrMeth) {
start = default(T);
val = default(T);
incr = incrMeth;
}
public T getNext() {
val = incr(val);
return val;
}
public void reset() {
val = start;
}
public void setStart(T v) {
start = v;
val = start;
}
}
class GenIntfDemo {
static int intPlusTwo(int v) {
return v + 2;
}
static double doublePlusTwo(double v) {
return v + 2.0;
}
public static void Main() {
ByTwos
for(int i=0; i < 5; i++)
Console.Write(intBT.getNext() + " ");
Console.WriteLine();
ByTwos
dblBT.setStart(11.4);
for(int i=0; i < 5; i++) Console.Write(dblBT.getNext() + " "); } } [/csharp]
Generic Anonymous Methods
public delegate void ADelegate<T>(T tvalue); public class MyClass{ public void MethodA() { ADelegate<int> del=delegate(int var) { }; } } public class YClass<T>{ public delegate void BDelegate(T tValue); public void MethodA() { BDelegate del=delegate(T tValue) { }; } }
a generic delegate being initialized with a normal method
using System; public delegate void DelegateClass<T>(T data); public class Starter { public static void Main() { DelegateClass<string> del = MethodA; del("data"); } public static void MethodA(string data) { Console.WriteLine("MethodA ({0})", data); } }
Generics and Delegates
using System; public delegate void DelegateClass(int data); public class Starter { public static void Main() { DelegateClass del1 = MethodA<int>; del1(5); DelegateClass del2 = MethodA; del1(10); // inferred } public static void MethodA<T>(T data) { Console.WriteLine("MethodA ({0})", data); } }
Generic Delegate and non-generic delegate
using System; using System.Collections.Generic; using System.Text; public delegate void MyGenericDelegate<T>(T arg); public delegate void MyDelegate(object arg); class Program { static void Main(string[] args) { MyDelegate d = new MyDelegate(MyTarget); d("More string data"); MyDelegate d2 = new MyDelegate(MyTarget); d2(9); MyGenericDelegate<string> strTarget = new MyGenericDelegate<string>(StringTarget); strTarget("Some string data"); MyGenericDelegate<int> intTarget = IntTarget; intTarget(9); Console.ReadLine(); } static void MyTarget(object arg) { if (arg is int) { int i = (int)arg; Console.WriteLine("++arg is: {0}", ++i); } if (arg is string) { string s = (string)arg; Console.WriteLine("arg in uppercase is: {0}", s.ToUpper()); } } static void StringTarget(string arg) { Console.WriteLine("arg in uppercase is: {0}", arg.ToUpper()); } static void IntTarget(int arg) { Console.WriteLine("++arg is: {0}", ++arg); } }
Convert event to use generic delegate
/* Example code from Chapter 18 - Generics C# 2.0: The Complete Reference, Second Edition by Herbert Schildt McGraw-Hill/Osborne ? 2006 */ using System; class MyEventArgs : EventArgs { public int eventnum; } delegate void MyEventHandler<T, V>(T source, V args); class MyEvent { static int count = 0; public event MyEventHandler<MyEvent, MyEventArgs> SomeEvent; public void OnSomeEvent() { MyEventArgs arg = new MyEventArgs(); if(SomeEvent != null) { arg.eventnum = count++; SomeEvent(this, arg); } } } class X { public void handler<T, V>(T source, V arg) where V : MyEventArgs { Console.WriteLine("Event " + arg.eventnum + " received by an X object."); Console.WriteLine("Source is " + source); Console.WriteLine(); } } class Y { public void handler<T,V>(T source, V arg) where V : MyEventArgs { Console.WriteLine("Event " + arg.eventnum + " received by a Y object."); Console.WriteLine("Source is " + source); Console.WriteLine(); } } class Test { public static void Main() { X ob1 = new X(); Y ob2 = new Y(); MyEvent evt = new MyEvent(); evt.SomeEvent += ob1.handler; evt.SomeEvent += ob2.handler; evt.OnSomeEvent(); evt.OnSomeEvent(); } }