Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/csharp/language-reference/compiler-messages/cs0702.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ caps.latest.revision: 10
author: "BillWagner"
ms.author: "wiwagn"
---
# Compiler Error CS0702
# Compiler error CS0702
Constraint cannot be special class 'identifier'

The following types may not be used as constraints: `System.Object,``System.Array`, `System.Delegate`, `System.Enum`, or `System.ValueType`.
The following types may not be used as constraints: <xref:System.Object>, <xref:System.Array>, or <xref:System.ValueType>.

## Example
The following sample generates CS0702:
Expand All @@ -29,5 +29,5 @@ class C<T> where T : System.Array // CS0702
}
```

## See Also
[Constraints on Type Parameters](../../../csharp/programming-guide/generics/constraints-on-type-parameters.md)
## See also
[Constraints on Type Parameters](../../programming-guide/generics/constraints-on-type-parameters.md)

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "where (generic type constraint) (C# Reference)"
ms.date: 07/20/2015
ms.date: 04/12/2018
ms.prod: .net
ms.technology:
- "devlang-csharp"
Expand All @@ -10,57 +10,62 @@ f1_keywords:
- "whereconstraint_CSharpKeyword"
helpviewer_keywords:
- "where (generic type constraint) [C#]"
ms.assetid: d7aa871b-0714-416a-bab2-96f87ada4310
caps.latest.revision: 10
author: "BillWagner"
ms.author: "wiwagn"
---
# where (generic type constraint) (C# Reference)
In a generic type definition, the `where` clause is used to specify constraints on the types that can be used as arguments for a type parameter defined in a generic declaration. For example, you can declare a generic class, `MyGenericClass`, such that the type parameter `T` implements the <xref:System.IComparable%601> interface:

```csharp
public class MyGenericClass<T> where T:IComparable { }
```


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. They declare capabilities that the type argument must possess.

For example, you can declare a generic class, `MyGenericClass`, such that the type parameter `T` implements the <xref:System.IComparable%601> interface:

[!code-csharp[using an interface constraint](../../../../samples/snippets/csharp/keywords/GenericWhereConstraints.cs#1)]

> [!NOTE]
> For more information on the where clause in a query expression, see [where clause](../../../csharp/language-reference/keywords/where-clause.md).

In addition to interface constraints, a `where` clause can include a base class constraint, which states that a type must have the specified class as a base class (or be that class itself) in order to be used as a type argument for that generic type. If such a constraint is used, it must appear before any other constraints on that type parameter.

[!code-csharp[csrefKeywordsContextual#6](../../../csharp/language-reference/keywords/codesnippet/CSharp/where-generic-type-constraint_1.cs)]

The `where` clause may also include a constructor constraint. It is possible to create an instance of a type parameter using the new operator; however, in order to do so the type parameter must be constrained by the constructor constraint, `new()`. The [new() Constraint](../../../csharp/language-reference/keywords/new-constraint.md) lets the compiler know that any type argument supplied must have an accessible parameterless--or default-- constructor. For example:

[!code-csharp[csrefKeywordsContextual#7](../../../csharp/language-reference/keywords/codesnippet/CSharp/where-generic-type-constraint_2.cs)]

The `new()` constraint appears last in the `where` clause.

With multiple type parameters, use one `where` clause for each type parameter, for example:

[!code-csharp[csrefKeywordsContextual#8](../../../csharp/language-reference/keywords/codesnippet/CSharp/where-generic-type-constraint_3.cs)]

You can also attach constraints to type parameters of generic methods, like this:

```csharp
public bool MyMethod<T>(T t) where T : IMyInterface { }
```

Notice that the syntax to describe type parameter constraints on delegates is the same as that of methods:

```csharp
delegate T MyDelegate<T>() where T : new()
```

For information on generic delegates, see [Generic Delegates](../../../csharp/programming-guide/generics/generic-delegates.md).

For details on the syntax and use of constraints, see [Constraints on Type Parameters](../../../csharp/programming-guide/generics/constraints-on-type-parameters.md).

## C# Language Specification
[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)]

## See Also
> For more information on the where clause in a query expression, see [where clause](where-clause.md).

The `where` clause can also include a base class constraint. The base class constraint states that a type to be used as a type argument for that generic type has the specified class as a base class (or is that base class) to be used as a type argument for that generic type. If the base class constraint is used, it must appear before any other constraints on that type parameter. Some types are disallowed as a base class constraint: <xref:System.Object>, <xref:System.Array>, and <xref:System.ValueType>. Prior to C# 7.3, <xref:System.Enum>, <xref:System.Delegate>, and <xref:System.MulticastDelegate> were also disallowed as base class constraints. The following example shows the types that can now be specified as a base class:

[!code-csharp[using an interface constraint](../../../../samples/snippets/csharp/keywords/GenericWhereConstraints.cs#2)]

The `where` clause can specify that the type is a `class` or a `struct`. The `struct` constraint removes the need to specify a base class constraint of `System.ValueType`. The `System.ValueType` type may not be used as a base class constraint. The following example shows both the `class` and `struct` constraints:

[!code-csharp[using the class and struct constraints](../../../../samples/snippets/csharp/keywords/GenericWhereConstraints.cs#3)]

The `where` clause may also include an `unmanaged` constraint. The `unmanaged` constraint limits the type parameter to types known as **unmanaged types**. An **unmanaged type** is a type that isn't a reference type and doesn't contain reference type fields at any level of nesting. The `unmanaged` constraint makes it easier to write low-level interop code in C#. This constraint enables reusable routines across all unmanaged types. The `unmanaged` constraint can't be combined with the `class` or `struct` constraint. The `unmanaged` constraint enforces that the type must be a `struct`:

[!code-csharp[using the unmanaged constraint](../../../../samples/snippets/csharp/keywords/GenericWhereConstraints.cs#4)]

The `where` clause may also include a constructor constraint, `new()`. That constraint makes it possible to create an instance of a type parameter using the `new` operator. The [new() Constraint](new-constraint.md) lets the compiler know that any type argument supplied must have an accessible parameterless--or default-- constructor. For example:

[!code-csharp[using the new constraint](../../../../samples/snippets/csharp/keywords/GenericWhereConstraints.cs#5)]

The `new()` constraint appears last in the `where` clause. The `new()` constraint can't be combined with the `struct` or `unmanaged` constraints. All types satisfying those constraints must have an accessible parameterless constructor, making the `new()` constraint redundant.

With multiple type parameters, use one `where` clause for each type parameter, for example:

[!code-csharp[using multiple where constraints](../../../../samples/snippets/csharp/keywords/GenericWhereConstraints.cs#6)]

You can also attach constraints to type parameters of generic methods, as shown in the following example:

[!code-csharp[where constraints with generic methods](../../../../samples/snippets/csharp/keywords/GenericWhereConstraints.cs#7)]

Notice that the syntax to describe type parameter constraints on delegates is the same as that of methods:

[!code-csharp[where constraints with generic methods](../../../../samples/snippets/csharp/keywords/GenericWhereConstraints.cs#8)]

For information on generic delegates, see [Generic Delegates](../../../csharp/programming-guide/generics/generic-delegates.md).

For details on the syntax and use of constraints, see [Constraints on Type Parameters](../../../csharp/programming-guide/generics/constraints-on-type-parameters.md).

## C# language specification

[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)]

## See also

[C# Reference](../../../csharp/language-reference/index.md)
[C# Programming Guide](../../../csharp/programming-guide/index.md)
[Introduction to Generics](../../../csharp/programming-guide/generics/introduction-to-generics.md)
[new Constraint](../../../csharp/language-reference/keywords/new-constraint.md)
[Constraints on Type Parameters](../../../csharp/programming-guide/generics/constraints-on-type-parameters.md)
[Constraints on Type Parameters](../../../csharp/programming-guide/generics/constraints-on-type-parameters.md)

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading