Export (0) Print
Expand All
Expand Minimize

Compiler Error CS0309 

Visual Studio 2005

Error Message

The type 'typename' must be convertible to 'constraint type' in order to use it as parameter 'parameter' in the generic type or method 'generic'

When using a generic class or method, constraints placed on the generic type using the where keyword must be observed. A violation of the constraints results in this error. To fix the error, a different type should be passed in to the generic class or method, or the constraints should be changed.

The following sample generates CS0309 since B does not implement I and C<T> specifies in its constraints that T must implement I:

// CS0309.cs
using System;

interface I
{
}

class C<T> where T : I
{
}

class B
{
}

class CMain
{
    public static void Main()
    {
        Console.WriteLine(new C<B>()); // CS0309
    }
}

Community Additions

ADD
Show:
© 2014 Microsoft