(Thanks to everyone for the answers, here is my edited example , in turn, another StackOverflow question about the principle of single responsibility.)
From PHP to C #, this syntax was intimidating:
container.RegisterType<Customer>("customer1");
until I realized that it expresses the same thing as:
container.RegisterType(typeof(Customer), "customer1");
as shown in the code below.
So, there is some reason why generics are used here (for example, in all Unity and most C # IoC containers), except that it is just a cleaner syntax, i.e. don't you need typeof () when sending type?
using System;
namespace TestGenericParameter
{
class Program
{
static void Main(string[] args)
{
Container container = new Container();
container.RegisterType<Customer>("test");
container.RegisterType(typeof(Customer), "test");
Console.ReadLine();
}
}
public class Container
{
public void RegisterType<T>(string dummy)
{
Console.WriteLine("Type={0}, dummy={1}, name of class={2}", typeof(T), dummy, typeof(T).Name);
}
public void RegisterType(Type T, string dummy)
{
Console.WriteLine("Type={0}, dummy={1}, name of class={2}", T, dummy, T.Name);
}
}
public class Customer {}
}
source
share