Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting object to IEnumerable<object> [duplicate]

The following is a simplified version of the problem I'm having:

var list = new List<int> {1, 2, 3, 4, 5};
// list Count = 5 System.Collections.Generic.List<int>

var obj = list as object;
// obj  Count = 5   object {System.Collections.Generic.List<int>}

var enumerable = obj as IEnumerable<object>;
// enumerable   null    System.Collections.Generic.IEnumerable<object>

The comments are the values from the watch window in Visual Studio 2017

However if I enter:

obj as IEnumerable<object>

in the watch window, I get:

obj as IEnumerable<object>  Count = 5   System.Collections.Generic.IEnumerable<object> {System.Collections.Generic.List<int>}

So how come I can cast it in the watch window and it works, but if I do it in the code it evaluates to null?

like image 920
Steve Wright Avatar asked Mar 19 '18 16:03

Steve Wright


People also ask

How to create an enumerable with an object and list?

If you really want an enumerable with that object then you need to create an empty enumerable and add that object to it. Update You have to create a Listfirst and add your object to that list, as, List<MyClass.Inventory> lst = new List<MyClass.Inventory>(); lst.Add(inventory); And then you can assign your list to the enumerable, as

What is cast<TResult> (IEnumerable) in Java?

An IEnumerable<T> that contains each element of the source sequence cast to the specified type. source is null. An element in the sequence cannot be cast to type TResult. The following code example demonstrates how to use Cast<TResult> (IEnumerable) to enable the use of the standard query operators on an ArrayList.

When to use IEnumerable<T>?

IEnumerable<T> can have zero or more objects, so defining an enumerable with a single object is useful as it can be filtered down to zero objects. And that's what the OP wants to do. This works even for value types. It then becomes somewhat equivalent to Nullable<T>. There are plenty of times when this is useful.

How to convert single object to IEnumerable in Java?

If you want to convert single object to IEnumerable then, you should implement IEnumerable interface to 'Inventory' class. – Uthaiah May 21 '14 at 5:33


1 Answers

From the C# documentation:

Variance in generic interfaces is supported for reference types only. Value types do not support variance. For example, IEnumerable<int> cannot be implicitly converted to IEnumerable<object>, because integers are represented by a value type.

What helps to know here is that the following two things are very different operations under the hood:

var l = (object) new List<int>();
var i = (object) 42;

despite the syntax looking the same. The first line merely treats the list as another type (with some checks whether the cast actually can work), while the second converts the integer into an object containing it (boxing). There's a third variant, which calls a conversion operator, and still looks the same. Since the first variant can get away with changing nothing about the object and just interpreting it differently it's vastly cheaper than the second. And I'd guess in this context here the only option that can work for interface covariance.

like image 163
Joey Avatar answered Sep 28 '22 07:09

Joey