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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:01:00+00:00 2026-05-31T08:01:00+00:00

Why is the last line not allowed? IEnumerable<double> doubleenumerable = new List<double> { 1,

  • 0

Why is the last line not allowed?

IEnumerable<double> doubleenumerable = new List<double> { 1, 2 };
IEnumerable<string> stringenumerable = new List<string> { "a", "b" };
IEnumerable<object> objects1 = stringenumerable; // OK
IEnumerable<object> objects2 = doubleenumerable; // Not allowed

Is this because double is a value type that doesn’t derive from object, hence the covariance doesn’t work?

Does that mean that there is no way to make this work:

public interface IMyInterface<out T>
{
    string Method(); 
}

public class MyClass<U> : IMyInterface<U>
{
    public string Method()
    {
        return "test";
    }
}

public class Test
{
    public static object test2() 
    {
        IMyInterface<double> a = new MyClass<double>();
        IMyInterface<object> b = a; // Invalid cast!
        return b.Method();
    }
}

And that I need to write my very own IMyInterface<T>.Cast<U>() to do that?

  • 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-31T08:01:01+00:00Added an answer on May 31, 2026 at 8:01 am

    Why is the last line not allowed?

    Because double is a value type and object is a reference type; covariance only works when both types are reference types.

    Is this because double is a value type that doesn’t derive from object, hence the covariance doesn’t work?

    No. Double does derive from object. All value types derive from object.

    Now the question you should have asked:

    Why does covariance not work to convert IEnumerable<double> to IEnumerable<object>?

    Because who does the boxing? A conversion from double to object must box the double. Suppose you have a call to IEnumerator<object>.Current that is “really” a call to an implementation of IEnumerator<double>.Current. The caller expects an object to be returned. The callee returns a double. Where is the code that does the boxing instruction that turns the double returned by IEnumerator<double>.Current into a boxed double?

    It is nowhere, that’s where, and that’s why this conversion is illegal. The call to Current is going to put an eight-byte double on the evaluation stack, and the consumer is going to expect a four-byte reference to a boxed double on the evaluation stack, and so the consumer is going to crash and die horribly with an misaligned stack and a reference to invalid memory.

    If you want the code that boxes to execute then it has to be written at some point, and you’re the person who gets to write it. The easiest way is to use the Cast<T> extension method:

    IEnumerable<object> objects2 = doubleenumerable.Cast<object>();
    

    Now you call a helper method that contains the boxing instruction that converts the double from an eight-byte double to a reference.

    UPDATE: A commenter notes that I have begged the question — that is, I have answered a question by presupposing the existence of a mechanism which solves a problem every bit as hard as a solution to the original question requires. How does the implementation of Cast<T> manage to solve the problem of knowing whether to box or not?

    It works like this sketch. Note that the parameter types are not generic:

    public static IEnumerable<T> Cast<T>(this IEnumerable sequence) 
    {
        if (sequence == null) throw ...
        if (sequence is IEnumerable<T>) 
            return sequence as IEnumerable<T>;
        return ReallyCast<T>(sequence);
    }
    
    private static IEnumerable<T> ReallyCast<T>(IEnumerable sequence)
    {
        foreach(object item in sequence)
            yield return (T)item;
    }
    

    The responsibility for determining whether the cast from object to T is an unboxing conversion or a reference conversion is deferred to the runtime. The jitter knows whether T is a reference type or a value type. 99% of the time it will of course be a reference type.

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

Sidebar

Related Questions

I don't understand the last line of this function from Programming Perl 3e .
I have this method that gets the last line of a .txt file and
The last line in the following line keeps on generating the warning C4552: '<='
What does the last line mean in the following code? import pickle, urllib handle
I can add and remove the last line in my dynamic form and calculate
I would like to find the last line matching a certain pattern, in python.
The following code gives a segmentation fault on the last line require 'rubygems' gem
Visual Studio c++ 2005 I am getting an error on the last line of
Executing the below code gives me the following exception on the last line: InvalidOperationException:
I want my JTextArea to resize itself (expand vertically) when the last line (that

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.