SHARE
TWEET

Untitled

a guest Jun 25th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static T GetCar<T>() where T : ICar
  2. {
  3.     T objCar = default(T);
  4.  
  5.     if (typeof(T) == typeof(SmallCar)) {
  6.         objCar = new SmallCar("");
  7.     } else if (typeof(T) == typeof(MediumCar)) {
  8.         objCar = new MediumCar("");
  9.     } else if (typeof(T) == typeof(BigCar)) {
  10.         objCar = new BigCar("");
  11.     }
  12.  
  13.     return objCar;
  14. }
  15.      
  16. public ICar GetCar<T>()
  17.     where T : ICar
  18. {
  19.     ICar objCar = null;
  20.  
  21.     if (typeof(T) == typeof(SmallCar)) {
  22.         objCar = new SmallCar();
  23.     } else if (typeof(T) == typeof(MediumCar)) {
  24.         objCar = new MediumCar();
  25.     } else if (typeof(T) == typeof(BigCar)) {
  26.         objCar = new BigCar();
  27.     }
  28.  
  29.     return objCar;
  30. }
  31.      
  32. public T GetCar<T>()
  33.     where T : ICar
  34. {
  35.     Object objCar = null;
  36.  
  37.     if (typeof(T) == typeof(SmallCar)) {
  38.         objCar = new SmallCar();
  39.     } else if (typeof(T) == typeof(MediumCar)) {
  40.         objCar = new MediumCar();
  41.     } else if (typeof(T) == typeof(BigCar)) {
  42.         objCar = new BigCar();
  43.     }
  44.  
  45.     return (T)objCar;
  46. }
  47.      
  48. public T GetCar<T>()
  49.     where T : ICar, new()
  50. {
  51.     return new T();
  52. }
  53.      
  54. public static T GetCar<T>() where T : ICar, new()
  55. {
  56.     return new T();
  57. }
  58.      
  59. public static T GetCar<T>()
  60.     where T : ICar, new()
  61. {
  62.     return new T();
  63. }
  64.      
  65. class Program
  66. {
  67.     static void Main(string[] args)
  68.     {
  69.         ICar smallCar = Helper.GetCar<SmallCar>("car 1");
  70.         ICar mediumCar = Helper.GetCar<MediumCar>("car 2");
  71.  
  72.         Console.ReadLine();
  73.     }
  74. }
  75.  
  76. static class Helper
  77. {
  78.     public static T GetCar<T>(string carName) where T : ICar
  79.     {
  80.         ICar objCar = default(T);
  81.  
  82.         if (typeof(T) == typeof(SmallCar))
  83.         {
  84.             objCar = new SmallCar { CarName = carName };
  85.         }
  86.         else if (typeof(T) == typeof(MediumCar))
  87.         {
  88.             objCar = new MediumCar { CarName = carName };
  89.         }
  90.  
  91.         return (T)objCar;
  92.     }
  93.  
  94. }
  95.  
  96. interface ICar
  97. {
  98.     string CarName { get; set; }
  99. }
  100.  
  101. class SmallCar : ICar
  102. {
  103.     public string CarName { get; set ; }
  104. }
  105.  
  106. class MediumCar : ICar
  107. {
  108.     public string CarName { get; set; }
  109. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top