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 */
This entry was posted in C#. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>