Problem in using C# generics with method overloading

Posted by Siva Chandran on Stack Overflow See other posts from Stack Overflow or by Siva Chandran
Published on 2010-12-21T17:49:04Z Indexed on 2010/12/21 17:54 UTC
Read the original article Hit count: 254

Filed under:
|

I am trying to call an overloaded method based on the generic type. I've been doing this in C++ without any pain. But I really don't understand why am not able to do this in C# with generics. Can anybody help me how can I achieve this in C# with generics?

class Test<T>
{
    public T Val;

    public void Do(T val)
    {
        Val = val;
        MainClass.Print(Val);
    }
}

class MainClass
{
    public static void Print(UInt16 val)
    {
        Console.WriteLine("UInt16: " + val.ToString());
    }

    public static void Print(UInt32 val)
    {
        Console.WriteLine("UInt32: " + val.ToString());
    }

    public static void Print(UInt64 val)
    {
        Console.WriteLine("UInt64: " + val.ToString());
    }

    public static void Main (string[] args)
    {   
        Test<UInt16> test = new Test<UInt16>();
        test.Do();

    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics