Home [C# 筆記] 取出陣列元素的方法
Post
Cancel

[C# 筆記] 取出陣列元素的方法

常見取出陣列元素的方法有二種:

  • for
  • foreach

for

for迴圈取出陣列元素

1
2
3
4
int[] nums = { 9, 7, 2, 23, 6, 35 };
for(int i = 0; i < nums.Length; i++) {
    Console.WriteLine(nums[i]);
}

foreach

foreach迴圈取出陣列元素

1
2
3
4
int[] nums = { 9, 7, 2, 23, 6, 35 };
foreach(var e in nums) {
    Console.WriteLine(e);
}

MSDN - Array.CopyTo 方法
MSDN - Array.GetLength(Int32) Method
MSDN - Array.GetLowerBound(Int32) Method
MSDN - Array.GetValue 方法
MSDN - Array.SetValue 方法
c#中的Length和GetLength()的区别
[C# 筆記] array 陣列 by R
[C# 筆記] 陣列(Array)的宣告 by R
Book: Visual C# 2005 建構資訊系統實戰經典教本

This post is licensed under CC BY 4.0 by the author.