none
What is the meaning of where T : class, new()

    Question

  • tell me what is the meaning of where T : class, new() ?

    public static T GetInstance<T>() where T : class, new() { return new T(); }

    Employee employee = GetInstance<Employee>();

    looking for answer. thanks

    Tuesday, April 11, 2017 12:43 PM

Answers

  • The 'where' clause is a constraint.

    It limits the data types that can be used as the generic parameter. See here.

    In your particular case T is limited by 'class' and 'new()'.

    The 'class' constraint means that T must be a reference type.

    The 'new()' constraint means that T must contain a public parameter-less constructor.

    • Proposed as answer by Ryan Software Tuesday, April 11, 2017 4:48 PM
    • Marked as answer by Mou_kolkata Wednesday, April 12, 2017 2:47 PM
    Tuesday, April 11, 2017 12:46 PM
  • Hi Mou_kolkata,

    Thank you for posting here.

    That is a constraint on the generic parameter T. It must be a class (reference type) and must have a public parameter-less default constructor.

    That means T could not be an int, float, double, DateTime or any other struct (value type).

    It could be a string, or any other custom reference type, as long as it has a default or parameter-less constructor.

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mou_kolkata Wednesday, April 12, 2017 2:47 PM
    Wednesday, April 12, 2017 2:43 AM
    Moderator

All replies

  • The 'where' clause is a constraint.

    It limits the data types that can be used as the generic parameter. See here.

    In your particular case T is limited by 'class' and 'new()'.

    The 'class' constraint means that T must be a reference type.

    The 'new()' constraint means that T must contain a public parameter-less constructor.

    • Proposed as answer by Ryan Software Tuesday, April 11, 2017 4:48 PM
    • Marked as answer by Mou_kolkata Wednesday, April 12, 2017 2:47 PM
    Tuesday, April 11, 2017 12:46 PM
  • Hi Mou_kolkata,

    Thank you for posting here.

    That is a constraint on the generic parameter T. It must be a class (reference type) and must have a public parameter-less default constructor.

    That means T could not be an int, float, double, DateTime or any other struct (value type).

    It could be a string, or any other custom reference type, as long as it has a default or parameter-less constructor.

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mou_kolkata Wednesday, April 12, 2017 2:47 PM
    Wednesday, April 12, 2017 2:43 AM
    Moderator