📌  相关文章
📜  C#|对数组的每个元素执行指定的操作

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

Array.ForEach(T [],Action )方法用于对指定数组的每个元素执行指定操作。

句法:

public static void ForEach (T[] array, Action action);

参数:

异常:该方法抛出ArgumentNullException数组为null或action为null。

下面的程序说明了Array.ForEach(T [],Action )方法:

范例1:

// C# program to demonstrate
// Array.ForEach(T[], Action)
// Method
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        try {
  
            // Creating and intializing
            // new Array of int
            int[] myArr = { 2, 3, 4, 5 };
  
            // Display the values of the myArr.
            Console.Write("Initial Array: ");
  
            // calling the PrintIndexAndValues()
            // method to print
            PrintIndexAndValues(myArr);
  
            // set a delegate for
            // the SetSquares method
            Action action = new Action(SetSquares);
  
            // performing the action
            // using ForEach() method
            Array.ForEach(myArr, action);
        }
        catch (ArgumentNullException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
  
    // Defining the method
    // PrintIndexAndValues
    public static void PrintIndexAndValues(int[] myArr)
    {
        for (int i = 0; i < myArr.Length; i++) {
  
            Console.Write("{0} ", myArr[i]);
        }
        Console.WriteLine();
        Console.WriteLine();
    }
  
    // Defining the method
    // ShowSquares
    private static void SetSquares(int val)
    {
        Console.WriteLine("{0} squared = {1}",
                              val, val * val);
    }
}
输出:
Initial Array: 2 3 4 5 

2 squared = 4
3 squared = 9
4 squared = 16
5 squared = 25

范例2:

// C# program to demonstrate
// Array.ForEach(T[], Action)
// Method
using System;
using System.Collections.Generic;
  
public class GFG {
  
    // Main Method
    public static void Main()
    {
  
        try {
  
            // Creating and intializing 
            // new Array of with null
            int[] myArr = null;
  
            // set a delegate for
            // the SSquares method
            Action action = new Action(SetSquares);
  
            // performing the action
            // using ForEach() method
            Console.WriteLine("Trying to perform "
                       +"action on a null Array");
            Console.WriteLine();
            Array.ForEach(myArr, action);
        }
        catch (ArgumentNullException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
  
    // Defining the method
    // PrintIndexAndValues
    public static void PrintIndexAndValues(int[] myArr)
    {
        for (int i = 0; i < myArr.Length; i++) {
  
            Console.Write("{0} ", myArr[i]);
        }
        Console.WriteLine();
        Console.WriteLine();
    }
  
    // Defining the method
    // ShowSquares
    private static void SetSquares(int val)
    {
        Console.WriteLine("{0} squared = {1}", val, val * val);
    }
}
输出:
Trying to perform action on a null Array

Exception Thrown: System.ArgumentNullException

参考:

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