0% found this document useful (0 votes)
151 views

Generic Constraint

The document discusses different type constraints that can be applied to generic types in C#. These constraints include: - where T: struct - T must be a value type - where T: class - T must be a reference type - where T: new() - T must have a parameterless constructor - where T: NameOfBaseClass - T must inherit from the specified base class - where T: NameOfInterface - T must implement the specified interface It provides examples of applying single and multiple constraints to generic types, and notes that multiple type parameters can each have unique constraints.

Uploaded by

Simon
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views

Generic Constraint

The document discusses different type constraints that can be applied to generic types in C#. These constraints include: - where T: struct - T must be a value type - where T: class - T must be a reference type - where T: new() - T must have a parameterless constructor - where T: NameOfBaseClass - T must inherit from the specified base class - where T: NameOfInterface - T must implement the specified interface It provides examples of applying single and multiple constraints to generic types, and notes that multiple type parameters can each have unique constraints.

Uploaded by

Simon
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 2

Generic Constraint Meaning in Life

where T : struct The type parameter <T> must have


System.ValueType in its chain of inheritance.

where T : class The type parameter <T> must not have


System.ValueType in its chain of inheritance
(e.g., <T> must be a reference
type).

where T : new() The type parameter <T>must have a


default constructor. This is
very helpful if your generic type
must create an instance of the
type parameter, as you cannot
assume the format of custom
constructors. Note that this
constraint must be listed last on a
multiconstrained type.

where T : NameOfBaseClass The type parameter <T> must be


derived from the class specified
by NameOfBaseClass.
where T : NameOfInterface The type parameter <T> must
implement the interface specified
by NameOfInterface.Multiple
interfaces can be separated as a
comma-delimited list.

When constraints are applied using the where keyword, the constraint
list is placed after the
generic type’s base class and interface list. By way of a few concrete
examples, consider the following
constraints of a generic class named MyGenericClass:

// MyGenericClass derives from Object, while


// contained items must have a default ctor.

public class MyGenericClass<T> where T : new()

{...}
// MyGenericClass derives from Object, while
// contained items must be a class implementing
IDrawable
// and support a default ctor.

public class MyGenericClass<T> where T : class,

IDrawable, new()

{...}

// MyGenericClass derives from MyBase and


implements ISomeInterface,
// while the contained items must be structures.

public class MyGenericClass<T> : MyBase,

ISomeInterface where T : struct

{...}

On a related note, if you are building a generic type that specifies


multiple type parameters,
you can specify a unique set of constraints for each:
// <K> must have a default ctor, while <T> must
// implement the generic IComparable interface.

public class MyGenericClass<K, T> where K : new()

where T : IComparable<T>

{...}

You might also like