ใช้ ConveyValue() ใน C# เพื่อค้นหาค่าในพจนานุกรม
สร้างพจนานุกรมและเพิ่มองค์ประกอบ -
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("keyboard", "One");
d.Add("mouse", "Two"); ตอนนี้ ใช้เมธอดContainsValue() เพื่อค้นหาค่าเฉพาะ -
d.ContainsValue("Two"); ต่อไปนี้เป็นรหัสที่สมบูรณ์ -
ตัวอย่าง
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("keyboard", "One");
d.Add("mouse", "Two");
// search for a value
Console.WriteLine(d.ContainsValue("Two"));
}
} ผลลัพธ์
True