Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c#集合

作者:你不爱我就滚。   发布日期:2026-05-24   浏览:56

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}");
        }
    }
}

解释说明

  1. List:

    • List<T> 是一个动态数组,可以存储任意数量的相同类型的元素。
    • 使用 Add 方法向集合中添加元素。
    • 使用 foreach 循环遍历集合中的元素。
  2. HashSet:

    • HashSet<T> 是一个无序的集合,不允许重复元素。
    • 使用 Add 方法向集合中添加元素。
    • 使用 foreach 循环遍历集合中的元素。
  3. Dictionary:

    • Dictionary<TKey, TValue> 是键值对的集合,每个键必须是唯一的。
    • 使用 Add 方法向集合中添加键值对。
    • 使用 foreach 循环遍历集合中的键值对,并通过 KeyValue 属性访问键和值。

上一篇:c#socket

下一篇:c# 正则

大家都在看

c# 二进制

c# 创建目录

c# socket服务端连接多个客户端

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

c# invoke方法

.net和c#

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站