Problem in using C# generics with method overloading
- by Siva Chandran
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();
}
}