Created
February 3, 2020 14:51
-
-
Save slorello89/b53d009db30fe828de94f6924a7c905b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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