where ( generic type constraint )

sj lee·2023년 12월 20일

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/where-generic-type-constraint

where

The where clause in a generic definition specifies constraints on the types that are used as arguments for type paramters 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 have, and must be placed after any declared base class or implemented interfaces.

For example, you can declare a generic class, AGenricClass such that the type parameter T implements the ICompareable<T> interface:

public class AGenericClass<T> where T : IComparable<T> { }

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. 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: Object, Array, and ValueType.
The following example shows the types that can now be specified as a base class:
where 절에는 기본 클래스 제약조건이 포함될 수도 있습니다. 기본 클래스 제약 조건에서는 해당 제네릭 형식에 대한 T로 사용할 형식이 지정된 클래스를 기본 클래스로 포함하거나 해당 기본 클래스임을 명시합니다. 기본 클래스 제약조건이 사용되는 경우 해당 형식매개변수에 대한 다른 제약 조건 앞에 나타나야 합니다. 일부 형식은 Object, Array 및 ValueType 기본 클래스 제약 조건으로 허용되지 않습니다. 다음 예제에서는 이제 기본 클래스로 지정할 수 있는 형식을 보여줍니다.

public class UsingEnum<T> where T : system.Enum { }
public class UsingDelegate<T> where T : System.Delegate { }
public class Multicaster<T> where T : System.MulticastDelegate { }

In a nullable context, the nullability of the base class type is enforced. If the base class is non-nullabe (for example Base), the type argument must be non-nullable. If the base class is nullable ( for exmple Base? ), the type argument may be either a nullable or non-nullable reference type. The compiler issues a warning if the type argument is a nullable reference type when the base class is non-nullable.
null 허용 컨텍스트에서는 기본 클래스 형식의 null 허용 여부가 적용됩니다. 기본 클래스가 null을 허용하지 않는 경우, 인수 형식도 null을 허용하지 않아야 합니다. 기본 클래스가 null을 허용하는 경우, T도 null을 허용하거나 허용하지 않는 참조일 수도 있습니다. 기본 클래스가 null을 허용하지 않는 경우 T 가 null 허용 참조 형식이면 컴파일러가 경고를 발생시킵니다.

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 exmpale shows both the class and struct constraints:

class MyClass<T, U>
	where T : class
    where U : struct
{  }

In a nullable context, the class constraint requires a type to be a non-nullable reference type. To allow nullable reference types, use the class? constraint, which allows both nullable and non-nullable reference types.

The where clause may include the notnull constraint. The notnull constrinat limits the type parameter to non-nullable types. The type mat be a value type or a non-nullable reference type. The notnull constraint is available for code compiled in a nullable enable context. Unlike other constraints, if a type argument violates the notnull constraint, the compiler generates a warning instead of an error. Warnings are only generated in a nullable enable context.

profile
Tomorrow will be better than Today

0개의 댓글