Has any consideration been given to adding the abliity to use type parameters in casting
public class Customer { }
List<object> list = new List<object>();
list.Add(new Customer ());
list.Add(new Customer ());
list.Add(new Customer ());
List<Customer > list2 = list as List<Customer>;

Interest in type parameter casting?
Rory Niland
Another thing you could do, instead of copying, is create a proxy class. This way, like with casting, if you change the list, the original also changes. You can't use the List<T> class for this (since its members are non-virtual), but you could instead use IList<T> interface, and do something like this:
// Make a customer list of an object list:
IList<Customer> list2 = new DowncastList<Customer, object>(list2);
public class DowncastList<T, TOriginal> : IList<T>
where T : class, TOriginal {
private readonly IList<TOriginal> innerList;
public DowncastList(IList<TOriginal> list) {
innerList = list;
}
public T this[int index] {
get { return innerList[index] as T; }
set { innerList[index] = value; }
}
// ... other members of IList ...
}
(ps: the syntax highlighting *and* newlines in code blocks seem to be foobarred right now, sorry for the code formatting)
softgame
Jon
DB2Question
How do you mean that would work A List<Customer> reference can't refer to a List<object> object because the latter can obviously contain any object, including things that aren't Customers.
Piyush Jain
Converting is not the same as casting.
With List.ConvertAll you convert all object, you must create static method that convert on object to an other. When you want to use the ConvertAll method, here is a little example for you:
public void MyMethod()
{
List<object> list = new List<object>();
list.Add(new Customer ());
list.Add(new Customer ());
list.Add(new Customer ());
Converter<object><Customer> converter = new Converter<object><Customer>( ObjectToCustomer );
List<Customer > list2 = list.ConvertAll( converter );
foreach( object obj in list )
{
if( obj is Customer )
{
list2.Add( (Customer)obj );
}
}
}
public static Customer ObjectToCustomer(object obj)
{
if( obj is Customer )
{
return (Customer)objl
}
else
return null;
}
But remember by doing this, you can have null values in your Customer collection when there are some object in your source list (list) that aren't type off Customer!
Gemini II
You can't cast an List<Object> to a List<Customer>, just copy all elements.
List<object> list = new List<object>();
list.Add(new Customer ());
list.Add(new Customer ());
list.Add(new Customer ());
List<Customer > list2 = new List<Customer>( list.Count );
foreach( object obj in list )
{
if( obj is Customer )
{
list2.Add( (Customer)obj );
}
}
list.Clear();
AstareGod
I know that converting isn't the same as casting. I was giving an easier solution to creating a new list. Note that the conversion on a per-element basis you specify is just casting though - contrary to your assertion that 'conversion convert one object to an other' in this case there are no new ( "other" ) objects created. The difference between your original solution and using ConvertAll is just in terms of what happens when an element isn't a reference to a Customer. Given that the OP wanted a straight cast, I suspect he's only really interested in the case where all the elements are customers.
But no, you don't need a static method, or at least you don't need to declare one - we're using C# 2.0 here. You can just do:
List<Customer> list2 = list.ConvertAll
(new Converter<object,Customer>
(delegate(object x) { return (Customer)x; }));
Or if you don't know that everything's a Customer, and want nulls when there are non-customers, use:
List<Customer> list2 = list.ConvertAll
(new Converter<object,Customer>
(delegate(object x) { return x as Customer; }));
Note that your converter isn't actually doing what you describe - it isn't converting from one object to another - no new objects are created.
Now, if we need to do this regularly, there are better ways using generics. You can create a single generic method to create "simple" constructors like this:
static Converter<T,U> CreateConverter<T,U>()
where T : class
where U : class
{
return new Converter<T,U>
(delegate(T value) { return value as U; });
}
then use List<Customer> list2 = list.ConvertAll (CreateConverter<object,Customer>());
Another common situation is the reverse - where you want to convert from List<Customer> to List<object>. Here, the method is even simpler:
static Converter<T,U> IdentityConverter<T,U>()
where T : U
{
return new Converter<T,U> (delegate(T value) { return value; });
}
Jon