C#中数组的使用
文章目錄
- 1 C#中的數(shù)組
- 1.1 語法
- 1.2 使用foreach遍歷數(shù)組
1 C#中的數(shù)組
1.1 語法
數(shù)據(jù)類型[] 數(shù)組名;
數(shù)組聲明同時初始化的三種情況:
int[] netScore1 = new int[3] { 67, 89, 78 }; int[] netScore2 = new int[] { 67, 89, 78 }; int[] netScore3 = { 67, 89, 78 };1.2 使用foreach遍歷數(shù)組
foreach循環(huán)結(jié)構(gòu):
foreach (元素類型 變量名 in 集合或者數(shù)組名) {//語句 } static void Test() {int[] netScore = new int[] { 67, 89, 78, 69, 95 };int sumScore = 0;//使用for循環(huán)遍歷數(shù)組for (int i = 0; i < netScore.Length; i++){sumScore += netScore[i];}int avgScore = sumScore / netScore.Length;Console.WriteLine($"學(xué)員的平均成績:{avgScore}"); }參考資料:
總結(jié)
- 上一篇: StringBuilder类
- 下一篇: C#中的值类型和引用类型