using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// List 集合示例
List<string> names = new List<string>();
names.Add("Alice");
names.Add("Bob");
names.Add("Charlie");
Console.WriteLine("List 集合中的元素:");
foreach (var name in names)
{
Console.WriteLine(name);
}
// HashSet 集合示例
HashSet<int> numbers = new HashSet<int>();
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);
Console.WriteLine("\nHashSet 集合中的元素:");
foreach (var number in numbers)
{
Console.WriteLine(number);
}
// Dictionary 集合示例
Dictionary<string, int> ages = new Dictionary<string, int>();
ages["Alice"] = 30;
ages["Bob"] = 25;
ages["Charlie"] = 35;
Console.WriteLine("\nDictionary 集合中的元素:");
foreach (var item in ages)
{
Console.WriteLine($"{item.Key}: {item.Value}");
}
}
}
List:
List<T> 是一个动态数组,可以存储任意数量的相同类型的元素。Add 方法向集合中添加元素。foreach 循环遍历集合中的元素。HashSet:
HashSet<T> 是一个无序的集合,不允许重复元素。Add 方法向集合中添加元素。foreach 循环遍历集合中的元素。Dictionary:
Dictionary<TKey, TValue> 是键值对的集合,每个键必须是唯一的。Add 方法向集合中添加键值对。foreach 循环遍历集合中的键值对,并通过 Key 和 Value 属性访问键和值。上一篇:c#socket
下一篇:c# 正则
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站