프로그래밍/c#

제네릭 타입 제약 [ where T : 제약조건 ]

즐겁게 하하하 2022. 4. 14. 09:54
728x90
// T는 Value 타입
class MyClass<T> where T : struct 

// T는 Reference 타입
class MyClass<T> where T : class

// T는 디폴트 생성자를 가져야 함
class MyClass<T> where T : new() 

// T는 MyBase의 파생클래스이어야 함
class MyClass<T> where T : MyBase

// T는 IComparable 인터페이스를 가져야 함
class MyClass<T> where T : IComparable

// 좀 더 복잡한 제약들
class EmployeeList<T> where T : Employee, IEmployee, IComparable<T>, new() {}

// 복수 타입 파라미터 제약
class MyClass<T, U> where T : class  where U : struct {}
728x90