Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"where" keyword in class declaration in c sharp

Tags:

Could anyone help me with the line where TEntity : class, IEntity, new() in the following class declaration.

public abstract class BaseEntityManager<TEntity>
        where TEntity : class, IEntity, new()
like image 405
user193442 Avatar asked Oct 21 '09 02:10

user193442


People also ask

What is where keyword in C#?

The where clause in a generic definition specifies constraints on the types that are used as arguments for type parameters in a generic type, method, delegate, or local function. Constraints can specify interfaces, base classes, or require a generic type to be a reference, value, or unmanaged type.

What is class declaration in C#?

Declaring Classes Classes are declared by using the class keyword followed by a unique identifier, as shown in the following example: C# Copy. //[access modifier] - [class] - [identifier] public class Customer { // Fields, properties, methods and events go here... } The class keyword is preceded by the access level.

Is class A keyword in C#?

A class defines the kinds of data and the functionality their objects will have. A class enables you to create your custom types by grouping variables of other types, methods, and events. In C#, a class can be defined by using the class keyword.

What does where T class mean in C#?

It's a type constraint on T , specifying that it must be a class. The where clause can be used to specify other type constraints, e.g.: where T : struct // T must be a struct where T : new() // T must have a default parameterless constructor where T : IComparable // T must implement the IComparable interface.


1 Answers

where TEntity : ... applies constraints to the generic parameter TEntity. In this case, the constraints are:

class: The argument to TEntity must be a reference type
IEntity: The argument must be or implement the IEntity interface
new(): The argument must have a public parameterless constructor

From http://msdn.microsoft.com/en-us/library/d5x73970.aspx

like image 82
user200783 Avatar answered Oct 13 '22 22:10

user200783