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

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

相关推荐
程序猿多布23 分钟前
使用Visual Studio将C#程序发布为.exe文件
c#·visual studio
老衲有点帅2 小时前
C#多线程Thread
开发语言·c#
王RuaRua3 小时前
[数据结构]5. 栈-Stack
linux·数据结构·数据库·链表
PascalMing3 小时前
C# 通过脚本实现接口
c#·codeanalysis·接口派生
一只鱼^_3 小时前
牛客练习赛138(首篇万字题解???)
数据结构·c++·算法·贪心算法·动态规划·广度优先·图搜索算法
寒小松4 小时前
Problem E: List练习
java·数据结构·list
清幽竹客5 小时前
redis数据结构-02(INCR、DECR、APPEND)
数据结构·redis
Akiiiira5 小时前
【数据结构】线性表
数据结构
小狗祈祷诗5 小时前
day20-线性表(链表II)
c语言·数据结构·链表
普通的冒险者5 小时前
几个简单的数组小练习(适合初学)
java·数据结构