In answering this question Why does a Linq Cast<T> operation fail when I have an implicit cast defined?
I have found that there are a number of ways to implicitly cast between objects.
Consider the following two classes:
public class Class1
{
public int Test1;
}
public class Class2
{
public int Test2;
public static implicit operator Class1(Class2 item)
{
return new Class1 { Test1 = item.Test2 };
}
}
In order to convert a List to List we can do any of the following:
List<Class2> items = new List<Class2> { new Class2 { Test2 = 9 } };
foreach (Class1 item in items)
{
Console.WriteLine(item.Test1);
}
foreach (Class1 item in items.ConvertAll<Class1>(i=>i))
{
Console.WriteLine(item.Test1);
}
foreach (Class1 item in items.Select<Class2, Class1>(i=> i))
{
Console.WriteLine(item.Test1);
}
foreach (Class1 item in items.Select(i=>i))
{
Console.WriteLine(item.Test1);
}
But which is clearer to read and understand what is going on?