HybridDictionary ক্লাসের কনস্ট্রাক্টর HybridDictionary(int initialSize, bool caseInsensitive) ব্যবহার করে কেস-সেনসিটিভিটি নিয়ন্ত্রণ করা যায়। caseInsensitive = false দিয়ে কেস-সেনসিটিভ (Case-Sensitive) ডিকশনারি তৈরি করা যায়, যেখানে "A" এবং "a" আলাদা কী হিসেবে বিবেচিত হয়।
উদাহরণ ১: কেস-সেনসিটিভ HybridDictionary (caseInsensitive = false)
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
// caseInsensitive = false → কেস-সেনসিটিভ
HybridDictionary myDict = new HybridDictionary(5, false);
myDict.Add("A", "AB");
myDict.Add("B", "BC");
myDict.Add("C", "DE");
myDict.Add("D", "FG");
myDict.Add("e", "fg"); // ছোট 'e' - বড় 'E' থেকে আলাদা
Console.WriteLine("কী/ভ্যালু জোড়া (কেস-সেনসিটিভ)...");
foreach (DictionaryEntry de in myDict) {
Console.WriteLine("Key = " + de.Key + ", Value = " + de.Value);
}
}
}
আউটপুট
কী/ভ্যালু জোড়া (কেস-সেনসিটিভ)... Key = A, Value = AB Key = B, Value = BC Key = C, Value = DE Key = D, Value = FG Key = e, Value = fg
উদাহরণ ২: কেস-ইনসেনসিটিভ HybridDictionary (caseInsensitive = true) - ডুপ্লিকেট কী এরর
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
// caseInsensitive = true → কেস-ইনসেনসিটিভ (ডিফল্ট আচরণ)
HybridDictionary myDict = new HybridDictionary(5, true);
myDict.Add("A", "AB");
myDict.Add("B", "BC");
myDict.Add("C", "DE");
myDict.Add("e", "fg"); // ছোট 'e'
myDict.Add("E", "FG"); // বড় 'E' - কেস-ইনসেনসিটিভ মোডে ডুপ্লিকেট!
Console.WriteLine("কী/ভ্যালু জোড়া...");
foreach (DictionaryEntry de in myDict) {
Console.WriteLine("Key = " + de.Key + ", Value = " + de.Value);
}
}
}
আউটপুট (রানটাইম এক্সেপশন)
Run-time exception : An entry with the same key already exists.
Stack Trace: [System.ArgumentException: An entry with the same key already exists.]
at System.Collections.Specialized.ListDictionary.Add(Object key, Object value)
at System.Collections.Specialized.HybridDictionary.Add(Object key, Object value)
at Demo.Main() :line 15
caseInsensitive প্যারামিটারের প্রভাব
| caseInsensitive | আচরণ | উদাহরণ |
|---|---|---|
true (ডিফল্ট) | কেস-ইনসেনসিটিভ: "A" == "a" | Add("A", 1); Add("a", 2) → Exception |
false | কেস-সেনসিটিভ: "A" != "a" | Add("A", 1); Add("a", 2) → OK, দুটি এন্ট্রি |
HybridDictionary কনস্ট্রাক্টর সারসংক্ষেপ
// ১. ডিফল্ট (case-insensitive, initial size 0)
new HybridDictionary()
// ২. নির্দিষ্ট আকার (case-insensitive)
new HybridDictionary(int initialSize)
// ৩. নির্দিষ্ট আকার + কেস-সেনসিটিভিটি
new HybridDictionary(int initialSize, bool caseInsensitive)
// ৪. অন্য ডিকশনারি থেকে কপি (case-insensitive)
new HybridDictionary(IDictionary d)
// ৫. অন্য ডিকশনারি থেকে কপি + কেস-সেনসিটিভিটি
new HybridDictionary(IDictionary d, bool caseInsensitive)
আন্তরিক বাস্তবায়ন বিস্তারিত
- caseInsensitive = true: আন্ডারলাইং
ListDictionary/HashtableCaseInsensitiveComparerবাCaseInsensitiveHashCodeProviderব্যবহার করে। - caseInsensitive = false: ডিফল্ট
Object.Equals/GetHashCodeব্যবহার করে (অর্ডিনাল কম্পারিজন)। - স্যুইচ থ্রেশহোল্ড: ৯ উপাদান পর্যন্ত ListDictionary, ১০ বা তার বেশি হলে Hashtable।
আধুনিক বিকল্প (জেনেরিক, টাইপ-সেফ)
// Dictionary - কেস-সেনসিটিভ (ডিফল্ট Ordinal comparer)
var dict = new Dictionary<string, string>(StringComparer.Ordinal); // Case-sensitive
var dictCI = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); // Case-insensitive
// SortedDictionary - কেস-সেনসিটিভিটি কন্ট্রোল
var sortedDict = new SortedDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
StringComparer অপশনসমূহ
| কম্পারার | কেস-সেনসিটিভ | কলচার |
|---|---|---|
StringComparer.Ordinal | হ্যাঁ | ইনভেরিয়েন্ট (ফাস্ট) |
StringComparer.OrdinalIgnoreCase | না | ইনভেরিয়েন্ট (ফাস্ট) |
StringComparer.CurrentCulture | হ্যাঁ | বর্তমান কালচার |
StringComparer.CurrentCultureIgnoreCase | না | বর্তমান কালচার |
StringComparer.InvariantCulture | হ্যাঁ | ইনভেরিয়েন্ট |
StringComparer.InvariantCultureIgnoreCase | না | ইনভেরিয়েন্ট |
সুপারিশ: নতুন কোডে Dictionary<TKey, TValue> + StringComparer ব্যবহার করুন। HybridDictionary লিগ্যাসি নন-জেনেরিক ক্লাস।