Home > Net >  Create a generic method for list and array
Create a generic method for list and array

Time:08-24

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;
}

CodePudding user response:

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.

CodePudding user response:

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.

  •  Tags:  
  • c#
  • Related