How do you provide a default type for generics?

后端 未结 6 1524
醉梦人生
醉梦人生 2020-12-30 18:58

I have a class that currently has several methods that take integer parameters. These integers map to operations that the application can perform. I\'d like to make the clas

6条回答
  •  粉色の甜心
    2020-12-30 19:43

    Keep your original version (non-generic version) and create a generic version of it.

    Then call the generic version from your non-generic version.

    void Main()
    {
    DoSomething(2);
    DoSomething(EnumValue);
    
    }
    
    public void DoSomething(int test) {
    DoSomething(test);
    }
    
    // Define other methods and classes here
    public void DoSomething(T test) {
    Console.WriteLine(test);
    }
    

提交回复
热议问题