Visual C Sharp syntax

2012-04-28 8:26 pm
Hello everyone,
private static Dictionary< string, string > IDNumber; Does that syntax is use to store IDNumber with 2 strings or 2 values? Ture or False?If I have 6 values need to store, which syntax I should use and how to use?Would someone provide some examples?Thanks.

回答 (2)

2012-04-29 4:21 pm
✔ 最佳答案
using System;
using System.Collections.Generic;

/*
** Dictionary provides very fast LOOKUPS with keys to get values.
** It is used when you have MANY different elements and not limited to 6.
** The key value is unique key in the data structure.
*/
public class Program
{
public static void Main()
{
Dictionary<string, string> IDNumber = new Dictionary<string, string>()
{
{"1", "one"},
{"2", "two"},
{"3", "three"}
};

// Prefer...
if (!IDNumber.ContainsKey("4")) IDNumber.Add("4", "four");
if (!IDNumber.ContainsKey("4")) IDNumber.Add("4", "four"); // ignore

foreach(KeyValuePair<string, string> pair in IDNumber)
{
Console.WriteLine("Key: {0}, Value: {1}", pair.Key, pair.Value);
}

Console.ReadKey();
}
}

2012-04-29 08:31:17 補充:
// Usage...
Console.WriteLine(IDNumber["4"]);
2012-04-29 6:38 am
The syntax define a data structure that holds an unlimited string->string pairs. It doesn't limit to just 2 pairs.

So if you have 6 values to store, the syntax is just the same.


收錄日期: 2021-04-13 18:39:24
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20120428000051KK00290

檢視 Wayback Machine 備份