C#中用foreach语句遍历数组及将数组作为参数的用法
作者:goldensun 时间:2022-03-14 16:25:12
对数组使用 foreach
C#提供 foreach 语句。 该语句提供一种简单、明了的方法来循环访问数组或任何可枚举集合的元素。 foreach 语句按数组或集合类型的枚举器返回的顺序处理元素,该顺序通常是从第 0 个元素到最后一个元素。 例如,以下代码创建一个名为 numbers 的数组,并使用 foreach 语句循环访问该数组:
int[] numbers = { 4, 5, 6, 1, 2, 3, -2, -1, 0 };
foreach (int i in numbers)
{
System.Console.Write("{0} ", i);
}
// Output: 4 5 6 1 2 3 -2 -1 0
借助多维数组,你可以使用相同的方法来循环访问元素,例如:
int[,] numbers2D = new int[3, 2] { { 9, 99 }, { 3, 33 }, { 5, 55 } };
// Or use the short form:
// int[,] numbers2D = { { 9, 99 }, { 3, 33 }, { 5, 55 } };
foreach (int i in numbers2D)
{
System.Console.Write("{0} ", i);
}
输出:
9 99 3 33 5 55
但对于多维数组,使用嵌套的 for 循环可以更好地控制数组元素。
将数组作为参数传递
数组可作为实参传递给方法形参。由于数组是引用类型,因此方法可以更改元素的值。
将一维数组作为参数传递
可以将初始化的一维数组传递给方法。例如,下面的语句将数组发送到 print 方法。
int[] theArray = { 1, 3, 5, 7, 9 };
PrintArray(theArray);
下面的代码显示 print 方法的部分实现。
void PrintArray(int[] arr)
{
// Method code.
}
您可以在一个步骤中初始化和传递新数组,如下面的示例所示。
PrintArray(new int[] { 1, 3, 5, 7, 9 });
示例
说明
在下面的示例中,将初始化一个字符串数组并将其作为参数传递到字符串的 PrintArray 方法。该方法显示数组的元素。接下来,调用 ChangeArray 和 ChangeArrayElement 方法以演示通过值发送数组参数时不会阻止更改这些数组元素。
代码
class ArrayClass
{
static void PrintArray(string[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
System.Console.Write(arr[i] + "{0}", i < arr.Length - 1 ? " " : "");
}
System.Console.WriteLine();
}
static void ChangeArray(string[] arr)
{
// The following attempt to reverse the array does not persist when
// the method returns, because arr is a value parameter.
arr = (arr.Reverse()).ToArray();
// The following statement displays Sat as the first element in the array.
System.Console.WriteLine("arr[0] is {0} in ChangeArray.", arr[0]);
}
static void ChangeArrayElements(string[] arr)
{
// The following assignments change the value of individual array
// elements.
arr[0] = "Sat";
arr[1] = "Fri";
arr[2] = "Thu";
// The following statement again displays Sat as the first element
// in the array arr, inside the called method.
System.Console.WriteLine("arr[0] is {0} in ChangeArrayElements.", arr[0]);
}
static void Main()
{
// Declare and initialize an array.
string[] weekDays = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
// Pass the array as an argument to PrintArray.
PrintArray(weekDays);
// ChangeArray tries to change the array by assigning something new
// to the array in the method.
ChangeArray(weekDays);
// Print the array again, to verify that it has not been changed.
System.Console.WriteLine("Array weekDays after the call to ChangeArray:");
PrintArray(weekDays);
System.Console.WriteLine();
// ChangeArrayElements assigns new values to individual array
// elements.
ChangeArrayElements(weekDays);
// The changes to individual elements persist after the method returns.
// Print the array, to verify that it has been changed.
System.Console.WriteLine("Array weekDays after the call to ChangeArrayElements:");
PrintArray(weekDays);
}
}
输出:
Sun Mon Tue Wed Thu Fri Sat
arr[0] is Sat in ChangeArray.
Array weekDays after the call to ChangeArray:
Sun Mon Tue Wed Thu Fri Sat
arr[0] is Sat in ChangeArrayElements.
Array weekDays after the call to ChangeArrayElements:
Sat Fri Thu Wed Thu Fri Sat
将多维数组作为参数传递
可采用与传递一维数组相同的方式将初始化的多维数组传递给方法。
int[,] theArray = { { 1, 2 }, { 2, 3 }, { 3, 4 } };
Print2DArray(theArray);
下面的代码显示 print 方法的部分声明,该方法接受一个二维数组作为其参数。
void Print2DArray(int[,] arr)
{
// Method code.
}
您可以在一个步骤中初始化和传递新数组,如下面的示例所示。
Print2DArray(new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } });
示例
说明
在下面的示例中,将初始化一个二维整数数组并将其传递到 Print2DArray 方法。该方法显示数组的元素。
代码
class ArrayClass2D
{
static void Print2DArray(int[,] arr)
{
// Display the array elements.
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
System.Console.WriteLine("Element({0},{1})={2}", i, j, arr[i, j]);
}
}
}
static void Main()
{
// Pass the array as an argument.
Print2DArray(new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } });
// Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
输出:
Element(0,0)=1
Element(0,1)=2
Element(1,0)=3
Element(1,1)=4
Element(2,0)=5
Element(2,1)=6
Element(3,0)=7
Element(3,1)=8
标签:C#,数组,foreach
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#日期格式字符串的相互转换操作实例分析
2021-09-01 10:06:56
Java程序常见异常及处理汇总
2021-08-12 04:20:44
![](https://img.aspxhome.com/file/2023/8/66688_0s.jpg)
java UDP实现一个聊天工具的示例代码
2021-09-19 18:41:47
![](https://img.aspxhome.com/file/2023/3/92723_0s.png)
使用HTTPclient保持长连接
2023-10-17 12:29:34
![](https://img.aspxhome.com/file/2023/9/64109_0s.png)
SpringCloud实战之Zuul网关服务
2023-01-30 11:33:53
![](https://img.aspxhome.com/file/2023/4/60544_0s.png)
java并发编程工具类JUC之ArrayBlockingQueue
2023-07-04 21:02:20
ListView用法中与滚动相关的需求实现
2021-06-08 09:00:08
![](https://img.aspxhome.com/file/2023/1/100091_0s.png)
Android自定义ViewGroup实现可滚动的横向布局(2)
2022-10-08 17:25:27
c# 在windows中操作IIS设置FTP服务器的示例
2023-07-18 06:13:01
![](https://img.aspxhome.com/file/2023/6/66116_0s.jpg)
关于Unsupported major.minor version 49.0的错误解决办法
2023-06-04 22:57:44
![](https://img.aspxhome.com/file/2023/7/96077_0s.jpg)
java按钮控件数组实现计算器界面示例分享
2021-09-12 22:37:59
基于Spring-Security自定义登陆错误提示信息
2021-09-20 17:33:40
![](https://img.aspxhome.com/file/2023/2/70352_0s.png)
mybatis的动态SQL和模糊查询实例详解
2022-03-10 09:47:03
![](https://img.aspxhome.com/file/2023/1/99851_0s.png)
SpringBoot拦截器的使用
2023-04-27 23:27:27
![](https://img.aspxhome.com/file/2023/7/91077_0s.png)
struts1之简单mvc示例_动力节点Java学院整理
2021-10-28 14:43:03
![](https://img.aspxhome.com/file/2023/5/68545_0s.png)
Java8生成时间方式及格式化时间的方法实例
2021-07-28 19:58:52
C#动态生成DropDownList执行失败原因分析
2023-08-30 22:37:28
j2ee之AJAX二级联动效果
2021-09-13 10:06:58
springboot相关面试题汇总详解
2023-10-06 17:16:11
Java实现的计时器【秒表】功能示例
2023-11-10 21:24:50
![](https://img.aspxhome.com/file/2023/1/59051_0s.gif)