What does the new() do in the code below?
public class A<T> where T : B, new()
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is a constraint on the generic parameter of your class, meaning that any type that is passed as the generic type must have a parameterless constructor.
So,
would be a valid type. You could create a new instance of
A<C>.However,
would not satisfy the constraint, and you would not be allowed to create a new instance of
A<D>. If you also added a parameterless constructor to D, then it would again be valid.