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

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

相关推荐
-To be number.wan29 分钟前
B 树 vs B+ 树:为什么 MySQL 用 B+ 树,而不是 B 树?
数据结构
杨间1 小时前
《排序算法全解析:从基础到优化,一文吃透八大排序!》
c语言·数据结构·排序算法
Remember_9931 小时前
【LeetCode精选算法】滑动窗口专题二
java·开发语言·数据结构·算法·leetcode
Gorgous—l2 小时前
数据结构算法学习:LeetCode热题100-动态规划篇(下)(单词拆分、最长递增子序列、乘积最大子数组、分割等和子集、最长有效括号)
数据结构·学习·算法
呆萌哈士奇2 小时前
告别 throw exception!为什么 Result<T> 才是业务逻辑的正确选择
c#·.net
呉師傅2 小时前
东芝3525AC彩色复印机CC219测试页打印方法【实际操作】
运维·网络·windows·计算机外设·电脑
Remember_9933 小时前
【LeetCode精选算法】滑动窗口专题一
java·数据结构·算法·leetcode·哈希算法
Lueeee.3 小时前
v4l2驱动开发
数据结构·驱动开发·b树
开开心心就好3 小时前
音频编辑工具,多端支持基础剪辑易操作
java·网络·windows·java-ee·电脑·maven·excel
漫随流水3 小时前
leetcode回溯算法(77.组合)
数据结构·算法·leetcode·回溯算法