Why C # convertall requires 2 parameters

I have an array of Car objects

I want to convert them to a list of Vehicle objects

I thought it would work

Vehicle[] vehicles = cars.ConvertAll(car=> ConvertToVehicle(car)).ToArray();

but he complains that ConvertAll requires two parameters.

here is the error:

Error 2 Using the general method "System.Array.ConvertAll (TInput [], System.Converter)" requires arguments of type "2" C: \ svncheckout \ latestTrunk \ Utility \ test.cs 229 33

Am I using the wrong method here?

+3
source share
4 answers

ConvertAll (Car []) (), 1. - , .

+4

, , , .

:

Array.ConvertAll<Vehicle>(cars, car=> ConvertToVehicle(car))
+1

Array.ConvertAll , , Select:

Vehicle[] vehicles = cars.Select(car=> ConvertToVehicle(car)).ToArray();

Vehicle[] vehicles = Array.ConvertAll(cars, car=> ConvertToVehicle(car));

:

  • Enumerable.Select, static - -
  • Array.ConvertAll ,
  • Enumerable.Select IEnumerable<T>, Enumerable.ToArray
  • Array.ConvertAll , , , Enumerable.ToArray
+1

If the car is a subtype of type Super Type, you can do the following. It should work equally well if the ConvertToVehicle returns the vehicle type.

class Vehicle { }
class Car : Vehicle { }

class Program
{
    static List<Car> ll = new List<Car>();
    static void Main(string[] args)
    {
       Vehicle[] v = ll.ConvertAll<Vehicle>(x => (Vehicle)x).ToArray();
    }
}
0
source

Source: https://habr.com/ru/post/1752022/