Share via


Did you know.... Generic Methods

Did you know that you can call a generic method with out supplying the type argument.  The C# compiler will fill in the type parameter for you at compile time since the language is strongly typed.

 
static void MyMethod<S>(S myParam)

{   

    Console.WriteLine(typeof(S).ToString());     

    Console.WriteLine(myParam.GetType().ToString()); 

}

static void Main(string[] args) {     
    int a1 = 5;     
    object a2 = 5;      

    MyMethod(a1);    
    MyMethod(a2);

}  /* //Outputs:  System.Int32 System.Int32 System.Object System.Int32 */

Comments