白骑士的C#教学进阶篇 2.2 集合与泛型

21 篇文章 1 订阅

系列目录

上一篇:白骑士的C#教学进阶篇 2.1 面向对象编程

        在C#编程中,集合是存储和管理数据的基本工具。集合类型包括数组、列表、字典等,它们提供了不同的方式来组织和操作数据。泛型的引入允许开发者定义类型安全的集合,从而提高代码的重用性和性能。在本节中,将详细介绍数组与列表、字典与集合以及泛型与泛型集合的使用方法和特点,帮助您理解如何在C#中高效地管理数据。

数组与列表

数组

        数组是一种定长的集合,用于存储相同类型的元素。数组的大小在创建时确定,之后不能更改。

定义和使用数组

public class Program
{
    public static void Main(string[] args)
    {
        // 定义一个整数数组
        int[] numbers = new int[5] { 1, 2, 3, 4, 5 };

        // 访问数组元素
        Console.WriteLine("First element: " + numbers[0]);

        // 遍历数组
        for (int i = 0; i < numbers.Length; i++)
        {
            Console.WriteLine("Element at index " + i + ": " + numbers[i]);
        }
    }
}

        在这个示例中,定义了一个整数数组 ‘numbers‘,并遍历打印其元素。

列表

        列表(List)是一种动态数组,大小可以根据需要动态调整。‘List‘ 类在 ‘System.Collections.Generic‘ 命名空间中定义。

定义和使用列表

using System;
using System.Collections.Generic;


public class Program
{
    public static void Main(string[] args)
    {
        // 定义一个整数列表
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

        // 添加元素
        numbers.Add(6);

        // 访问列表元素
        Console.WriteLine("First element: " + numbers[0]);

        // 遍历列表
        foreach (int number in numbers)
        {
            Console.WriteLine("Element: " + number);
        }
    }
}

        在这个示例中,定义了一个整数列表‘numbers‘,并遍历打印其元素。

字典与集合

字典

        字典(Dictionary)是一种键值对集合,每个键必须是唯一的。‘Dictionary‘ 类在 ‘System.Collections.Generic‘ 命名空间中定义。

定义和使用字典

using System;
using System.Collections.Generic;


public class Program
{
    public static void Main(string[] args)
    {
        // 定义一个字符串键和整数值的字典
        Dictionary<string, int> ages = new Dictionary<string, int>();

        // 添加键值对
        ages["Alice"] = 30;
        ages["Bob"] = 25;

        // 访问字典元素
        Console.WriteLine("Alice's age: " + ages["Alice"]);

        // 遍历字典
        foreach (var kvp in ages)
        {
            Console.WriteLine(kvp.Key + " is " + kvp.Value + " years old.");
        }
    }
}

        在这个示例中,定义了一个字符串键和整数值的字典‘ages‘,并遍历打印其元素。

集合

        集合(HashSet)是一种无序的唯一元素集合。‘HashSet‘ 类在‘System.Collections.Generic‘ 命名空间中定义。

定义和使用集合

using System;
using System.Collections.Generic;


public class Program
{
    public static void Main(string[] args)
    {
        // 定义一个整数集合
        HashSet<int> uniqueNumbers = new HashSet<int> { 1, 2, 3, 4, 5 };

        // 添加元素
        uniqueNumbers.Add(6);
        uniqueNumbers.Add(1); // 添加重复元素,不会生效

        // 遍历集合
        foreach (int number in uniqueNumbers)
        {
            Console.WriteLine("Element: " + number);
        }
    }
}

        在这个示例中,定义了一个整数集合‘uniqueNumbers‘,并遍历打印其元素。

泛型与泛型集合

        泛型允许我们定义类型参数,从而创建类型安全和高效的集合。泛型提高了代码的可重用性和性能。

定义泛型类

public class Box<T>
{
    private T content;

    public void Add(T item)
    {
        content = item;
    }

    public T Get()
    {
        return content;
    }
}


public class Program
{
    public static void Main(string[] args)
    {
        // 创建一个存储整数的泛型Box实例
        Box<int> intBox = new Box<int>();
        intBox.Add(123);
        Console.WriteLine("Integer Box contains: " + intBox.Get());

        // 创建一个存储字符串的泛型Box实例
        Box<string> strBox = new Box<string>();
        strBox.Add("Hello");
        Console.WriteLine("String Box contains: " + strBox.Get());
    }
}

        在这个示例中,定义了一个泛型类‘Box<T>‘,可以存储任何类型的对象。

泛型集合

        C#中的集合类大多数都有泛型版本,如‘List<T>‘、‘Dictionary<TKey, TValue>‘、‘HashSet<T>‘等。

使用泛型列表

using System;
using System.Collections.Generic;


public class Program
{
    public static void Main(string[] args)
    {
        // 定义一个泛型整数列表
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

        // 添加元素
        numbers.Add(6);

        // 遍历列表
        foreach (int number in numbers)
        {
            Console.WriteLine("Element: " + number);
        }
    }
}

使用泛型字典

using System;
using System.Collections.Generic;


public class Program
{
    public static void Main(string[] args)
    {
        // 定义一个泛型字典
        Dictionary<string, int> ages = new Dictionary<string, int>
        {
            { "Alice", 30 },
            { "Bob", 25 }
        };

        // 遍历字典
        foreach (var kvp in ages)
        {
            Console.WriteLine(kvp.Key + " is " + kvp.Value + " years old.");
        }
    }
}

总结

        通过学习数组与列表、字典与集合以及泛型与泛型集合,您掌握了在C#中管理和操作数据的强大工具。这些集合类型和泛型技术不仅提高了代码的灵活性和效率,还增强了程序的类型安全性。在实际开发中,合理选择和使用这些集合类型,将大大提升程序的性能和可维护性。在接下来的章节中,您将继续深入了解C#的高级特性和编程技巧,以进一步提升您的开发能力。

下一篇:白骑士的C#教学进阶篇 2.3 委托与事件​​​​​​​

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白骑士所长

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值