C# Generic Method Without Specifying Type

后端 未结 3 1835
無奈伤痛
無奈伤痛 2020-12-11 15:20

Ok so I\'m a Java guy starting to use C# and I was coding and started making a generic method and what I wrote runs and compiles but it goes against everything I know about

3条回答
  •  轮回少年
    2020-12-11 15:46

    No, it is inferred at compile time - the generic type parameter in the IEnumerable you supply is used, which is known at compile time. Generally put, everything concerning generics and type parameters is specified at compile time. If there is mismatch of any kind, the compiler will complain and your code won't compile.

    There are edge cases where you have to specify the types explicitly, these only occurs in rare circumstances with overloaded methods, sometimes with multiple combinations of type parameters.

提交回复
热议问题