2

How can I create a generic method so the return type is either a list or an array?

Now for this method I get this error:

(string, int)[]' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic method 'T TournamentsAnalytics.GetParameters()

private void Test()
{
    var parameters = GetParameters<List<(string, int )>>();
    var parameters2 = GetParameters<(string, int)[]>();
}

private T GetParameters<T>() where T: ICollection<(string, int)>, new()
{
    var parameters = new T
    {
        ("nr1", 1),
        ("nr2", 2),
        ("nr3", 3),
        ("nr4", 4),
        ("nr5", 5),
        ("nr6", 6)
    };
    return parameters;
}
2
  • if you know what ICollection is, why do not use this type instead of T ? Commented Aug 23, 2022 at 8:41
  • Type (string, int)[] doesn't satisfy constraint new(). Commented Aug 23, 2022 at 8:47

2 Answers 2

4

You probably should not use generics for this. You could for example just use LINQ to convert a sequence of values to a list or an array:

GetParameters().ToList();
GetParameters().ToArray();
...
private IEnumerable<(string, int)> GetParameters(){
   yield return ("nr1", 1);
   yield return ("nr2", 2);
        ...

This seem like it is both much shorter and simpler than messing around with generics. Or just return either a list or an array, and use LINQ to convert to the other type if needed, for small lists created infrequently any inefficiencies will be irrelevant.

There might be ways to use generics if you have some specific issue to solve, For example delegating the creation of the collection to the caller by injecting a delegate: Func<IEnumerable<(string, int)>, T), but it will likely just make things more complicated.

Note that your example uses a collection initializer, and this will just not work with arrays, since this initializer uses the Add-method, and this will just not work for arrays.

Sign up to request clarification or add additional context in comments.

Comments

-1

The constructor of the Array class is private, and as a result you can't pass an array type into a method with a generic new() constraint.

Arrays can still be created in a few ways using the new keyword:

  • var cars = new Car[3]; // no Car objects created (yet)
  • var cars = new[] { new Car(), new Car() }; // initialized
  • var cars = new Car[] { new Car(), new Car() }; // initialized

This is special syntax for arrays, and you'll see that the syntax is also different from what you are trying to make work in the generic method.

I am assuming that the new calls are converted by the compiler to call the static Array.CreateInstance method (which then calls the private constructor), followed by initializing the entries as needed.

2 Comments

This is not correct. List<T> satisfies both constraints.
Answer improved (I hope)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.