学懂C#编程:常用高级技术——学会泛型与集合(一)

发布于:2024-07-03 ⋅ 阅读:(17) ⋅ 点赞:(0)

        C# 中的泛型和集合是两个非常重要的概念,它们极大地增强了代码的灵活性和可重用性。下面我将详细讲解这两个概念。

一、泛型 (Generics)
        泛型允许你在定义类、接口、方法或委托时使用类型参数,从而使这些类型或方法可以在不指定具体类型的情况下工作。泛型提高了代码的重用性,增强了类型安全性,并减少了类型转换的需要。

1、泛型类
示例:

public class GenericClass<T>
{
    private T _value;
 
    public GenericClass(T value)
    {
        _value = value;
    }
 
    public T GetValue()
    {
        return _value;
    }
}
 
class Program
{
    static void Main()
    {
        GenericClass<int> intGeneric = new GenericClass<int>(10);
        Console.WriteLine(intGeneric.GetValue()); // 输出: 10
 
        GenericClass<string> stringGeneric = new GenericClass<string>("Hello");
        Console.WriteLine(stringGeneric.GetValue()); // 输出: Hello
    }
}


2、泛型方法
示例:

public class GenericMethod
{
    public static void Print<T>(T value)
    {
        Console.WriteLine(value);
    }
}
 
class Program
{
    static void Main()
    {
        GenericMethod.Print(10); // 输出: 10
        GenericMethod.Print("Hello"); // 输出: Hello
    }
}

3、泛型约束
你可以通过泛型约束来限制类型参数必须满足的条件。

示例:

public class GenericClassWithConstraint<T> where T : IComparable<T>
{
    private T _value;
 
    public GenericClassWithConstraint(T value)
    {
        _value = value;
    }
 
    public T GetValue()
    {
        return _value;
    }
 
    public bool IsGreaterThan(T other)
    {
        return _value.CompareTo(other) > 0;
    }
}
 
class Program
{
    static void Main()
    {
        GenericClassWithConstraint<int> intGeneric = new GenericClassWithConstraint<int>(10);
        Console.WriteLine(intGeneric.IsGreaterThan(5)); // 输出: True
    }
}

二、集合 (Collections)
        C# 提供了多种集合类型,用于存储和操作一组对象。这些集合类型包括数组、列表、队列、栈、字典等。

1、数组 (Array)
数组是最基本的集合类型,它具有固定大小。

示例:

int[] numbers = new int[5];
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
numbers[4] = 5;
 
foreach (int number in numbers)
{
    Console.WriteLine(number);
}

2、列表 (List)
列表是一种动态数组,它的大小可以动态调整。

示例:

using System.Collections.Generic;
 
List<int> numbers = new List<int>();
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);
numbers.Add(4);
numbers.Add(5);
 
foreach (int number in numbers)
{
    Console.WriteLine(number);
}

3、队列 (Queue)
队列是一种先进先出 (FIFO) 的数据结构。

示例:

using System.Collections.Generic;
 
Queue<int> queue = new Queue<int>();
queue.Enqueue(1);
queue.Enqueue(2);
queue.Enqueue(3);
 
while (queue.Count > 0)
{
    Console.WriteLine(queue.Dequeue());
}


4、栈 (Stack)
栈是一种后进先出 (LIFO) 的数据结构。

示例:

using System.Collections.Generic;
 
Stack<int> stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
stack.Push(3);
 
while (stack.Count > 0)
{
    Console.WriteLine(stack.Pop());
}


5、字典 (Dictionary)
字典是一种键值对集合,可以通过键快速查找值。

示例:

using System.Collections.Generic;
 
Dictionary<string, int> dictionary = new Dictionary<string, int>();
dictionary.Add("one", 1);
dictionary.Add("two", 2);
dictionary.Add("three", 3);
 
foreach (var item in dictionary)
{
    Console.WriteLine($"{item.Key}: {item.Value}");
}


6、泛型集合
泛型集合是 C# 中推荐的集合类型,因为它们提供了类型安全性和性能优势。

示例:

using System.Collections.Generic;
 
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
Queue<string> queue = new Queue<string>();
queue.Enqueue("first");
queue.Enqueue("second");
Stack<double> stack = new Stack<double>();
stack.Push(1.1);
stack.Push(2.2);
Dictionary<int, string> dictionary = new Dictionary<int, string>
{
    { 1, "one" },
    { 2, "two" },
    { 3, "three" }
};
 
foreach (int number in numbers)
{
    Console.WriteLine(number);
}
 
while (queue.Count > 0)
{
    Console.WriteLine(queue.Dequeue());
}
 
while (stack.Count > 0)
{
    Console.WriteLine(stack.Pop());
}
 
foreach (var item in dictionary)
{
    Console.WriteLine($"{item.Key}: {item.Value}");
}

通过这些示例,你可以看到泛型和集合在 C# 中的强大功能和灵活性。它们是现代 C# 编程中不可或缺的工具。


网站公告

今日签到

点亮在社区的每一天
去签到