Why doesn't strings.Cast <object> cast the list <string> to the list <object>?

In this respect it was suggested that I could generate a total collection up to a collection of objects using .Cast<object>. After re-reading a bit.Cast<> , I still can’t get it in the general collection for adding to another collection. Why does the following not work?

using System.Collections.Generic;
using System.Linq;
using System;

namespace TestCast2343
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> strings = new List<string> { "one", "two", "three" };

            //gives error: cannot convert from 'System.Collections.Generic.List<string>'
            //to 'System.Collections.Generic.List<object>'
            //IEnumerable<string> items = strings.Cast<object>();

            //this works
            strings.Cast<object>();

            //but they are still strings:
            foreach (var item in strings)
            {
                System.Console.WriteLine(item.GetType().Name);
            }

            //gives error: cannot convert from 'System.Collections.Generic.List<string>'
            //to 'System.Collections.Generic.List<object>'
            ProcessCollectionDynamicallyWithReflection(strings);

            Console.ReadLine();
        }

        static void ProcessCollectionDynamicallyWithReflection(List<object> items)
        {
            //...
        }
    }

}

Answer:

Thanks Reed, here is the code I received:

using System.Collections.Generic;
using System.Linq;
using System;

namespace TestCast2343
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> strings = new List<string> { "one", "two", "three" };
            List<int> ints = new List<int> { 34, 35, 36 };
            List<Customer> customers = Customer.GetCustomers();

            ProcessCollectionDynamicallyWithReflection(strings.Cast<object>().ToList());
            ProcessCollectionDynamicallyWithReflection(ints.Cast<object>().ToList());
            ProcessCollectionDynamicallyWithReflection(customers.Cast<object>().ToList());

            Console.ReadLine();
        }

        static void ProcessCollectionDynamicallyWithReflection(List<object> items)
        {
            foreach (var item in items)
            {
                Console.WriteLine(item.GetType().Name);
            }
        }
    }

    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Street { get; set; }
        public string Location { get; set; }
        public string ZipCode { get; set; }

        public static List<Customer> GetCustomers()
        {
            List<Customer> customers = new List<Customer>();
            customers.Add(new Customer { FirstName = "Jim", LastName = "Jones", ZipCode = "23434" });
            customers.Add(new Customer { FirstName = "Joe", LastName = "Adams", ZipCode = "12312" });
            customers.Add(new Customer { FirstName = "Jake", LastName = "Johnson", ZipCode = "23111" });
            customers.Add(new Customer { FirstName = "Angie", LastName = "Reckar", ZipCode = "54343" });
            customers.Add(new Customer { FirstName = "Jean", LastName = "Anderson", ZipCode = "16623" });
            return customers;
        }
    }
}
+3
source share
5 answers

You are using it wrong Cast<T>.

Firstly, here:

IEnumerable<string> items = strings.Cast<object>();

strings.Cast<object>(), IEnumerable<object>, IEnumerable<string>. - , .

, , List<object>, IEnumerable<T> IList<T>. :

// Cast to IEnumerabe<object> then convert to List<object>
ProcessCollectionDynamicallyWithReflection(strings.Cast<object>().ToList());
+11

, Cast<> List<T>, IEnumerable<T>. .ToList , .

strings.Cast<object>().ToList();
+2

: ProcessCollectionDynamicallyWithReflection, :

private static void ShowItemTypes(IEnumerable items)
{
    foreach (object item in items)
    {
        string itemTypeName = (item != null) ? item.GetType().Name : "null";
        Console.WriteLine(itemTypeName);
    }
}
+1

:

IEnumerable<object> items = strings.Cast<object>();

, IEnumerable ProcessCollectionDynamicallyWithReflection . , :

List<object> items = strings.Cast<object>().ToList();
0

, .Cast<object>

, . Cast , , , .

, , . (, , , .)

, , List<object>, :

List<object> objects = strings.Cast<object>.ToList();

Perhaps you should consider using generics instead of dropping an object:

static void ProcessCollectionDynamicallyWithReflection<T>(List<T> items)

That way, you can write strongly typed code in a method, and you don’t need to create a new collection to send it to the method.

0
source

Source: https://habr.com/ru/post/1724522/


All Articles