using System.Collections.Generic;
using System.Threading.Tasks;
public async static Task Main()
Service test = new Service();
TestBase category3 = await test.GetItemAsync3(new Category(""), 0);
TestBase product3 = await test.GetItemAsync3(new Product(""), 0);
Console.WriteLine($@"category 3: {category3}");
Console.WriteLine($@"product 3: {product3}");
TestBase category = await test.GetItemAsync<Category>(0);
TestBase product = await test.GetItemAsync<Product>(0);
Console.WriteLine($@"category: {category}");
Console.WriteLine($@"product: {product}");
object category2 = await test.GetItemAsync2(ObjectType.Category, 2);
object product2 = await test.GetItemAsync2(ObjectType.Product, 2);
Console.WriteLine($@"category: {category2}");
Console.WriteLine($@"product: {product2}");
private static readonly IDictionary<Type, int> types = new Dictionary<Type, int>() {
public async Task<object> GetItemAsync2(ObjectType t, int number, CancellationToken ct = default)
object toReturn = t switch
ObjectType.Category => await this.GetCategoryAsync(number, ct),
ObjectType.Product => await this.GetProductAsync(number, ct),
_ => throw new NotSupportedException()
public async Task<TestBase> GetItemAsync<T>(int number, CancellationToken ct = default) where T: TestBase
TestBase t = types[typeof(T)] switch
1 => await this.GetCategoryAsync(number, ct),
2 => await this.GetProductAsync(number, ct),
_ => throw new NotSupportedException()
public async Task<TestBase> GetItemAsync3(TestBase tBase, int number, CancellationToken ct = default)
TestBase t = tBase switch
Category => await this.GetCategoryAsync(number, ct),
Product => await this.GetProductAsync(number, ct),
_ => throw new NotSupportedException()
private Category[] categories = {
new Category("second Category"),
public Task<Category> GetCategoryAsync(int number, CancellationToken ct) {
return Task.FromResult(this.categories[number]);
private Product[] products = {
new Product("test product"),
new Product("second product"),
new Product("new feature")
public Task<Product> GetProductAsync(int number, CancellationToken ct) {
return Task.FromResult(this.products[number]);
public class Category: TestBase {
public string name => "category";
public string categoryValue { get; set; }
public Category (string categoryValue) {
this.categoryValue = categoryValue;
public override string ToString() {
return $@"{this.name}, {this.categoryValue}";
public class Product: TestBase {
public string name => "product";
public string productValue { get; set; }
public Product (string productValue) {
this.productValue = productValue;
public override string ToString() {
return $@"{this.name}, {this.productValue}";