즉, T
은 인터페이스 Core.ITwitterObject
을 구현해야합니다.
이 인터페이스를 구현하지 않는 일반 유형 T
을 전달하면 컴파일 타임 오류가 발생합니다.
이 조건은 컴파일러가 Core.ITwitterObject
에 선언 된 함수를 T
의 인스턴스에서 호출 할 수있게합니다.
자세한 내용은 documentation을 참조하십시오.
예 :이 이상의 장점이있다
interface IFoo { void Perform(); }
class FooList<T> where T : IFoo
{
List<T> foos;
...
void PerformForAll()
{
foreach (T foo in foos)
foo.Perform(); // this line compiles because the compiler knows
// that T implements IFoo
}
}
First
같은 방법이 더 타입 안전 때문에, 당신은 IFoo
에서 SomeRandomFooImpl
로 다운 캐스트 할 필요가 없습니다
interface IFoo { void Perform(); }
class FooList
{
List<IFoo> foos;
...
void PerformForAll()
{
foreach (IFoo foo in foos)
foo.Perform();
}
// added example method
IFoo First { get { return foos[0]; } }
}
관습 .
그래, 단지'어디 T :'줄을 제거하고 컴파일러가 무엇에 대해 불평하는지 확인하십시오 : ** [Constraints on Type Parameters] (http://msdn.microsoft.com/en-us/) 라이브러리/d5x73970 (V = vs.80)에서 .aspx) ** – sehe