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

  • Home
  • SEARCH
  • 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 7533641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:47:01+00:00 2026-05-30T05:47:01+00:00

It is possible in C# to add variance annotation to type parameter, constrained to

  • 0

It is possible in C# to add variance annotation to type parameter, constrained to be value type:

interface IFoo<in T> where T : struct
{
  void Boo(T x);
}

Why is this allowed by compiler if variance annotation makes completely no sense in a such situation?

  • 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-30T05:47:03+00:00Added an answer on May 30, 2026 at 5:47 am

    Why this is allowed by compiler since variance annotation make completely no sense in a such situation?

    It’s allowed by the compiler because I never even considered that someone might try to do that when I added the variance rules to the C# 4.0 compiler.

    Compiler warnings and errors are features, and in order for a feature to be implemented, it has to, at a bare minimum, be thought of at some point before you ship your compiler. I failed to do so, and therefore never had the opportunity to even debate whether there ought to be a warning for such a situation.

    Now that you’ve brought it to my attention, the question is: should it be a feature? Should the compiler produce a warning (or error) for this case?

    That’s a judgment call. A number of things we’d consider are:

    • Is the code the sort of thing someone might type in thinking it does something sensible? One hopes not; one hopes that the developer who knows enough about the type system to make an interface variant also knows that variance only works on reference types. But maybe there are developers out there who might type this in thinking that it will work. It doesn’t seem beyond plausibility at least. It’s not clearly contrived.

    • Is the code clearly wrong? Yes, it probably is. It seems very unlikely that someone deliberately wants to write an interface that looks variant but in fact is not.

    And so on.

    I’ll have to give it more thought, but at first glance it looks like this actually might be a decent warning to add to the compiler. I’ll talk it over with the team and we’ll consider adding it to the Roslyn version.

    Thanks for the idea!

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

Sidebar

Related Questions

Possible Duplicate: Add a field and insert an increment value I have this table
Is it possible to add attributes at runtime or to change the value of
Is it possible to add a new property to an anonymous type instance after
Possible Duplicate: add an image to a html type input check box or radio
<tr> <td>type</td> <td>text</td> <td>text + <?php echo $var ?></td> </tr> is possible add any
Possible Duplicate: Add an attribute with static value with xslt I have an XML
Is it possible to add values to the windows registry of a type other
is it possible to add a custom event for a property value change?. I
Is it possible to add a custom tab to a project properties page in
Is it possible to add an image overlay to a google map that scales

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.