none
Regarding generic function in c#

    Question

  • 1) just see the code. this syntax is not clear T GetInstance<T>

    public static T GetInstance<T>(Importance objType)
        {
            if (objType == Importance.Employee)
                return (T)Convert.ChangeType((new Employee()), typeof(T));
            else
                return (T)Convert.ChangeType((new Teacher()), typeof(T));
        }

    2) the first T means return type will be generic ? what is the meaning of second T like GetInstance<T> ?

    3) generic function can return void ? if yes then function signature would look like below one ?

    public static void GetInstance<T>(Importance objType)

    {

    }

    looking for guidance to understand the meaning of above function signature.

    code i got from http://stackoverflow.com/questions/40307029/c-sharp-how-to-return-instance-dynamically-by-generic

    thanks






    Thursday, April 13, 2017 1:38 PM

Answers

  • The T in the angled brackets (the second T) is the generic type parameter. The first T is the return type of the method, which is to say that the method returns the same type as the generic type parameter.

    So for example, if you used GetInstance<int>(...) then this would return an int. If you used GetInstance<string>(...) then this would return a string.

    e.g

    string s = GetInstance<string>(Importance.Employee);

    In the example code, the GetInstance<T> method will create either an Employee() or Teacher() class and then attempt to convert that into the type passed as a generic type parameter and return it (which implicitly assumes that the conversion makes sense - if you tried something silly like converting an Employee() class into an integer I wouldn't be surprised if you just got a conversion exception).

    And yes, generic methods may return void. Nothing wrong with that if that's what you want to do.


    • Edited by RJP1973 Thursday, April 13, 2017 1:49 PM
    • Proposed as answer by Ryan Software Friday, April 14, 2017 5:33 PM
    • Marked as answer by Mou_kolkata Monday, April 17, 2017 2:40 PM
    Thursday, April 13, 2017 1:49 PM
  • >>1) just see the code. this syntax is not clear T GetInstance<T>

    It is a generic method that returns an instance of the generic type T. So when you call the method for an Employee, en Employee object will be returned:

    Employee emp = GetInstance<Employee>(importance);

    >>2) the first T means return type will be generic ? what is the meaning of second T like GetInstance<T> ?

    It is the type argument of the method: https://msdn.microsoft.com/en-us/library/twcad0zb.aspx

    >>3) generic function can return void ?

    Yes, a generic method can return any type.

    >>if yes then function signature would look like below one ?

    Yes.

    Note that the GetInstance method you have posted isn't really a good candidate for a generic method. It should return a common base type that both the Employee and Teacher classes inherit from:

    public static Person GetInstance(Importance objType)
        {
            if (objType == Importance.Employee)
                return new Employee();
            else
                return new Teacher();
        }

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.


    Thursday, April 13, 2017 7:12 PM

All replies

  • The T in the angled brackets (the second T) is the generic type parameter. The first T is the return type of the method, which is to say that the method returns the same type as the generic type parameter.

    So for example, if you used GetInstance<int>(...) then this would return an int. If you used GetInstance<string>(...) then this would return a string.

    e.g

    string s = GetInstance<string>(Importance.Employee);

    In the example code, the GetInstance<T> method will create either an Employee() or Teacher() class and then attempt to convert that into the type passed as a generic type parameter and return it (which implicitly assumes that the conversion makes sense - if you tried something silly like converting an Employee() class into an integer I wouldn't be surprised if you just got a conversion exception).

    And yes, generic methods may return void. Nothing wrong with that if that's what you want to do.


    • Edited by RJP1973 Thursday, April 13, 2017 1:49 PM
    • Proposed as answer by Ryan Software Friday, April 14, 2017 5:33 PM
    • Marked as answer by Mou_kolkata Monday, April 17, 2017 2:40 PM
    Thursday, April 13, 2017 1:49 PM
  • >>1) just see the code. this syntax is not clear T GetInstance<T>

    It is a generic method that returns an instance of the generic type T. So when you call the method for an Employee, en Employee object will be returned:

    Employee emp = GetInstance<Employee>(importance);

    >>2) the first T means return type will be generic ? what is the meaning of second T like GetInstance<T> ?

    It is the type argument of the method: https://msdn.microsoft.com/en-us/library/twcad0zb.aspx

    >>3) generic function can return void ?

    Yes, a generic method can return any type.

    >>if yes then function signature would look like below one ?

    Yes.

    Note that the GetInstance method you have posted isn't really a good candidate for a generic method. It should return a common base type that both the Employee and Teacher classes inherit from:

    public static Person GetInstance(Importance objType)
        {
            if (objType == Importance.Employee)
                return new Employee();
            else
                return new Teacher();
        }

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.


    Thursday, April 13, 2017 7:12 PM