I come from a C++ background and I am relatively new to C#.
Currently, I am trying to write two print functions, the first of which accepts a generic array parameter (and prints the items of the array to the command line) and a second one which accepts a generic primitive parameter (and invokes its ToString() method). Here's my code:
using System;
namespace OverloadResolution
{
class Program
{
static void Main(string[] args)
{
string[] foo = new string[] { "A", "B", "C" };
Extensions.PrintMe(foo);
Extensions.PrintMe("Hello world");
Console.ReadLine();
}
}
public static class Extensions
{
public static void PrintMe<T>(T[] elm)
{
Console.WriteLine("PrintMe<T>(T[] elm)");
Console.WriteLine(string.Join("", elm));
}
public static void PrintMe<T>(T elm)
{
Console.WriteLine("PrintMe<T>(T elm)");
Console.WriteLine(elm.ToString());
}
}
}
Everything works as expected and the correct overloads are chosen. However, if I change my code as follows:
using System;
namespace OverloadResolution
{
class Program
{
static void Main(string[] args)
{
string[] foo = new string[] { "A", "B", "C" };
Extensions.PrintMe2(foo); // <<< Note the PrintMe2 function
Extensions.PrintMe2("Hello world"); // <<< Note the PrintMe2 function
Console.ReadLine();
}
}
public static class Extensions
{
public static void PrintMe2<T>(T elm)
{
PrintMe(elm);
}
public static void PrintMe<T>(T[] elm)
{
Console.WriteLine("PrintMe<T>(T[] elm)");
Console.WriteLine(string.Join("", elm));
}
public static void PrintMe<T>(T elm)
{
Console.WriteLine("PrintMe<T>(T elm)");
Console.WriteLine(elm.ToString());
}
}
}
the type information seems to get lost when calling the PrintMe2 function because, internally, the second PrintMe function is invoked in both cases. Are there any special rules which apply in this case? Or am I missing something? I use C#7.0 and .NET framework 4.7. I should note that I already learned that the use of C# generics is quite limited as compared to C++ templates...