Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static T GetCar<T>() where T : ICar
- {
- T objCar = default(T);
- if (typeof(T) == typeof(SmallCar)) {
- objCar = new SmallCar("");
- } else if (typeof(T) == typeof(MediumCar)) {
- objCar = new MediumCar("");
- } else if (typeof(T) == typeof(BigCar)) {
- objCar = new BigCar("");
- }
- return objCar;
- }
- public ICar GetCar<T>()
- where T : ICar
- {
- ICar objCar = null;
- if (typeof(T) == typeof(SmallCar)) {
- objCar = new SmallCar();
- } else if (typeof(T) == typeof(MediumCar)) {
- objCar = new MediumCar();
- } else if (typeof(T) == typeof(BigCar)) {
- objCar = new BigCar();
- }
- return objCar;
- }
- public T GetCar<T>()
- where T : ICar
- {
- Object objCar = null;
- if (typeof(T) == typeof(SmallCar)) {
- objCar = new SmallCar();
- } else if (typeof(T) == typeof(MediumCar)) {
- objCar = new MediumCar();
- } else if (typeof(T) == typeof(BigCar)) {
- objCar = new BigCar();
- }
- return (T)objCar;
- }
- public T GetCar<T>()
- where T : ICar, new()
- {
- return new T();
- }
- public static T GetCar<T>() where T : ICar, new()
- {
- return new T();
- }
- public static T GetCar<T>()
- where T : ICar, new()
- {
- return new T();
- }
- class Program
- {
- static void Main(string[] args)
- {
- ICar smallCar = Helper.GetCar<SmallCar>("car 1");
- ICar mediumCar = Helper.GetCar<MediumCar>("car 2");
- Console.ReadLine();
- }
- }
- static class Helper
- {
- public static T GetCar<T>(string carName) where T : ICar
- {
- ICar objCar = default(T);
- if (typeof(T) == typeof(SmallCar))
- {
- objCar = new SmallCar { CarName = carName };
- }
- else if (typeof(T) == typeof(MediumCar))
- {
- objCar = new MediumCar { CarName = carName };
- }
- return (T)objCar;
- }
- }
- interface ICar
- {
- string CarName { get; set; }
- }
- class SmallCar : ICar
- {
- public string CarName { get; set ; }
- }
- class MediumCar : ICar
- {
- public string CarName { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement