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 845727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:29:58+00:00 2026-05-15T06:29:58+00:00

Can anyone shed light on why contravariance does not work with C# value types?

  • 0

Can anyone shed light on why contravariance does not work with C# value types?

The below does not work

private delegate Asset AssetDelegate(int m);

internal string DoMe()
{
    AssetDelegate aw = new AssetDelegate(DelegateMethod);
    aw(32);
    return "Class1";
}

private static House DelegateMethod(object m)
{
    return null;
}
  • 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-15T06:29:58+00:00Added an answer on May 15, 2026 at 6:29 am

    The problem is that an int is not an object.

    An int can be boxed to an object. The resulting object (aka boxed int) is, of course, an object, but it’s not exactly an int anymore.

    Note that the “is” I’m using above is not the same as the C# operator is. My “is” means “is convertible to by implicit reference conversion“. This is the meaning of “is” used when we talk about covariance and contravariance.

    An int is implicit convertible to an object, but this is not a reference conversion. It has to be boxed.

    An House is implicit convertible to an Asset through a reference conversion. There’s no need to create or modify any objects.

    Consider the example below. Both variables house and asset are referencing the very same object. The variables integer and boxedInt, on the other hand, hold the same value, but they reference different things.

    House house = new House();
    Asset asset = house;
    
    int integer = 42;
    object boxedInt = integer;
    

    Boxing and Unboxing is not as simple as it may look like. It has many subtleties, and might affect your code in unexpected ways. Mixing boxing with covariance and contravariance is an easy way to make anyone dazzle.

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

Sidebar

Related Questions

Can anyone shed any light on why I'm unable to click and play a
Can anyone shed some light on this? From Bison's documentation, I didn't see anything
I'm not sure why the following doesn't work. StructureMap tells me there is no
After writing code that can be boiled down to the following: var size=-1; var
As of Java 1.5, you can pretty much interchange Integer with int in many
I am a newbie to spring mvc annotations. Which is the best way to
Ok I have been trying every which way to figure this out. I need
Right now, if I go to the index action of a model that I
The following code throws an exception which I have no idea what goes wrong
Hey everyone, I am working on parsing the PDF content stream to be able

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.