User999999
User999999

Reputation: 2520

Add Integer[] to list<Object>

Question: Why can't i add an Array of Integers to a List<Object>?

following the posts:

do-value-types-integer-decimal-boolean-etc-inherit-from-object

how-do-valuetypes-derive-from-object-referencetype-and-still-be-valuetypes

i'm understanding the following structure:

Integer => Inherits => System.ValueType => Inherits => System.Object

Then why am i getting an error when i do the following:

 List<object> myObjects = new List<object>();
 myObjects.AddRange(IntegerArray.ToList());

Given Error:

cannot convert from 'System.Collections.Generic.List<int>' to
'System.Collections.Generic.IEnumerable<object>

This one works though:

 List<int> myObjects = new List<int>();
 myObjects.AddRange(IntegerArray.ToList());

Upvotes: 1

Views: 4879

Answers (6)

garima garg
garima garg

Reputation: 310

int is not object type it's primitive type. So to convert it to Object, first you need to convert it to Integer, it's wrapper class, then convert it to Object.

Also, This code requires typecasting twice. First from int to Integer. then Integer to Object which can not be done implicitly. implicit typecasting can be done by compiler only once. So it needs explicit typecasting in Object type.

Upvotes: 0

L-Four
L-Four

Reputation: 13531

You have to explicitely say how to convert these types to each other by for example using ConvertAll, as explained here: convert a list of objects from one type to another using lambda expression.

The advantage is that it's potentially possible to convert any type.

Also look for contravariance to have an explanation on why this is: a List<Type1> cannot be casted to a List<Type2>.

Upvotes: 2

Raja Nadar
Raja Nadar

Reputation: 9489

Co/Contravariance aren't allowed in C# at a collection/generics level. i.e. assignment of List<int> to List<object>

Upvotes: 1

jmcilhinney
jmcilhinney

Reputation: 54417

The issue is that AddRange expects, as the error message suggests, an IEnumerable<object> and you're passing it an IEnumerable<int>. The fact that 'int' derives from object does not mean that IEnumerable<int> derives from IEnumerable<object>. If you do this then it will work because you'll be using the correct type:

myObjects.AddRange(IntegerArray.Cast<object>());

Upvotes: 4

Jon
Jon

Reputation: 437376

Because you are not adding an array to the List<object>, but rather attempting to add each element in the array individually.

You can certainly do this, which is how the question title reads to me:

List<object> myObjects = new List<object>();
myObjects.Add(new[] { 1, 2, 3 });

However your code tells a different story, and it fails because of signature incompatibility between AddRange and the type you use to pass to it. If you have an int[] and want to add its contents to the list individually, use Cast:

myObjects.AddRange((new[] { 1, 2, 3 }).Cast<object>());

Upvotes: 1

Daren Thomas
Daren Thomas

Reputation: 70314

Have you tried casting?

List<object> myObjects = new List<object>();
myObjects.AddRange(IntegerArray.ToList().Cast<object>());

Upvotes: 1

Related Questions