C#常见技能_数组

前几天一个学员在学习C#与数组交互时,也不知道数组可以用来做什么 。下面我们就详细讲讲C# 和数组交互的相关知识。

在C#编程中,数组是一种非常重要的数据结构,它可以存储多个相同类型的数据,并且使用索引来访问这些数据。在实际应用中,我们经常需要在数组和其他结构之间传递数据和消息,以实现功能的协作和交互。本文将介绍如何在C#中实现数组和其他结构之间的交互,并提供一些实用的示例。

一、数组基础

在C#中,数组是一种由相同类型元素组成的集合。数组可以通过声明和初始化来创建,并且可以使用索引来访问其中的元素。以下是一个示例,演示了如何声明、初始化和访问一个整数数组:

public class MyClass

{

public static void Main(string[] args)

{

int[] numbers = new int[5];

numbers[0] = 10;

numbers[1] = 20;

numbers[2] = 30;

numbers[3] = 40;

numbers[4] = 50;

for (int i = 0; i < numbers.Length; i++)

{

Console.WriteLine($"The element at index {i} is {numbers[i]}");

}

}

}

在上面的代码中,我们声明了一个包含5个整数元素的数组,并将其初始化为0。然后,我们分别将数组中的前5个元素赋值为10、20、30、40和50。接着,我们使用for循环遍历数组,并使用索引访问每个元素并输出其值。

二、数组和方法之间的交互

在C#中,我们可以将数组作为参数传递给方法,并从方法中返回数组。以下是一个示例,演示了如何在方法和数组之间传递数据:

public class MyClass

{

public static void Main(string[] args)

{

int[] numbers = { 10, 20, 30, 40, 50 };

int sum = GetSum(numbers);

Console.WriteLine($"The sum of the elements in the array is {sum}");

}

public static int GetSum(int[] numbers)

{

int sum = 0;

for (int i = 0; i < numbers.Length; i++)

{

sum += numbers[i];

}

return sum;

}

}

在上面的代码中,我们声明了一个整数数组,并将其初始化为包含5个元素的数组。然后,我们调用GetSum方法,并将数组作为参数传递给该方法。GetSum方法接收一个整数数组参数,并计算数组中所有元素的总和。最后,GetSum方法返回计算得到的总和,并将其赋值给sum变量。在Main方法中,我们输出计算得到的总和。

三、数组和循环结构之间的交互

在C#中,我们可以使用循环结构遍历数组,并根据需要执行不同的操作。以下是一个示例,演示了如何在循环结构和数组之间交互:

public class MyClass

{

public static void Main(string[] args)

{

int[] numbers = { 10, 20, 30, 40, 50 };

for (int i = 0; i < numbers.Length; i++)

{

if (numbers[i] % 2 == 0)

{

Console.WriteLine($"The element at index {i} is even");

}

else

{

Console.WriteLine($"The element at index {i} is odd");

}

}

}

}

在上面的代码中,我们声明了一个整数数组,并将其初始化为包含5个元素的数组。然后,我们使用for循环遍历数组,并判断每个元素是否为偶数。如果元素为偶数,则输出“索引为i的元素是偶数”,否则输出“索引为i的元素是奇数”。

四、数组和选择结构之间的交互

在C#中,我们可以使用选择结构根据条件执行不同的操作。以下是一个示例,演示了如何在选择结构和数组之间交互:

public class MyClass

{

public static void Main(string[] args)

{

int[] numbers = { 10, 20, 30, 40, 50 };

int sum = 0;

for (int i = 0; i < numbers.Length; i++)

{

if (numbers[i] % 2 == 0)

{

sum += numbers[i];

}

}

if (sum > 0)

{

Console.WriteLine($"The sum of even elements in the array is {sum}");

}

else

{

Console.WriteLine("There are no even elements in the array");

}

}

}

在上面的代码中,我们声明了一个整数数组,并将其初始化为包含5个元素的数组。然后,我们使用for循环遍历数组,并计算所有偶数元素的总和。如果总和大于0,则输出“偶数元素的总和是sum”,否则输出“数组中没有偶数元素”。

五、数组和对象之间的交互

在C#中,我们可以使用对象来封装数据和方法,并将其作为参数传递给其他方法或存储在数组中。以下是一个示例,演示了如何在数组和对象之间交互:

public class Student

{

public string Name;

public int Age;

public Student(string name, int age)

{

this.Name = name;

this.Age = age;

}

}

public class MyClass

{

public static void Main(string[] args)

{

Student[] students = new Student[3];

students[0] = new Student("Tom", 18);

students[1] = new Student("Jerry", 19);

students[2] = new Student("Mary", 20);

foreach (Student student in students)

{

Console.WriteLine($"Name: {student.Name}, Age: {student.Age}");

}

}

}

在上面的代码中,我们定义了一个Student类,并在该类中定义了姓名和年龄属性。然后,在Main方法中,我们声明了一个包含3个元素的Student数组,并将其初始化为3个Student对象。最后,我们使用foreach循环遍历数组,并输出每个学生的姓名和年龄。

六、数组和文件之间的交互

在C#中,我们可以使用文件读写操作来读取和写入数组数据。以下是一个示例,演示了如何在数组和文件之间交互:

public class MyClass

{

public static void Main(string[] args)

{

int[] numbers = { 10, 20, 30, 40, 50 };

// 写入数据到文件

using (StreamWriter writer = new StreamWriter("numbers.txt"))

{

foreach (int number in numbers)

{

writer.WriteLine(number);

}

}

// 从文件读取数据

using (StreamReader reader = new StreamReader("numbers.txt"))

{

List<int> list = new List<int>();

string line;

while ((line = reader.ReadLine()) != null)

{

list.Add(int.Parse(line));

}

int[] newArray = list.ToArray();

Console.WriteLine("The elements in the array are:");

foreach (int number in newArray)

{

Console.WriteLine(number);

}

}

}

}

在上面的代码中,我们声明了一个整数数组,并将其初始化为包含5个元素的数组。然后,我们使用StreamWriter类将数组中的每个元素写入到名为“numbers.txt”的文件中。接着,我们使用StreamReader类从“numbers.txt”文件中读取数据,并将其存储在List<int>对象中。最后,我们将List<int>转换为int[]数组,并输出该数组中的所有元素。

部分项目图片:

七、结论

在C#编程中,数组是一种非常重要的数据结构,它可以存储多个相同类型的数据,并且使用索引来访问这些数据。通过本文的介绍,我们了解了如何在C#中实现数组和其他结构之间的交互,并提供了一些实用的示例。希望这篇文章能够帮助你更好地理解和应用数组。。

文章如果对你有用,麻烦点赞,评论~
最近很多小伙伴找我,说想要一些学习资料,然后我根据自己从业二十年经验,精心整理了一份「上位机编程入门到高级教程+工具包」,点个关注,限时分享给大家,以下是领取入口:

 点击领取上位机编程全套入门教程+工具

猜你喜欢

转载自blog.csdn.net/hspx668/article/details/131082666
今日推荐