Why this line throws an error?

Practicing interview questions. Why does the second line throws an error? Is it because an explicit conversation is needed? I read about ValueTypes but still don't understand why it errors out..

IEnumerable<object> objects = new List<string>();
IEnumerable<ValueType> v =  objects; //*** error here.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
You could cast it and it will compile.
            IEnumerable<object> objects = new List<string>();
            IEnumerable<ValueType> v = (IEnumerable<ValueType>)objects;

Note it will then fail at run time, at least in this particular case.

A reason being is the following does not work
            IEnumerable<ValueType> objects = new List<string>();

Because a string is not a ValueType
If on the other hand, you meant to do this by insinuating that ValueType is the type of the original list's T parameter.  Then you wont get a runtime error as pointed out by Andy above.  In order to accomplish this, you would do something like this:
IEnumerable<object> objects = new List<string>();
IEnumerable<string> values = objects as IEnumerable<string>;
// IEnumerable<string> values = (IEnumerable<string>)objects; // This is the same thing as above

Open in new window

Proof of concept -
using System;
using System.Collections.Generic;

namespace EE_Q29137538
    class Program
        static void Main(string[] args)
            IEnumerable<object> objects = new List<string> { "A", "New", "List" };
            IEnumerable<string> values = objects as IEnumerable<string>;
            Console.WriteLine(string.Join(" ", values));

Open in new window

Which produces the following output -Capture.PNG-saige-
CamilliaAuthor Commented:
I tried casting but I did it wrong. These are the options they gave me (interviewer let me take the questions with me)

Their question was "why is there an error when compiling the code". Looking at your explanation...it's the last one...explicit conversion is needed...correct?

AndyAinscowFreelance programmer / ConsultantCommented:
To make it compile one must use a cast.  (Note it doesn't make the code actually work.)
So it certainly looks like the final choice is the one they wanted as the answer.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CamilliaAuthor Commented:
Thanks, Andy
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.