General methods in non-generic types

If you have a method like:

public static T DoSomething<T> ( params T [ ] input )

C # allows you to call it without specifying T, for example:

DoClass.DoSomething ( "1", "2", "3" );

Does the T compiler determine what was passed to it?

Is this a good deal (leave T in this case)?

+3
source share
4 answers

Yes, in most cases, the compiler can output a type parameter. (One, moreover, when your type is a lambda expression, if I remember correctly.)

It is generally considered good practice to omit common parameters when they can be inferred. In fact, I would say that it improves the readability of a certain amount (refining them is often quite redundant).

+3
source

, . " ".

, T . , - .

ReSharper, , . , , .

+1

, , T, , . , , , ( ).

+1

, . .

One more thing - if you are reading design guides for .net libraries, it is actually recommended that you write ALL of your common methods so that types can be inferred. Inexpressible general methods are considered more difficult to understand and should be avoided whenever possible in accordance with the design instructions.

+1
source

Source: https://habr.com/ru/post/1709214/


All Articles