您的位置:首页 > 产品设计 > UI/UE

foreach (int i in d.Values) Console.Write(i);

2016-07-27 12:09 447 查看
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication8

{

    class Program

    {

        static void Main(string[] args)

        {

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

            d.Add("One", 1);

            d["Two"] = 2; // adds to dictionary because "two" is not already present

            d["Two"] = 22; // updates dictionary because "two" is now present

            d["Three"] = 3;

            Console.WriteLine(d["Two"]); // Prints "22"

            Console.WriteLine(d.ContainsKey("One")); // true (fast operation)

            Console.WriteLine(d.ContainsValue(3)); // true (slow operation)

          

            // Three different ways to enumerate the dictionary:

            foreach (KeyValuePair<string, int> kv in d) // One ; 1

                Console.WriteLine(kv.Key + "; " + kv.Value); // Two ; 22

            // Three ; 3

            foreach (string s in d.Keys) Console.Write(s); // OneTwoThree

            Console.WriteLine();

            foreach (int i in d.Values) Console.Write(i); // 1223

        }

    }

}

/*

22

True

True

One; 1

Two; 22

Three; 3

OneTwoThree

1223请按任意键继续. . .

 */
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: