Skip to main content
Post Undeleted by Matthew Watson
added 1591 characters in body
Source Link
Matthew Watson
  • 111k
  • 12
  • 179
  • 300

JustIn theory you could add the extra type to the generic types for the class itself:

public class LDBList<A, T> : List<T> where T : LDBRootClass
{
    // typical constructor
    public LDBList(LDBList<A, T> x) : base(x) { }

    public LDBList(
        Func<A, T> converter,
        IList<A>   aList)
    {
        foreach (var x in aList)
        {
            this.Append(converter(x));
        }
    }
}

But of course that means that you need to declare the instances of that type with an extra type parameter that you won't even need unless you're using that specific constructor. So that's no good.

You could declare a helper class like so:

public static class LDBListCreator
{
    public static LDBList<T> CreateFrom<T, A>(Func<A, T> converter, IList<A> aList) where T: LDBRootClass
    {
        var result = new LDBList<T>();
        result.AddRange(aList.Select(converter));
        return result;
    }
}

This assumes that LDBList<T> has a default constructor.

But on inspection, you should be able to see that it's pointless creating such a simple helper class. If you add a constructor to your list class that accepts an IEnumerable<T> (like the List<T> class has) like so:

public LDBList(IEnumerable<T> items) : base(items)
{
}

Then you can construct an instance of LDBList<T> just by using IEnumerable.Select().

For example, given:

public class LDBRootClass
{
}

public class DerivedLDBRootClass : LDBRootClass
{
    public DerivedLDBRootClass(string value)
    {
        // .. whatever
    }
}

Then you could convert from a List<string> to a LDBList<DerivedLDBRootClass> very simply without any additional scaffolding, like so:

var strings = new List<string> { "One", "Two", "Three", "Four", "Five" };
var result  = new LDBList<DerivedLDBRootClass>(strings.Select(item => new DerivedLDBRootClass(item)));

Just add the extra type to the generic types for the class itself:

public class LDBList<A, T> : List<T> where T : LDBRootClass
{
    // typical constructor
    public LDBList(LDBList<A, T> x) : base(x) { }

    public LDBList(
        Func<A, T> converter,
        IList<A>   aList)
    {
        foreach (var x in aList)
        {
            this.Append(converter(x));
        }
    }
}

In theory you could add the extra type to the generic types for the class itself:

public class LDBList<A, T> : List<T> where T : LDBRootClass
{
    // typical constructor
    public LDBList(LDBList<A, T> x) : base(x) { }

    public LDBList(
        Func<A, T> converter,
        IList<A>   aList)
    {
        foreach (var x in aList)
        {
            this.Append(converter(x));
        }
    }
}

But of course that means that you need to declare the instances of that type with an extra type parameter that you won't even need unless you're using that specific constructor. So that's no good.

You could declare a helper class like so:

public static class LDBListCreator
{
    public static LDBList<T> CreateFrom<T, A>(Func<A, T> converter, IList<A> aList) where T: LDBRootClass
    {
        var result = new LDBList<T>();
        result.AddRange(aList.Select(converter));
        return result;
    }
}

This assumes that LDBList<T> has a default constructor.

But on inspection, you should be able to see that it's pointless creating such a simple helper class. If you add a constructor to your list class that accepts an IEnumerable<T> (like the List<T> class has) like so:

public LDBList(IEnumerable<T> items) : base(items)
{
}

Then you can construct an instance of LDBList<T> just by using IEnumerable.Select().

For example, given:

public class LDBRootClass
{
}

public class DerivedLDBRootClass : LDBRootClass
{
    public DerivedLDBRootClass(string value)
    {
        // .. whatever
    }
}

Then you could convert from a List<string> to a LDBList<DerivedLDBRootClass> very simply without any additional scaffolding, like so:

var strings = new List<string> { "One", "Two", "Three", "Four", "Five" };
var result  = new LDBList<DerivedLDBRootClass>(strings.Select(item => new DerivedLDBRootClass(item)));
Post Deleted by Matthew Watson
Source Link
Matthew Watson
  • 111k
  • 12
  • 179
  • 300

Just add the extra type to the generic types for the class itself:

public class LDBList<A, T> : List<T> where T : LDBRootClass
{
    // typical constructor
    public LDBList(LDBList<A, T> x) : base(x) { }

    public LDBList(
        Func<A, T> converter,
        IList<A>   aList)
    {
        foreach (var x in aList)
        {
            this.Append(converter(x));
        }
    }
}