question about Static method/class with where T : class,new(), what does it mean

Martin Wang 101 Reputation points

I am studying an example project and one class is like this:

public class ClassDB<T> where T : class,new(),
public static IEnumerable<T> ExecuteGetList(T whereModel)

And in another class(classA) , the code is

public static IEnumerable<T> GetList<T>(T whereModel) where T : class,new()
return ClassDB<T>.ExecuteGetList(whereModel);


I guess:
The code "class,new()," in classDB means in classA, we don't have to initialize classDB? because "class,new()," in classDB has already new it? and what does "class,new()" in classA mean?
Please let me know if I have explained clearly.
Thank you very much

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,215 questions
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,611 Reputation points

    Hi MartinWang-5107,
    >>where T : class,new(),
    Those are generic type constraints.
    where T : class
    Means that the type T must be a reference type (not a value type).
    where T : new()
    Means that the type T must have a parameter-less constructor.
    In order to understand it better, please refer to the links below:
    What does “where T : class, new()” mean?
    Constraints on type parameters (C# Programming Guide)
    Best Regards,
    Daniel Zhang

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful