.NET ez 2012-06-04
如何將 Dictionary 裡的資料列舉出來,使用 KeyValuePair。
using System;
using System.Collections.Generic;
namespace Test
{
class Program
{
static Dictionary<string, string> D = new Dictionary<string, string>();
/// <summary>
/// 產生測試資料
/// </summary>
static void testdata()
{
int num = 1000000;
for (int i = 0; i < num; i ++)
{
D.Add((i * 2).ToString(), (i * 2).ToString());
}
}
static void Main(string[] args)
{
//產生測試資料
testdata();
//列舉資料
foreach (KeyValuePair<string, string> Items in D)
{
Console.WriteLine(String.Format("key: {0} \t value: {1}", Items.Key, Items.Value));
}
Console.ReadLine();
}
}
}如何將 Dictionary 的 Keys 轉換出來,以下提供兩種方法:
using System;
using System.Collections.Generic;
namespace Test
{
class Program
{
static Dictionary<string, string> D = new Dictionary<string, string>();
/// <summary>
/// 產生測試資料
/// </summary>
static void testdata()
{
int num = 1000000;
for (int i = 0; i < num; i ++)
{
D.Add((i * 2).ToString(), (i * 2).ToString());
}
}
static void Main(string[] args)
{
//產生測試資料
testdata();
List<string> K0 = new List<string>(D.Keys); //方法1
//or
Dictionary<string, string>.KeyCollection K1 = D.Keys; //方法2
Console.ReadLine();
}
}
}標籤: .NET
本文章網址:
https://www.ez2o.com/Blog/Post/csharp-Dictionary-KeyValuePair
https://www.ez2o.com/Blog/Post/61
https://www.ez2o.com/Blog/Post/csharp-Dictionary-KeyValuePair
https://www.ez2o.com/Blog/Post/61
