Share via


컴파일러 오류 CS0313

업데이트: 2007년 11월

오류 메시지

제네릭 형식 또는 메서드 'type2'에서 'type1' 형식을 형식 매개 변수 'parameter name'(으)로 사용할 수 없습니다. nullable 형식 'type1'이(가) 'type2' 제약 조건을 만족하지 않습니다. Nullable 형식은 인터페이스 제약 조건을 만족할 수 없습니다.
The type 'type1' cannot be used as type parameter 'parameter name' in the generic type or method 'type2'. The nullable type 'type1' does not satisfy the constraint of 'type2'. Nullable types cannot satisfy any interface constraints.

nullable 형식은 nullable이 아닌 해당 형식과 다릅니다. 다음 예제에서 Nullable<ImplStruct>가 BaseInterface를 구현하지 않기 때문에 ImplStruct는 BaseInterface 제약 조건을 만족하고 ImplStruct?는 해당 제약 조건을 만족하지 않습니다.

이 오류를 해결하려면

  • 예제에 나오는 코드를 사용하여 문제를 해결하려면 TestMethod에 대한 호출에서 표준 ImplStruct를 첫 번째 형식 인수로 지정합니다. 그런 다음 TestMethod를 수정하여 반환 문에 nullable 버전의 Implstruct를 만듭니다.

    return new Nullable<T>(t);
    

예제

다음 코드에서는 CS0313 오류가 발생하는 경우를 보여 줍니다.

// cs0313.cs
public interface BaseInterface { }
public struct ImplStruct : BaseInterface { }

public class TestClass
{
    public T? TestMethod<T, U>(T t) where T : struct, U
    {
        return t;
    }
}

public class NullableTest
{
    public static void Run()
    {

        TestClass tc = new TestClass();
        tc.TestMethod<ImplStruct?, BaseInterface>(new ImplStruct?()); // CS0313
    }
    public static void Main()
    { }
}

참고 항목

참조

nullable 형식(C# 프로그래밍 가이드)