Generic contraints on method overloads

Is possible to do it, you need create something like enable_if from C++

public class ClassTag<V> where V : class { }

public class StructTag<V> where V : struct { }

public void Func<V>(V v, ClassTag<V> dummy = null) where V : class
{
    Console.Writeln("class");
}

public void Func<V>(V v, StructTag<V> dummy = null) where V : struct
{
    Console.Writeln("struct");
}

public void Func<V>(V? v, StructTag<V> dummy = null) where V : struct
{
    Console.Writeln("struct?");
}

static void Main()
{
    Func("A");
    Func(5);
    Func((int?)5);
}

It can be expanded to use any disjoint where to distinguish between overloads. Only drawback is that It cant be used inside another generic method:

public static void Z1<T>(T t) // where T : class
{
    Func(t); //error there
}

public static void Z2<T>(T t) where T : class
{
    Func(t); //ok 
}

edit But there is possibility of use dynamic in that case to work around this limitation:

public static void Z1<T>(T t)
{
     Func((dynamic)t); //if `T == int` it will call "struct" version
}

Only drawback is run time cost similar to call to Dictionary<,> index.


Jon Skeet has an answer to everything: click me

quote:

the declarations only differ in generic constraints, and constraints aren't part of the signature