C#: list of <T> of = list of <Derived from T> doesn't compile
I've got a base Class ScenBase and a derived class ScenC. I have a class HexC derived form the base class Hex. I also have a class UnitC derived from the base class Unit. I can set an array of Hex to point to an array of HexC, but the compiler won't let me set a List of Unit to point to a list of UnitC:
// The Core Database for the game scenario.
// Theoretically multiple secenarios can be loaded
// into memory at the same time.
[Serializable]
public class ScenC : ScenBase
{
public开发者_开发知识库 bool playable { get; set; }
public HexC[,] hexCs { get; private set; }
public List<UnitC> unitCs { get; private set; }
// A database for every user and AI entity.
public List<ScenV> chViews { get; private set; }
public List<string> characters { get; private set; }
public ScenC(int xDimI, int yDimI) : base (xDimI, yDimI)
{
playable = false;
chViews = new List<ScenV>();
characters = new List<string>();
characters.Add("Supreme");
hexCs = new HexC[xDim, yDim];
hexs = hexCs; //this line complies fine
newHex = (int x, int y) => new HexC(this, x, y);
unitCs = new List<UnitC>();
// **This line won't compile**
unitCs = units;
Init();
}
}
Here are the fields properties for the base class ScenBase:
[Serializable]
public abstract class ScenBase
{
public Hex[,] hexs;
public List<Unit> units { get; protected set; }
public DateTime currGameTime { get; set; }
public int xDim { get; set; }
public int yDim { get; set; }
public double scale { get; protected set; }
protected delegate Hex NewHex(int x, int y);
protected NewHex newHex;
//Rest of Class not shown for simplicity
}
Why Can't I set a List<Hex>
to a List<HexC>
when that follows the "is a" implicit cast rule, but I can set array to array
This is not possible.
Had it been possible, you would have been able to add a different derived type to the list.
For example:
List<Car> cars = new List<Car>
List<Vehicle> vehicles = cars; //Error!
vehicles.Add(new Bicycle()); //That's not a car!
What you're asking for is called covariance; it's only possible for immutable interfaces (such as IEnumerable<T>
)
you could use a list of interfaces (1) where the Hex and Unit conform to the nominated interface in some way. otherwise, you will need to cast.
ref: http://msdn.microsoft.com/en-us/library/dd465120.aspx
btw: any interface of IEnumerable is already covariant (see: http://msdn.microsoft.com/en-us/library/9eekhta0.aspx). Syntax: the out in IEnumerable<out T> defines covariance.
(1) A list of interface
using System;
using System.Collections.Generic;
public interface IHex {
string ToString();
}
public class Hex : IHex
{
public override string ToString() { return "Hex"; }
}
public interface IHexC : IHex {}
public class HexC : Hex
{
public override string ToString() { return "HexC"; }
}
public class Test{
public static void Main()
{
IList<IHex> HexList = new List<IHex>();
HexList.Add(new Hex());
HexList.Add(new HexC());
HexList.Add(new HexC());
foreach(var o in HexList){
Console.WriteLine(o.ToString());
}
}
}
Console output:
Hex
HexC
HexC
Not possible like SLaks said, you could use an interface instead. IList<IHex>
for example...
you can't cast a
List<UnitC> // UnitC - to a derived from a Unit class
to a
List<Unit> // Unit - the base class
try with a IUnit interface instead
A general point to consider, in addition to SLaks' explanation as to why your code doesn't work is that using a generic defines a new type:
Unit is a type.
UnitC is a type derived from Unit.
List<Unit> is a new type.
List<UnitC> is a new type, but is not derived from List<Unit>.
However,
Hex[] defines multiple instances of the Hex type.
HexC[] defines multiple instances of the HexC type (which is derived from Hex).
Your assignment doesn't work because the compiler can't ensure that the elements of units are actually instances of UnitC, but if you attempted the inverse (units = unitCs), that wouldn't work, either, because List < UnitC > isn't derived from List < Unit >.
精彩评论