เมธอด SortedDictionary<TKey, TValue>.ContainsValue() ใน C# ใช้ตรวจสอบว่า SortedDictionary มีสมาชิกที่มีค่า (Value) ตรงกับที่ระบุหรือไม่ คืนค่า true หากพบ, false หากไม่พบ
รูปแบบไวยากรณ์ (Syntax)
public bool ContainsValue(TValue value);
value: ค่าที่ต้องการค้นหาใน SortedDictionary
ตัวอย่างที่ 1: ค้นหาค่าที่มีอยู่และเพิ่มข้อมูล
using System;
using System.Collections;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
SortedDictionary sortedDict = new SortedDictionary();
sortedDict.Add(100, "Inspiron");
sortedDict.Add(200, "Alienware");
sortedDict.Add(300, "Projectors");
sortedDict.Add(400, "XPS");
Console.WriteLine("SortedDictionary key-value pairs...");
IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
Console.WriteLine("Count = " + sortedDict.Count);
sortedDict.Add(800, "Notebook");
sortedDict.Add(10000, "Bluetooth Speaker");
Console.WriteLine("\nSortedDictionary key-value pairs...UPDATED");
demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
Console.WriteLine("Count (UPDATED) = " + sortedDict.Count);
// ตรวจสอบค่า
Console.WriteLine("Contains 'Notebook'? = " + sortedDict.ContainsValue("Notebook"));
sortedDict.Clear();
Console.WriteLine("\nCount after Clear = " + sortedDict.Count);
}
}
ผลลัพธ์
SortedDictionary key-value pairs... Key = 100, Value = Inspiron Key = 200, Value = Alienware Key = 300, Value = Projectors Key = 400, Value = XPS Count = 4 SortedDictionary key-value pairs...UPDATED Key = 100, Value = Inspiron Key = 200, Value = Alienware Key = 300, Value = Projectors Key = 400, Value = XPS Key = 800, Value = Notebook Key = 10000, Value = Bluetooth Speaker Count (UPDATED) = 6 Contains 'Notebook'? = True Count after Clear = 0
ตัวอย่างที่ 2: ค้นหาค่าที่ไม่มีอยู่
using System;
using System.Collections;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
SortedDictionary sortedDict = new SortedDictionary();
sortedDict.Add(1, "One");
sortedDict.Add(2, "Two");
sortedDict.Add(3, "Three");
sortedDict.Add(4, "Four");
sortedDict.Add(5, "Five");
sortedDict.Add(6, "Six");
Console.WriteLine("SortedDictionary key-value pairs...");
IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
Console.WriteLine("Count = " + sortedDict.Count);
sortedDict.Add(7, "Seven");
sortedDict.Add(8, "Eight");
Console.WriteLine("\nSortedDictionary key-value pairs...UPDATED");
demoEnum = sortedDict.GetEnumerator();
while (demoEnum.MoveNext())
{
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
Console.WriteLine("Count (UPDATED) = " + sortedDict.Count);
// ค้นหาค่าที่ไม่มี
Console.WriteLine("Contains 'Eleven'? = " + sortedDict.ContainsValue("Eleven"));
sortedDict.Clear();
Console.WriteLine("\nCount after Clear = " + sortedDict.Count);
}
}
ผลลัพธ์
SortedDictionary key-value pairs... Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five Key = 6, Value = Six Count = 6 SortedDictionary key-value pairs...UPDATED Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five Key = 6, Value = Six Key = 7, Value = Seven Key = 8, Value = Eight Count (UPDATED) = 8 Contains 'Eleven'? = False Count after Clear = 0
สรุปและประสิทธิภาพ
- ContainsValue() : O(n) - ต้องสแกนทุกค่า (Linear Search) เพราะ SortedDictionary เรียงตาม Key ไม่ใช่ Value
- ContainsKey() : O(log n) - เร็วมาก เพราะใช้ Binary Search Tree (Red-Black Tree)
- หากต้องการค้นหา Value บ่อย ควรพิจารณาเก็บ Reverse Dictionary (Value → Key) หรือใช้ Lookup
- ใช้ EqualityComparer<TValue>.Default สำหรับการเปรียบเทียบค่า
- คืนค่า true หากพบ อย่างน้อยหนึ่งคีย์ ที่มีค่าตรงกัน (Value อาจซ้ำได้)
ตัวอย่าง: ค้นหา Key จาก Value (Reverse Lookup)
// หา Key ทั้งหมดที่มี Value ตรงกัน
var keys = sortedDict
.Where(kvp => kvp.Value == "Notebook")
.Select(kvp => kvp.Key)
.ToList();
// keys = [800]