Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6799491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:51:17+00:00 2026-05-26T18:51:17+00:00

I’m writting a generalized method to use it in a special task at a

  • 0

I’m writting a generalized method to use it in a special task at a T4 template. The method should allow me to use specialized types from a general interface. I thought about the following signatures:

interface IGreatInterface {
    Object aMethodAlpha<U>(U parameter) where U : IAnInterface;
    Object aMethodBeta(IAnInterface parameter)
}

public class AnInterestingClass : IAnInterface{}

When I try to implement IGreatInterface the compiler flags an error for aMethodBeta() because I’ve made my T4 to write that method using a subtype of IAnInterface (i.e. I want to implement that method like this: Object aMethodBeta(AnInterestingClass parameter)).

Method aMethodAlpha<U>() can be used but is not as clean as I want because my T4 has to generate some extra code. I (perhaps wrongly)
propose that an implementation of that method, which has to be done by a T4, could be
Object aMethodAlpha<AnInterestingClass>(AnInterestingClass parameter).

I’m thinking that generic methods do not support contravariant types but I’m not sure; I suppose that It’s the way the compiler prevents the coder to use a specific type having a method not defined in the general type…

  1. Does a generic method have to use the exact type when being implemented?
  2. Is there any trick to change this behavior?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:51:18+00:00Added an answer on May 26, 2026 at 6:51 pm

    This question is quite confusing. Let me see if I can clarify it.

    When I try to implement IGreatInterface the compiler flags an error for aMethodBeta() because I’ve made that method using a subtype of IAnInterface I want to implement that method like this: Object aMethodBeta(AnInterestingClass parameter).

    That’s not legal. Simplifying somewhat:

    class Food {}
    class Fruit : Food {}
    class Meat : Food {}
    interface IEater
    {
        void Eat(Food food);
    }
    class Vegetarian : IEater
    {
        public void Eat(Fruit fruit);
    }
    

    Class Vegetarian does not fulfill the contract of IEater. You should be able to pass any Food to Eat, but a Vegetarian only accepts Fruit. C# does not support virtual method formal parameter covariance because that is not typesafe.

    Now, you might then say, how about this:

    interface IFruitEater
    {
        void Eat(Fruit fruit);
    }
    class Omnivore : IFruitEater
    {
        public void Eat(Food food);
    }
    

    Now we have got type safety; Omnivore can be used as an IFruitEater because an Omnivore can eat fruit, as well as any other food.

    Unfortunately, C# does not support virtual method formal parameter type contravariance even though doing so is in theory typesafe. Few languages do support this.

    Similarly, C# does not support virtual method return type variance either.

    I’m not sure if that actually answered your question or not. Can you clarify the question?

    UPDATE:

    What about:

    interface IEater
    {
        void Eat<T>(T t) where T : Food;
    }
    class Vegetarian : IEater
    {
        // I only want to eat fruit!
        public void Eat<Fruit>(Fruit food) { }
    }
    

    Nope, that’s not legal either. The contract of IEater is that you will provide a method Eat<T> that can take any T that is a Food. You cannot partially implement the contract, any more than you could do this:

    interface IAdder
    {
        int Add(int x, int y);
    }
    class Adder : IAdder
    {
        // I only know how to add two!
        public int Add(2, int y){ ... }
    }
    

    However, you can do this:

    interface IEater<T> where T : Food
    {
        void Eat(T t);
    }
    class Vegetarian : IEater<Fruit>
    {
        public void Eat(Fruit fruit) { }
    }
    

    That is perfectly legal. However, you cannot do:

    interface IEater<T> where T : Food
    {
        void Eat(T t);
    }
    class Omnivore : IEater<Fruit>
    {
        public void Eat(Food food) { }
    }
    

    Because again, C# does not support virtual method formal parameter contravariance or covariance.

    Note that C# does support parametric polymorphism covariance when doing so is known to be typesafe. For example, this is legal:

    IEnumerable<Fruit> fruit = whatever;
    IEnumerable<Food> food = fruit;
    

    A sequence of fruit may be used as a sequence of food. Or,

    IComparable<Fruit> fruitComparer = whatever;
    IComparable<Apples> appleComparer = fruitComparer;
    

    If you have something that can compare any two fruits then it can compare any two apples.

    However, this kind of covariance and contravariance is only legal when all of the following are true: (1) the variance is provably typesafe, (2) the author of the type added variance annotations indicating the desired co- and contra-variances, (3) the varying type arguments involved are all reference types, (4) the generic type is either a delegate or an interface.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.