What are the benefits of using generics in method signatures?

(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 {}
}

//OUTPUT:
//Type=TestGenericParameter.Customer, dummy=test, name of class=Customer
//Type=TestGenericParameter.Customer, dummy=test, name of class=Customer
+3
source share
7 answers

. Type, .

IoC , , .

public void Register<TAbstract, TConcrete>() where TConcrete : TAbstract
{
}

, TConcrete TAbstract. Type, .

+5

, , - generic . , ,

public T GetAs<T>(string name)

, . :

int value = GetAs<int>("foo");

public object GetAs(Type t, string name)

:

int value = (int)GetAs(typeof(int), "foo");
+4

- , .

, , :

void SomeMethod<T>(T x, T y) where T : IComparable<T> {
    Console.WriteLine("Result: {0} to {1} is {2}", x, y, x.CompareTo(y));
}

, :

SomeMethod(3, 4);         // instead of SomeMethod<int>(3, 4);
SomeMethod("one", "two"); // instead of SomeMethod<string>("one", "two");

generic type , :

var emptySequence = Enumerable.Empty<int>();
+4

, , typeof . . . .

, , . - , .

, . , , , , , . , , .

+3

, . , / () ().

:

public T RegisterType<T>(string name) 
{
    T obj = new T();
    obj.DoSomething();
    return obj;
}

, ,

public void DoSomething<T>(T obj) 
{
    //operate on obj
}
+3

If you did not use Generics, you would have to overload the method for each type that you want to support, or you need to accept this parameter as an object and execute the casting logic.

+2
source

I would say that the best reason is type safety, using the where keyword to ensure that the generic type has a particular type (or subclass / implementation). Using "typeof" will allow you to send anything through.

+1
source

Source: https://habr.com/ru/post/1704971/


All Articles