-
C# Dictionary 终极使用方法 (转 http://www.yongfa365.com/Item/Dictionary.html) 武胜
- using System;
- using System.Collections.Generic;
-
- class DictionaryDemo
- {
- static void Main(string[] args)
- {
- DictionaryDemo001();
- Console.ReadLine();
-
- DictionaryDemo002();
- Console.ReadLine();
-
- DictionaryDemo003();
- Console.ReadLine();
- }
-
- /// <summary>
- /// 一般用法
- /// </summary>
- static void DictionaryDemo001()
- {
- Dictionary<int, string> dict = new Dictionary<int, string>();
- dict.Add(1, "111");
- dict.Add(2, "222");
-
- //判断是否存在相应的key并显示
- if (dict.ContainsKey(2))
- {
- Console.WriteLine(dict[2]);
- }
-
- //遍历Keys
- foreach (var item in dict.Keys)
- {
- Console.WriteLine("Key:{0}", item);
- }
-
- //遍历Values
- foreach (var item in dict.Values)
- {
- Console.WriteLine("value:{0}", item);
- }
-
- //遍历整个字典
- foreach (var item in dict)
- {
- Console.WriteLine("key:{0} value:{1}", item.Key, item.Value);
- }
- }
-
- /// <summary>
- /// 参数为其它类型
- /// </summary>
- static void DictionaryDemo002()
- {
- Dictionary<string, string[]> dict = new Dictionary<string, string[]>();
- dict.Add("1", "1,11,111".Split(','));
- dict.Add("2", "2,22,222".Split(','));
- Console.WriteLine(dict["2"][2]);
- }
-
- /// <summary>
- /// 调用自定义类
- /// </summary>
- static void DictionaryDemo003()
- {
- Dictionary<int, yongfa365> dict = new Dictionary<int, yongfa365>();
- for (int i = 0; i < 10; i++)
- {
- yongfa365 y = new yongfa365();
- y.UserCode = i;
- y.UserName = "www.yongfa365.com " + i.ToString();
- dict.Add(i, y);
- }
- foreach (var item in dict)
- {
- Console.WriteLine("{0} One:{1} UserName:{2}", item.Key, item.Value.UserCode, item.Value.UserName);
- }
- }
- }
-
- class yongfa365
- {
- public int UserCode { get; set; }
- public string UserName { get; set; }
-
- }
-
相关阅读:
深度分页,不存在的
netty做客户端实战(二)
netty服务端实战(一)
netty做上位机主动同时连接多个下位机时,接受报文报 java.lang.IndexOutOfBoundsException: srcIndex: 0异常错误
使用线程实现多生产者,多消费者demo
vscode中vue安装,使用的插件
实用的RBAC模型
Springboot+thymeleaf+shiro整合
Log4j作用
SpringBoot整合thymeleaf
-
原文地址:https://www.cnblogs.com/zeroone/p/1700220.html
Copyright © 2020-2023
润新知