📌  相关文章
📜  C#|将一种类型的数组转换为另一种类型的数组

📅  最后修改于: 2021-05-30 00:40:31             🧑  作者: Mango

Array.ConvertAll(TInput [],Converter )方法用于将一种类型的数组转换为另一种类型的数组。

句法:

public static TOutput[] ConvertAll (TInput[] array, 
Converter converter);

在此, TInputTOutput分别是源数组和目标数组。

参数:

返回值:该方法返回目标类型的数组,其中包含源数组中转换后的元素。

异常:如果数组为null或converter为null,则此方法将引发ArgumentNullException。

下面的程序说明了Array.ConvertAll(TInput [],Converter )方法的用法

范例1:

// C# program to demonstrate
// Array.ConvertAll() Method
using System;
using System.Collections.Generic;
  
public class GFG {
  
    // Main Method
    public static void Main()
    {
  
        try {
  
            // Creating and initializing
            // new the Array of int
            int[] myArr = {10, 20, 30, 40};
  
            // Display the values of the myArr.
            Console.WriteLine("Initial Array:");
  
            // calling the PrintIndexAndValues()
            // method to print
            PrintIndexAndValues(myArr);
  
            // converting int myArr to String conarr
            String[] conarr = Array.ConvertAll(myArr, 
              new Converter(intToString));
  
            // Display the values of the myArr.
            Console.WriteLine("Converted Array:");
  
            // calling the PrintIndexAndValues()
            // method to print
            PrintIndexAndValues(conarr);
        }
        catch (ArgumentNullException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
  
    // Defining the method
    // PrintIndexAndValues
    public static void PrintIndexAndValues(String[] myArr)
    {
        for (int i = 0; i < myArr.Length; i++) {
  
            Console.WriteLine("{0}", myArr[i]);
        }
        Console.WriteLine();
    }
  
    // Defining the method
    // PrintIndexAndValues
    public static void PrintIndexAndValues(int[] myArr)
    {
        for (int i = 0; i < myArr.Length; i++) {
  
            Console.WriteLine("{0}", myArr[i]);
        }
        Console.WriteLine();
    }
  
    // Defining the method
    // intToString
    public static String intToString(int pf)
    {
        return pf.ToString();
    }
}
输出:
Initial Array:
10
20
30
40

Converted Array:
10
20
30
40

范例2:

// C# program to demonstrate
// Array.ConvertAll() Method
using System;
using System.Collections.Generic;
  
public class GFG {
  
    // Main Method
    public static void Main()
    {
  
        try {
  
            // Creating and initializing new 
            // the Array of int with null value
            int[] myArr = null;
  
            // converting int myArr to String conarr
            String[] conarr = Array.ConvertAll(myArr, 
             new Converter(intToString));
  
            // calling the PrintIndexAndValues()
            // method to print
            PrintIndexAndValues(conarr);
        }
        catch (ArgumentNullException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
  
    // Defining the method
    // PrintIndexAndValues
    public static void PrintIndexAndValues(String[] myArr)
    {
        for (int i = 0; i < myArr.Length; i++) {
  
            Console.WriteLine("{0}", myArr[i]);
        }
        Console.WriteLine();
    }
  
    // Defining the method
    // intToString
    public static String intToString(int pf)
    {
        return pf.ToString();
    }
}
输出:
Exception Thrown: System.ArgumentNullException

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.array.convertall?view=netframework-4.7.2