Dictionary<string, string>是一个泛型使用说明

Posted on 2010-08-05 15:03 moss_tan_jun 阅读(2273) 评论(0) 编辑 收藏

Dictionary<string, string>是一个泛型

他本身有集合的功能有时候可以把它看成数组

他的结构是这样的:Dictionary<[key], [value]>

他的特点是存入对象是需要与[key]值一一对应的存入该泛型

通过某一个一定的[key]去找到对应的值

举个例子:

//实例化对象

Dictionary<int, string> dic = new Dictionary<int, string>();

//对象打点添加

dic.Add(1, "one");

dic.Add(2, "two");

dic.Add(3, "one");

//提取元素的方法

string a = dic[1];

string b = dic[2];

string c = dic[3];

//1、2、3是键,分别对应“one”“two”“one”

//上面代码中分别把值赋给了a,b,c

//注意,键相当于找到对应值的唯一标识,所以不能重复

//但是值可以重复

如果你还看不懂我最后给你举一个通俗的例子

有一缸米,你想在在每一粒上都刻上标记,不重复,相当于“键”当你找的时候一一对应不会找错,这就是这个泛型的键的-作用,而米可以一样,我的意思你明白了吧?

-------------------------------------------------------------------------

c# 对dictionary类进行排序用什么接口实现

如果使用.Net Framework 3.5的话,事情就很简单了。呵呵。

如果不是的话,还是自己写排序吧。

using System;

using System.Collections.Generic;

using System.Text;

using System.Linq;

namespace DictionarySorting

{

class Program

{

static void Main(string[] args)

{

Dictionary<int, string> dic = new Dictionary<int, string>();

dic.Add(1, "HaHa");

dic.Add(5, "HoHo");

dic.Add(3, "HeHe");

dic.Add(2, "HiHi");

dic.Add(4, "HuHu");

var result = from pair in dic orderby pair.Key select pair;

foreach (KeyValuePair<int, string> pair in result)

{

Console.WriteLine("Key:{0}, Value:{1}", pair.Key, pair.Value);

}

Console.ReadKey();

}

}

}

【执行结果】

Key:1, Value:HaHa

Key:2, Value:HiHi

Key:3, Value:HeHe

Key:4, Value:HuHu

Key:5, Value:HoHo

Dictionary的基本用法。假如

需求:现在要导入一批数据,这些数据中有一个称为公司的字段是我们数据库里已经存在了的,目前我们需要把每个公司名字转为ID后才存入数据库。

分析:每导一笔记录的时候,就把要把公司的名字转为公司的ID,这个不应该每次都查询一下数据库的,因为这太耗数据库的性能了。

解决方案:在业务层里先把所有的公司名称及相应的公司ID一次性读取出来,然后存放到一个Key和Value的键值对里,然后实现只要把一个公司的名字传进去,就可以得到此公司相应的公司ID,就像查字典一样。对,我们可以使用字典Dictionary操作这些数据。

示例:SetKeyValue()方法相应于从数据库里读取到了公司信息。

/// <summary>
/// 定义Key为string类型,Value为int类型的一个Dictionary
/// </summary>
/// <returns></returns>
protected Dictionary<string, int> SetKeyValue()
{
Dictionary<string, int> dic = new Dictionary<string, int>();
dic.Add("公司1", 1);
dic.Add("公司2", 2);
dic.Add("公司3", 3);
dic.Add("公司4", 4);
return dic;
}

/// <summary>
/// 得到根据指定的Key行到Value
/// </summary>
protected void GetKeyValue()
{
Dictionary<string, int> myDictionary = SetKeyValue();
//测试得到公司2的值
int directorValue = myDictionary["公司2"];
Response.Write("公司2的value是:" + directorValue.ToString());
}

