Essential C# Generics  Suggest an edit

Explicit type parameters

There are different cases where you must Explicitly specify the type parameters for a generic method. In both of the below cases, the compiler is not able to infer all of the type parameters from the specified method parameters.

One case is when there are no parameters:

public void SomeMethod<T, V>() 
{
   // No code for simplicity
}

SomeMethod(); // doesn't compile
SomeMethod<int, bool>(); // compiles

Second case is when one (or more) of the type parameters is not part of the method parameters:

public K SomeMethod<K, V>(V input)
{
    return default(K);
}

int num1 = SomeMethod(3); // doesn't compile
int num2 = SomeMethod<int>("3"); // doesn't compile
int num3 = SomeMethod<int, string>("3"); // compiles.

Table Of Contents
22 Enum
25 GUID
58 Generics
107 Stream
108 Timers
  ↑ ↓ to navigate     ↵ to select     Esc to close