Hi,
if I have two classes, and have defined an explicit type conversion between them, should I not be able to convert an array of one to an array of the other ?
ie.
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Apple ted = new Apple() { Variety = Apple.EVariety.RedEllison };
Orange bob = (Orange)ted; // explicit type conversion
Apple[] apples = new Apple[] { ted };
Orange[] oranges = new Orange[1];
//oranges = apples; // why is this illegal ?
// is this better ?
oranges = Array.ConvertAll<Apple, Orange>(apples, new Converter<Apple, Orange>(p => (Orange)p));
}
class Apple
{
public enum EVariety { RedEllison, GrannySmith }
public EVariety Variety;
}
class Orange
{
enum EColour { Unknown, Red, Green }
EColour Colour;
public static explicit operator Orange(Apple apple)
{
Orange result = new Orange();
result.Colour = apple.Variety == Apple.EVariety.RedEllison ? result.Colour = EColour.Red : result.Colour = EColour.Green;
return result;
}
}
}
}
Thanks, Ross