Skip to content
Discussion options

You must be logged in to vote

Variance does apply to static members.

interface IFoo<in T, TThis> where TThis : IFoo<T, TThis>
{
    static abstract TThis Create(T val);
}

class C : IFoo<object, C>
{
    public static C Create(object val) => new C();
}

static class S
{
    static T Create<T>(string val) where T : IFoo<string, T> => T.Create(val);

    static C Create(string val) => Create<C>(val);//this compiles due to contravariance.
}

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
5 replies
@Richiban
Comment options

@FaustVX
Comment options

@Richiban
Comment options

@FaustVX
Comment options

@Richiban
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by Richiban
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants