Advertisement
ivandrofly

C# Delegates Covariance Demo

Feb 19th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.93 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Delegates_Covariance
  4. {
  5.     class Program
  6.     {
  7.         public delegate Shape GetShape();
  8.  
  9.         public static Rectangle GetRectangle()
  10.         {
  11.             return new Rectangle();
  12.         }
  13.         public static Circle GetCircle()
  14.         {
  15.             return new Circle();
  16.         }
  17.         static void Main(string[] args)
  18.         {
  19.             GetShape getCircleMethod = GetCircle;
  20.             GetShape getRectangleMethod = GetRectangle;
  21.             Console.WriteLine(getCircleMethod());
  22.             Console.WriteLine(getRectangleMethod.Invoke());
  23.  
  24.         }
  25.     }
  26.  
  27.     public class Shape { }
  28.     public class Rectangle : Shape
  29.     {
  30.         public override string ToString()
  31.         {
  32.             return "I'm Circle";
  33.         }
  34.     }
  35.     public class Circle : Shape
  36.     {
  37.         public override string ToString()
  38.         {
  39.             return "I'm Rectangle";
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement