您的位置:首页 > 编程语言 > C#

C# 不分大小写的 Dictionary<string, string>

2014-07-10 11:18 465 查看
using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//我以前都這麼處理不分大小寫的Hashtable
Dictionary<string, int> sillyBoy =
new Dictionary<string, int>();
//新增時將所有Key強迫轉小寫
sillyBoy.Add("Darkthread".ToLower(), 9999);
//比較時也強迫轉小寫就好了呀
if (sillyBoy.ContainsKey("DARKTHREAD".ToLower()))
Console.WriteLine("阿母啊! 哇行公啊~~");
//當時覺得我好會想辦法解問題,沒想到自己根本是
//笨小孩笨小孩笨小孩笨小孩笨小孩笨小孩笨小孩笨小孩

//原來,Dictionary<T, T>的建構式可傳入Comparer當參數
Dictionary<string, int> dict =
new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
dict.Add("Darkthread", 32767);
Console.WriteLine(dict["DARKTHREAD"]);
Console.WriteLine(dict["darkthread"]);
//是的,醬子就可以搞定。我以後不會再阿呆了
Console.Read();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: