Assignment Compatibility Covariance With Delegates C# with Example



Assignment Compatibility Covariance With Delegates C# with Example

 using System;

namespace CSharpGenerics
{
    delegate T MyDelegate();			//Generic Delegate

    class BaseClass
    {
        public void Display()
        {
            Console.WriteLine("BaseClass Display Method.");
        }
    }

    class DerivedClass : BaseClass { }

    class Program
    {

        static DerivedClass CreateDerivedObject()	//Delegate Matching Method
        {
            return new DerivedClass();
        }

        static void Main(string[] args)
        {
            //Create Derived Object via Delegate
            MyDelegate derivedObjectCreator = CreateDerivedObject;

            //Assign Derived Object�s reference to Base Reference via Delegate 
            MyDelegate baseObjectCreator = derivedObjectCreator; 	
            baseObjectCreator().Display();
        }
    }
}
 

0 Comment's

Comment Form

Submit Comment