How do you provide a default type for generics?

后端 未结 6 1572
醉梦人生
醉梦人生 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:38

    The compiler can infer the type arguments on methods most of the time based on the type of the arguments passed:

    public void DoSomething(T test) {
    }
    

    can be called with

    DoSomething(4);                   // = DoSomething(4);
    DoSomething(MyEnum.SomeValue);    // = DoSomething(MyEnum.SomeValue);
    

    By the way, you can have non-generic overloads of a generic method too.

提交回复
热议问题