Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a generic list of composite generic subtypes in C#

Tags:

c#

generics

I have implemented the following hierachical data structure: Tree<T> → Branch<T> → T.

UPDATE: Lots of people asked: Why don't you use object instead of <T> (or <dynamic> or whatever)? So I modified my question to state my "constraints". Here we go...

Here are example Trees:

*
├─Negative
│ ├─-2
├─0
│ ├─0
├─Positive
│ ├─2
│ ├─12
│ ├─2147483647

*
├─Spring
│ ├─Mar
│ ├─Apr
│ ├─May
├─Summer
│ ├─Jun
│ ├─Jul
│ ├─Aug
├─Fall
│ ├─Sep
│ ├─Oct
│ ├─Nov
├─Winter
│ ├─Dec
│ ├─Jan
│ ├─Feb

The implementation in C#:

public class Tree<T>
{
    public readonly List<Branch<T>> Branches = new List<Branch<T>>();
}

public class Branch<T>
{
    public readonly List<T> Leaves = new List<T>();
    public string Name { get; set; }
}

public class StringLeaf
{
    public StringLeaf(string value) { Label = value; }
    public string Label { get; private set; }
    public override string ToString() { return Label; }
}

public class PositiveIntLeaf
{
    private readonly int _value;
    public PositiveIntLeaf(int value) { _value = value; }

    public string Value
    {
        get { return _value < 0 ? "-" : _value.ToString(); }
    }
    public override string ToString() { return Value; }
}

public class IntTree : Tree<IntLeaf>
{
    private readonly Branch<IntLeaf> _negatives = new Branch<IntLeaf> { Name = "Negative" };
    private readonly Branch<IntLeaf> _zeros = new Branch<IntLeaf> { Name = "0" };
    private readonly Branch<IntLeaf> _positives = new Branch<IntLeaf> { Name = "Positive" };

    public IntTree()
    {
        Branches.AddRange(new []{
            _negatives,
            _zeros,
            _positives
        });
    }

    public void Add(int value)
    {
        if (value < 0) _negatives.Leaves.Add(new IntLeaf(value));
        else if (value > 0) _positives.Leaves.Add(new IntLeaf(value));
        else _zeros.Leaves.Add(new IntLeaf(value));
    }
}

Assuming I have different trees, I'am incapable to put them into a list:

IntTreeintTree = new IntTree();
intTree.Add(-2); intTree.Add(2); intTree.Add(0); intTree.Add(12); intTree.Add(int.MaxValue);
Tree<StringLeaf> months = new Tree<StringLeaf>{ Branches =
{
    new Branch<StringLeaf> { Name = "Spring", Leaves = { new StringLeaf( "Mar"),new StringLeaf("Apr") ,new StringLeaf("May")} },
    new Branch<StringLeaf> { Name = "Summer", Leaves = {  new StringLeaf( "Jun"),new StringLeaf("Jul") ,new StringLeaf("Aug")} },
    new Branch<StringLeaf> { Name = "Fall", Leaves = { new StringLeaf( "Sep"),new StringLeaf("Oct") ,new StringLeaf("Nov")} },
    new Branch<StringLeaf> { Name = "Winter", Leaves = { new StringLeaf( "Dec"),new StringLeaf("Jan") ,new StringLeaf("Feb")} }
}};

var list = new [] { intTree, months };
var currentTree = list[0];
// Work with the current tree:
var count = currentTree.Branches.Count;
Display(currentTree);

The errors is: No best type found for implicitly-typed array

How can I get a list out of all this trees?

I'd like to underline that I just want to put them in a list, maybe iterate through it and access the current tree and all its branches (e.g. to display their names). I don't care whether T is an object or an abstract base class! Assume that I just call .ToString(). The concrete type is just important for sub types like the IntTree.

like image 305
Marcel Avatar asked Nov 15 '13 14:11

Marcel


1 Answers

You can't really. This is a problem known as covariance and contravariance in generics. You can't stuff giraffes and tygers into a list of animals and hope that everything will be all right just because your collection is a list of animals.

So many articles written about this problem that I won't describe it to details. Just check out MSDN or google another articles.

like image 149
Ondrej Janacek Avatar answered Sep 28 '22 22:09

Ondrej Janacek