C# 数据结构初始化长度

在C#中,各个数据结构的初始化长度是动态的,下面是一个例子,展示了如何初始化各个数据结构并演示它们的长度:

复制代码
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // 初始化数组
        int[] intArray = new int[] { 1, 2, 3, 4, 5 };
        Console.WriteLine($"数组长度: {intArray.Length}");

        // 初始化列表
        List<int> intList = new List<int>() { 1, 2, 3, 4, 5 };
        Console.WriteLine($"列表长度: {intList.Count}");

        // 初始化字典
        Dictionary<string, int> myDictionary = new Dictionary<string, int>()
        {
            {"apple", 1},
            {"banana", 2},
            {"orange", 3}
        };
        Console.WriteLine($"字典长度: {myDictionary.Count}");

        // 初始化集合
        HashSet<int> mySet = new HashSet<int>() { 1, 2, 3, 4, 5 };
        Console.WriteLine($"集合长度: {mySet.Count}");

        // 初始化堆栈
        Stack<int> myStack = new Stack<int>();
        myStack.Push(1);
        myStack.Push(2);
        myStack.Push(3);
        Console.WriteLine($"堆栈长度: {myStack.Count}");

        // 初始化队列
        Queue<string> myQueue = new Queue<string>();
        myQueue.Enqueue("apple");
        myQueue.Enqueue("banana");
        myQueue.Enqueue("orange");
        Console.WriteLine($"队列长度: {myQueue.Count}");
    }
}

这个示例演示了如何初始化各个数据结构,并且打印出它们的长度。在这个示例中,长度是在初始化后动态确定的,而不需要事先指定固定的长度。

相关推荐
硫酸锌018 分钟前
安卓手机连接Windows电脑后通过DHCP服务为手机分配IP地址
android·windows·计算机网络
Cccp.12332 分钟前
【leetcode】(十三)大数据处理
数据结构·算法·leetcode
Pniubi1 小时前
力扣70爬楼梯
数据结构·算法·leetcode
ClickHouseDB2 小时前
ClickHouse .NET 驱动程序新特性:从 1.0 到 1.3 的演进之路
java·前端·windows
csdn_aspnet2 小时前
C# .NetCore Swagger ISchemaFilter Long 转 String 防精度丢失
c#·.netcore·swagger
雪落漂泊3 小时前
C++算法:双指针篇(下)
数据结构·c++·算法
略懂而已13 小时前
腾讯云COS 自动同步 for windows
windows·云计算·腾讯云
学逆向的13 小时前
C 循环结构的执行效率分析
c语言·开发语言·数据结构·网络安全
weixin1997010801615 小时前
《抖音商品API:/product/detail 与 /product/list 的字段边界与图片合规踩坑》(附 Python 源码)
服务器·网络·windows
小杍随笔15 小时前
egui 0.37 升级实录:卡顿修复与5倍性能回退
windows·rust·egui