Skip to content

Instantly share code, notes, and snippets.

@slorello89
Created February 3, 2020 14:51
Show Gist options
  • Save slorello89/b53d009db30fe828de94f6924a7c905b to your computer and use it in GitHub Desktop.
Save slorello89/b53d009db30fe828de94f6924a7c905b to your computer and use it in GitHub Desktop.
using System;
namespace TestSwitchExpressions
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var resposne = CarMakeFactory(CarMake.Ford);
Console.WriteLine(resposne);
}
public enum CarMake
{
Chevrolet,
Ford,
Dodge,
Tesla
}
public static ICarService CarMakeFactory(CarMake make)
{
return make switch
{
CarMake.Chevrolet => new ChevroletService(),
CarMake.Ford => new FordService(),
CarMake.Dodge => new DodgeService(),
CarMake.Tesla => new TeslaService(),
_ => throw new ArgumentException(message: "Invalid value for CarMake", paramName: nameof(make))
};
}
public interface ICarService { }
public class ChevroletService : ICarService { }
public class FordService : ICarService { }
public class DodgeService : ICarService { }
public class TeslaService : ICarService { }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment