zoukankan      html  css  js  c++  java
  • C#数组 List、Dictionary 、Arrary、ArrayList

     #region Dictionary  泛型集合,动态修改查询、查询和排序
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add("北京", "1");
                dic.Add("山西", "2");
                dic.Add("深圳", "3");
    
                if (!dic.ContainsKey("山西"))
                {
                    dic.Add("山西", "5");
                }
                foreach (KeyValuePair<string, string> item in dic)
                {
                    Console.WriteLine("Key:{0},Value{1}", item.Key, item.Value);
                }
                //排序遍历
                var result = from d in dic orderby d.Key select d;
                foreach (KeyValuePair<string, string> item in result)
                {
                    Console.WriteLine("Key:{0},Value{1}", item.Key, item.Value);
                }
                #endregion
    

     Arrary:

      #region Arrary 创建固定长度的数组
                string[] str = { "1", "2", "3" };
                                int[] i = { 1, 2, 3, 4 };
                                //取数
                                string[] arr = new string[3];
                                arr[0] = "山西";
                                arr[1] = "广州";
                                arr[2] = "上海";
                                Console.WriteLine("{0}",arr.Length);
                #endregion
    

     ArrayList:

      #region  ArrayList 允许动态增加,移除元素,排序、反转数组  using System.Collections;
    
                ArrayList arraylist = new ArrayList();
                arraylist.Add("山西");
                arraylist.Add("北京");
                Console.WriteLine("长度{0}",arraylist.Count);
    
                arraylist.Remove("北京");
                arraylist.RemoveAt(1);
    
                //反转
                arraylist.Reverse();
                //排序
                arraylist.Sort();
                foreach (string s in arraylist)
                {
                    Console.WriteLine(s);
                }
    
                //允许创建元素为类实例的泛型数组
                ArrayList arrList = new ArrayList();
                arrList.Add(new Person(1, "1"));
                foreach (Person item in arrList)
                {
                    Console.WriteLine("item.Id:{0},item.Name:{1}",item.ID,item.Name);
                }
    
                #endregion
    

     List:

      #region  List  创建泛型集合,动态增加、排序、反转 using System.Collections.Generic;
                List<Person> lists = new List<Person>();
                lists.Add(new Person(1, "2"));
                foreach (Person item in lists)
                {
                    Console.WriteLine("item.Id:{0},item.Name:{1}", item.ID, item.Name);
                }
               #endregion
    
  • 相关阅读:
    mysql常用基本命令
    mysql8.0.13下载与安装图文教程
    k8s ingress 增加跨域配置
    Jenkins 备份恢复插件 thinBackup 使用
    k8s HA master 节点宕机修复
    nginx 跨域问题解决
    mongodb 3.4.24 主从复制
    k8s 线上安装 jenkins并结合 jenkinsfile 实现 helm 自动化部署
    k8s helm 运用与自建helm仓库chartmuseum
    centos6 源码安装 unzip
  • 原文地址:https://www.cnblogs.com/sunliyuan/p/6913541.html
Copyright © 2011-2022 走看看