Dictionary<string, string>是一个泛型使用说明的更多相关文章

  1. 关于 Dictionary<string,string>,和List<T>在View的使用

    在MVC中Dictionary<string,string>如何应用到View页面中呢,例: <input type="text" name=key value= ...

  2. MVC 自定义IModelBinder实现json参数转Dictionary<string, string>

    IModelBinder的学习不算深入,现在用它来实现一个json转Dictionary<string, string> 一.原始json转Dictionary<string, st ...

  3. C#基础总结之五Dictionary<string, string[]>和while循环

    #region 第五天作业 名片集(01) //Dictionary<string, string[]> PersonCard = new Dictionary<string, st ...

  4. ERROR: “System.Web.Mvc.Controller.File(string, string, string)”是一个“方法”

    ERROR: “System.Web.Mvc.Controller.File(string, string, string)”是一个“方法”,这在给定的上下文中无效 这是一个与Controller.F ...

  5. 转:Dictionary<int,string>怎么获取它的值的集合?急!急!急!

    怎么获取Dictionary<int, string>的值?我知道这个是键值对的,我知道可以根据key得到value,但关键是现在连key也不知道啊就是想让这个显示在listbox中,应该 ...

  6. Dictionary<string, string> 排序

    .net framework 2.0 版 Dictionary<string, string> collection = new Dictionary<string, string& ...

  7. QueryString to Dictionary<string, string>

    public class ModelConvertHelper<T> where T : new() {// 此处一定要加上new() public static IList<T&g ...

  8. leetCode题解之寻找string中最后一个word的长度

    1.题目描述 返回一个 string中最后一个单词的长度.单词定义为没有空格的连续的字符,比如 ‘a’,'akkk'. 2.问题分析 从后向前扫描,如果string是以空格‘  ’结尾的,就不用计数, ...

  9. C#将URL中的参数转换成字典Dictionary<string, string>

    /// <summary> /// 将获取的formData存入字典数组 /// </summary> public static Dictionary<String, ...

随机推荐

  1. 安卓app测试之cpu监控

    安卓app测试之cpu监控,如何获取监控的cpu数据呢? 一.通过Dumpsys 来取值 1.adb shell dumpsys cpuinfo 二.top 1.top -d 1|grep packa ...

  2. python3.7实现九九乘法表

    for i in range(1,10): for j in range(1,i+1): print("%d*%d=%d" % (i,j,i*j),end=" " ...

  3. 迷宫问题 POJ - 3984 (搜索输出路径)

    题目大意 题目不需要大意,poj居然还有中文题 鸣谢 特别鸣谢ljc大佬提供的方法!!! 解法 我们可能输出个最短路径的长度比较简单,但是输出最短路径真的是没有做过,这里有一种简单的方法 因为我们的d ...

  4. circumferential averaged streamwise velocity in ParaView

    Goal: get a averaged axial velocity in a circular loop (dashed line in the following figure) Steps: ...

  5. python基础学习 str,list,dict,set,range,enumerate

    一.字符串 s = 'python' s1 = 'python' + 'learn' #相加其实就是简单拼接 s2 = 'python' * 5 #相乘其实就是复制自己多少次,再拼接在一起 字符串切片 ...

  6. phpcms 短信替换

    后台表单向导文件路径: [/www/wwwroot/phpcms/phpcms/modules/formguide/templates/formguide_info_list.tpl.php] pub ...

  7. RNN and Language modeling in TensorFlow

    RNNs and Language modeling in TensorFlow From feed-forward to Recurrent Neural Networks (RNNs) In th ...

  8. Leetcode 95.不同的二叉搜索树II

    不同的二叉搜索树2 给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树. 示例: 输入: 3 输出: [ [1,null,3,2], [3,2,null,1], [3,1,null ...

  9. Linux下清除DNS缓存

    通常有的时候我们通过域名打不开网页,有可能使DNS缓存的原因(DNS解析的ip地址变了),解决办法如下: 方法一:$nslookup ecafe.pub(这里是你要打开的域名) 方法二:$sudo / ...

  10. PostGIS学习相关术语

    POI 兴趣点(英语:point of interest,通常缩写成POI)乃是电子地图上的某个地标.景点,用以标示出该地所代表的政府部门.各行各业之商业机构(加油站.百货公司.超市.餐厅.酒店.便利 ...