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

Martin Wang 101 Reputation points
2020-12-03T07:49:24.393+00:00

Hi
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

C#
C#
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,197 questions
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,611 Reputation points
    2020-12-03T08:15:44.127+00:00

    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