static void Main(string[] args) { Calculate cal = Add; Console.WriteLine(cal(5, 5)); Calculate cal1 = Mutiply; Console.WriteLine(cal1(8, 8)); Console.ReadLine(); Del d = MethodOne; d += MethodTwo; d(); int invocationCount = d.GetInvocationList().GetLength(0); Console.WriteLine(invocationCount); CovarianceDel Cov = null; Cov = MethodStream; Cov = MethodString; Console.ReadLine(); }
static void Main(string[] args) { //Because MethodStream and MethodString inherit from Text write you can use //del with both methods. This is covariance; del = MethodStream; del = MethodString; }
// covariance with delegates (objects that inherit from the same thing) public static void Main(string[] args) { CovarianceDel del = MethodStream; del += MethodString; del(); Console.ReadKey(); }
public static void Test_CovarianceDel() { // you can assign both methods to del del = MethodStream; del(); del = MethodString; del(); Console.ReadLine(); }
static void Main(string[] args) { MaineCoon mc = new MaineCoon("Чейз"); CovarianceDel del = mc.GetMaineCoonCat; Cat cat = del(); Console.WriteLine(cat.Name); Console.WriteLine(cat.Breed); Console.ReadKey(); }
public void DoTest() { // both return class inherits from TextWriter // 共変性(covariance) del = MethodStream; del = MethodString; // parameter StreamWriter also inherits TextWriter // 反返性(contravariance) contraDel = DoSomething; Func <int, int, int> calc = (x, y) => x + y; Console.WriteLine($"Func<int, int, int> calc = {calc(1, 2)}"); }
static void Main(string[] args) { CovarianceDel d = GetInfo; Console.WriteLine("Covariance.... !!"); Console.Write(d().Age); //Covariance : Able to call method with return type is derived form which is defined by delegate Console.ReadLine(); Console.WriteLine("Contravariance.... !!"); Employee emp = new Employee() { Age = 23, CompanyName = "Acme Inc" }; ContraDel c2 = PrintEmp; c2(emp); Console.ReadKey(); }
public void Ex3_Covariance() { CovarianceDel del = MethodCovar; Base d = del(); // returns delegate type }