简体   繁体   中英

How to create an array from a collection

A collection is passed to the function parameters. I need to take an element in the middle from it. How can i do this?

private PaperProduct midEl(ICollection<Type> Coll)
        {
            int index = (Coll.Count() / 2)+1;
            Type T = Coll.GetType();
            Coll.CopyTo(T[], 10);
        }

You can just convert the collection to an array and take the index.

var middle = Coll.ToArray()[index];

Or more simple and without conversion:

var middle = Coll.ElementAt(index);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM