Yahoo Web Search

Search results

  1. Nov 29, 2016 · You can do some workaround here (although i myself don't like it) through this method : public <T> List<T> convert(List list, T t){. return list; } Yes. It will cast your list into your demanded generic type. In the given case above, you can do some code like this : List<Object> list = getList();

    Usage example

    (List<Customer>)(Object)list;
  2. Jan 20, 2012 · Cast will throw an exception if you cannot cast to the type specified. OfType on the other hand will return only those items in the list that can be cast to the specified type. I would recommend using OfType in your situation. List<Foo> fooList = myList.OfType<Foo>().ToList();

  3. Sep 16, 2010 · You could do a few things. One example is cast the elements of the list to Animal. Using your code: cat.Play(new List<Cat>().Cast<Animal>().ToList()); Another is to make Animal generic, so cat.Play(new List<Cat>()); would work.

  4. Jan 19, 2012 · You can't cast it (preserving reference identity) - that would be unsafe. For example: public interface IFruit {} public class Apple : IFruit {} public class Banana : IFruit {} ... List<Apple> apples = new List<Apple>(); List<IFruit> fruit = apples; // Fortunately not allowed. fruit.Add(new Banana());

  5. Jul 14, 2014 · Much of the time an implicit cast will work, assign a LinkedList to a List, or pass it to a function expecting a List and it should just `work'. An explicit cast can also be done if necessary. //This is valid List<Customer> myList = new LinkedList<Customer>(); //Also Valid List<Customer> myList = (List<Customer>) new LinkedList<Customer>();

  6. Oct 22, 2010 · In C# it's possible to cast to List<T> - so if you have: List<Activity> _Activities; List<T> _list; The following will work: _list = _Activities as List<T>; but the translated line with VB.NET which is: _list = TryCast(_Activities, List(Of T)) throws a compilation error.

  1. People also search for