Dictionary
来源:互联网 发布:nginx php error log 编辑:程序博客网 时间:2024/04/28 11:44
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
//文字列キーで文字列の新しい辞書を作成する
Dictionary<string, string> openWith = new Dictionary<string, string>();
//いくつかの要素を辞書に追加する。重複するキーはありませんが、値の一部は重複しています。
openWith.Add("txt", "Notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
//新しいキーが既に辞書にある場合は、Addメソッドが例外をスローする。
try
{
openWith.Add("txt", "Winword.exe");
}
catch (ArgumentException)
{
Console.WriteLine("An element with Key = \"txt\" already exists.");
}
Console.WriteLine("For Key =\"rtf\" , value={0}", openWith["rtf"]);
Console.ReadKey();
//インデクサを使用して、キーに関連付けられた値を変更することができる
openWith["rtf"] = "winword.exe";
Console.WriteLine("For Key=\"rtf\",value={0}", openWith["rtf"]);
Console.ReadKey();
//新しいKeyとして追加する、valueはwinword.exe
openWith["doc"] = "winword.exe";
try
{
Console.WriteLine("For Key=\"tif\",value={0}", openWith["tif"]);
}
catch (KeyNotFoundException)
{
Console.WriteLine("key=\"tif\" is not found");
}
try
{
Console.WriteLine("For Key=\"rtf\",value={0}", openWith["rtf"]);
}
catch (KeyNotFoundException)
{
Console.WriteLine("key=\"rtf\" is override");
}
Console.ReadKey();
try
{
Console.WriteLine("For Key=\"doc\",value={0}", openWith["doc"]);
}
catch (KeyNotFoundException)
{
Console.WriteLine("key=\"doc\" is not add");
}
Console.ReadKey();
string value = "";
if (openWith.TryGetValue("tif", out value))
{
Console.WriteLine("For Key=\"tif\",value={0}", value);
}
else
{
Console.WriteLine("Key =\"tif\" is not found");
}
Console.ReadKey();
//ContainsKeyはキーを挿入する前に、キーをテストするために使用できる
if (!openWith.ContainsKey("ht"))
{
openWith.Add("ht", "hypertrm.exe");
Console.WriteLine("value added for key=\"ht\":{0}", openWith["ht"]);
}
Console.ReadKey();
//foreachを使用して、辞書要素を列挙する。要素はKeyValuePairオブジェクトとして取得される
foreach (KeyValuePair<string, string> kvp in openWith)
{
Console.WriteLine("key={0},value={1}", kvp.Key, kvp.Value);
}
Console.ReadKey();
// Valuesプロパティを使用、Valueだけを取得する
Dictionary<string, string>.ValueCollection valueColl = openWith.Values;
foreach (string s in valueColl)
{
Console.WriteLine("Value{0}", s);
}
Console.ReadKey();
//Keysプロパティを使用、キーだけを取得する
Dictionary<string, string>.KeyCollection keyColl = openWith.Keys;
foreach (string s in keyColl)
{
Console.WriteLine("key={0}", s);
}
Console.ReadKey();
//Removeメソッドを使用、Key/Valueのペアを削除する
Console.WriteLine("\n Remove(\"doc\")");
openWith.Remove("doc");
if (!openWith.ContainsKey("doc"))
{
Console.WriteLine("key \"doc\" is not found");
}
Console.ReadKey();
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
//文字列キーで文字列の新しい辞書を作成する
Dictionary<string, string> openWith = new Dictionary<string, string>();
//いくつかの要素を辞書に追加する。重複するキーはありませんが、値の一部は重複しています。
openWith.Add("txt", "Notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
//新しいキーが既に辞書にある場合は、Addメソッドが例外をスローする。
try
{
openWith.Add("txt", "Winword.exe");
}
catch (ArgumentException)
{
Console.WriteLine("An element with Key = \"txt\" already exists.");
}
Console.WriteLine("For Key =\"rtf\" , value={0}", openWith["rtf"]);
Console.ReadKey();
//インデクサを使用して、キーに関連付けられた値を変更することができる
openWith["rtf"] = "winword.exe";
Console.WriteLine("For Key=\"rtf\",value={0}", openWith["rtf"]);
Console.ReadKey();
//新しいKeyとして追加する、valueはwinword.exe
openWith["doc"] = "winword.exe";
try
{
Console.WriteLine("For Key=\"tif\",value={0}", openWith["tif"]);
}
catch (KeyNotFoundException)
{
Console.WriteLine("key=\"tif\" is not found");
}
try
{
Console.WriteLine("For Key=\"rtf\",value={0}", openWith["rtf"]);
}
catch (KeyNotFoundException)
{
Console.WriteLine("key=\"rtf\" is override");
}
Console.ReadKey();
try
{
Console.WriteLine("For Key=\"doc\",value={0}", openWith["doc"]);
}
catch (KeyNotFoundException)
{
Console.WriteLine("key=\"doc\" is not add");
}
Console.ReadKey();
string value = "";
if (openWith.TryGetValue("tif", out value))
{
Console.WriteLine("For Key=\"tif\",value={0}", value);
}
else
{
Console.WriteLine("Key =\"tif\" is not found");
}
Console.ReadKey();
//ContainsKeyはキーを挿入する前に、キーをテストするために使用できる
if (!openWith.ContainsKey("ht"))
{
openWith.Add("ht", "hypertrm.exe");
Console.WriteLine("value added for key=\"ht\":{0}", openWith["ht"]);
}
Console.ReadKey();
//foreachを使用して、辞書要素を列挙する。要素はKeyValuePairオブジェクトとして取得される
foreach (KeyValuePair<string, string> kvp in openWith)
{
Console.WriteLine("key={0},value={1}", kvp.Key, kvp.Value);
}
Console.ReadKey();
// Valuesプロパティを使用、Valueだけを取得する
Dictionary<string, string>.ValueCollection valueColl = openWith.Values;
foreach (string s in valueColl)
{
Console.WriteLine("Value{0}", s);
}
Console.ReadKey();
//Keysプロパティを使用、キーだけを取得する
Dictionary<string, string>.KeyCollection keyColl = openWith.Keys;
foreach (string s in keyColl)
{
Console.WriteLine("key={0}", s);
}
Console.ReadKey();
//Removeメソッドを使用、Key/Valueのペアを削除する
Console.WriteLine("\n Remove(\"doc\")");
openWith.Remove("doc");
if (!openWith.ContainsKey("doc"))
{
Console.WriteLine("key \"doc\" is not found");
}
Console.ReadKey();
}
}
阅读全文
0 0
- Dictionary
- dictionary
- Dictionary
- Dictionary
- dictionary
- Dictionary
- Dictionary
- dictionary
- Dictionary
- Dictionary
- Dictionary
- Dictionary
- Dictionary I
- java dictionary
- scripting.dictionary
- Dictionary 泛型类
- ABAP Dictionary
- online dictionary
- 深入分析Volatile的实现原理
- studio gradle乱码
- Servlet 生命周期、工作原理
- Ajax中的XMLHttpRequest对象详解
- 1.shell脚本入门学习
- Dictionary
- 第一节课: 创建一个MFC工程
- 基于君正T20主控芯片的超低功耗电池IP CAMERA方案
- SQL Server 阻塞 死锁说明与分析
- 数十种TensorFlow实现案例汇集:代码+笔记
- 比较实用的移动端webApp兼容
- 深度学习中的sarsa(lambda)和 Q(lambda)算法
- forward和redirect的区别
- RADIUS and IPv6[frc-3162译文]