Is there any shortcut or better way to typecast List<bool> to List<object>?
List<bool>
List<object>
I know i can do that by looping and casting individual item but i want to know is thi_Stack Overflow日本語サイト
Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
I know i can do that by looping and casting individual item but i want to know is thi
I know i can do that by looping and casting individual item but i want to know is this possible to cast entire list in one statement.
The keyboard shortcut ctrl+shift+/ produces comments in the format of:
/*comment*/
How can I change the shortcut so that it adds a space before and after the asterisk?
/* comment */
Many thanks!
Enumerable.Cast<T>次のメソッドでこれを行うことができます。
Enumerable.Cast<T>
List<bool> bools= GetBoolList(); IList<Object> objects= bools.Cast<Object>().ToList();
The non-LINQ way to do this is with List.ConvertAll:
List.ConvertAll
List<bool> b = new List<bool> { true, false, true }; List<object> o = b.ConvertAll(x => (object)x);
Since this method knows what size to make the new list, it is likely to be faster than the LINQ version for large lists.
List<bool> list = new List<bool>{true,true,false,false,true}; List<Object> listObj1 = list.Select(i=> (Object)i).ToList();// First way List<Object> listObj2 = list.Cast<Object>().ToList();// Second way List<Object> listObj3 = list.OfType<Object>().ToList();// Third way
Following is to test quickly in linqpad
list.Dump(); listObj1.Dump(); listObj2.Dump(); listObj3.Dump();