System.Collections.Generic的各容器类的用法

本文详细介绍了.NET Framework中的System.Collections.Generic命名空间下的各种容器类,包括Dictionary、KeyValuePair、SortedDictionary、SortedList、HashSet、SortedSet、List、Queue和Stack等,阐述了它们的特点和使用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

演示System.Collections.Generic的各容器类的用法.

包括:Dictionary,KeyValuePair,SortedDic tionary,SortedList,HashSet,SortedSet,List,Queue,Stack等

 

  1 System.Collections.Generic.Dictionary<>;       //键/值对集合
  2 System.Collections.Generic.KeyValuePair<>;     //键/值对结构, 作为 Dictionary<> 的一个元素存在
  3 System.Collections.Generic.SortedDictionary<>; //相当于 Key 能自动排序 Dictionary<>
  4 System.Collections.Generic.SortedList<>;       //和 SortedDictionary<> 功能相似, 但内部算法不同, 其 Keys、Values 可通过索引访问
  5 
  6 System.Collections.Generic.HashSet<>;   //无序、无重复的元素集合
  7 System.Collections.Generic.SortedSet<>; //相当于能自动排序的 HashSet<>
  8 System.Collections.Generic.List<>;      //相当于泛型的 ArrayList, 元素可重复、可排序、可插入、可索引访问
  9 
 10 System.Collections.Generic.Queue<>; //队列, 先进先出
 11 System.Collections.Generic.Stack<>; //堆栈, 后进先出
 12 
 13 System.Collections.Generic.LinkedList<>;     //双向链表
 14 System.Collections.Generic.LinkedListNode<>; //LinkedList<> 的节点
 15 
 16 System.Collections.Generic.SynchronizedCollection<>;         //线程安全的集合
 17 System.Collections.Generic.SynchronizedReadOnlyCollection<>; //线程安全的只读集合
 18 System.Collections.Generic.SynchronizedKeyedCollection<>;    //线程安全的键/值集合
 19 
 20 Dictionary<>、KeyValuePair<>:
 21 protected void Button1_Click(object sender, EventArgs e)
 22 {
 23     Dictionary<string, int> dict = new Dictionary<string, int>();
 24     dict.Add("K1", 123);
 25     dict["K2"] = 456;
 26     dict.Add("K3", 789);
 27 
 28     string str = "";
 29     foreach (KeyValuePair<string, int> k in dict)
 30     {
 31         str += string.Format(
using System; using System.Collections.Generic; using System.Drawing; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Newtonsoft.Json; namespace ServerApp { public partial class ServerForm : Form { private TcpListener server; private bool isRunning = false; private List<TcpClient> clients = new List<TcpClient>(); private object lockObject = new object(); private Label[] directionStatusLabels; private Label[] directionCountdownLabels; private TextBox txtLog; private Button btnPeakMode; private Button btnNormalMode; private Button btnEmergencyMode; private Button btnStart; private Button btnStop; public ServerForm() => InitializeUI(); private void InitializeUI() { this.Text = "智能交通管理系统(服务端)"; this.Size = new System.Drawing.Size(1200, 800); // 日志文本框 txtLog = new TextBox { Multiline = true, ScrollBars = ScrollBars.Vertical, Dock = DockStyle.Bottom, ReadOnly = true, BackColor = Color.Black, ForeColor = Color.Lime, Font = new Font("Consolas", 10), Height = 300 }; // 模式按钮面板 Panel buttonPanel = new Panel { Dock = DockStyle.Top, Height = 50 }; btnPeakMode = new Button { Text = "高峰模式", Dock = DockStyle.Left, Width = 120, Font = new Font("微软雅黑", 10) }; btnPeakMode.Click += (s, e) => SendCommandToAll("{\"mode\":\"PEAK\"}"); btnNormalMode = new Button { Text = "平峰模式", Dock = DockStyle.Left, Width = 120, Font = new Font("微软雅黑", 10) }; btnNormalMode.Click += (s, e) => SendCommandToAll("{\"mode\":\"NORMAL\"}"); btnEmergencyMode = new Button { Text = "紧急模式", Dock = DockStyle.Left, Width = 120, Font = new Font("微软雅黑", 10) }; btnEmergencyMode.Click += (s, e) => SendCommandToAll("{\"mode\":\"EMERGENCY\"}"); buttonPanel.Co
04-01
using Liudin.Acs; using Liudin.Mik; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Interop; using static Liudin.Acs.Data.BaseData; namespace Liudin.App.Models.abc { public class Jcassis { public BScheduler JSMS { get { return App.Current.gcScheduler; } set { App.Current.gcScheduler = value; } } public DBBState JSGS { get { return App.Current.gcScheduler.GS; } } public DBCabSet CabSET { get { return App.Current.CurSet.CabSet; } set { App.Current.CurSet.CabSet = value; } } public DBDlySet DlySET { get { return App.Current.CurSet.DlySet; } set { App.Current.CurSet.DlySet = value; } } public DBUixSet UixSET { get { return App.Current.CurSet.UixSet; } set { App.Current.CurSet.UixSet = value; } } public DBIOsSet IosSET { get { return App.Current.IosSet; } set { App.Current.IosSet = value; } } public DBMotSet MotSET { get { return App.Current.MotSet; } set { App.Current.MotSet = value; } } public DBUniSet UniSET { get { return App.Current.UniSet; } set { App.Current.UniSet = value; } } public bool JHomed() { return JSMS.IsMacHomed(); } public bool JSFRun() { return JSMS.IsFSSWork(); } public bool JSWork(bool isHomed = false) { if (!isHomed) { return (JSMS.IsWorking()); } else { return ((JSMS.IsWorking()) && JSMS.IsMacHomed()); } } public bool JLimit(ref string msg) { msg = ""; for (int i = 0; i < AXIS_NUM; ++i) { if (JSGS.bLimitP[i]) { msg = GetAxNm(i) + "超出正极限"; return true; } else if (JSGS.bLimitN[i]) { msg = GetAxNm(i) + "超出负极限"; return true; } } return false; } public string GetAxNm(int no, bool bAddNo = false) { string strName = ""; switch (no) { case 0: strName = ""; break; case 1: strName = "X"; break; case 2: strName = "Y"; break; case 3: strName = "Z"; break; case 4: strName = "A"; break; case 5: strName = "B"; break; default: break; } return strName; } public string GetInNm(int no) { string strName = ""; switch (no) { case 0: strName = "电机"; break; case 1: strName = "电压"; break; case 2: strName = "电流"; break; default: strName = no.ToString(); break; } return strName; } public string GetQnNm(int no) { string strName = ""; switch (no) { case 0: strName = "位置"; break; case 1: strName = "速度"; break; case 2: strName = "加速度"; break; default: strName = no.ToString(); break; } return strName; } } } 如何在其他类直接调用JSMS
04-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值