Sử dụng phương thức ContainsValue () trong C # để tìm kiếm giá trị trong Từ điển.
Tạo Từ điển và thêm các phần tử -
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("keyboard", "One");
d.Add("mouse", "Two"); Bây giờ, hãy sử dụng phương thức ContainsValue () để tìm một giá trị cụ thể -
d.ContainsValue("Two"); Sau đây là mã hoàn chỉnh -
Ví dụ
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"));
}
} Đầu ra
True