Did you know.... Generic Methods

Published 16 December 07 11:14 AM | MattManela 

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 */

Filed under: ,

Comments

# Geek Lectures - Things geeks should know about » Blog Archive » Did you know…. Generic Methods said on December 16, 2007 2:40 PM:

PingBack from http://geeklectures.info/2007/12/16/did-you-know-generic-methods/

Anonymous comments are disabled

About MattManela

I am a software developer at Microsoft. My blog is http://blogs.msdn.com/matt
Page view tracker