কম্পিউটার টিউটোরিয়াল

C#-এ OrderedDictionary-এ মানগুলির ICollection পাওয়া (Values প্রপার্টি)


C#-এ OrderedDictionary.Values প্রপার্টি একটি ICollection রিটার্ন করে যাতে OrderedDictionary-এ থাকা সব মান (values) থাকে। OrderedDictionary কি-ভ্যালু পেয়ারের ক্রম বজায় রাখে (insertion order)।

উদাহরণ ১: মানগুলো কপি করে অ্যারে-এ প্রদর্শন

using System; 
using System.Collections; 
using System.Collections.Specialized; 
public class Demo { 
    public static void Main(){ 
        OrderedDictionary dict = new OrderedDictionary(); 
        dict.Add("1", "One"); 
        dict.Add("2", "Two"); 
        dict.Add("3", "Three"); 
        dict.Add("4", "Four"); 
        dict.Add("5", "Five"); 
        dict.Add("6", "Six"); 
        dict.Add("7", "Seven"); 
        dict.Add("8", "Eight"); 
        ICollection col = dict.Values; 
        String[] strVal = new String[dict.Count]; 
        col.CopyTo(strVal, 0); 
        Console.WriteLine("Displaying the values..."); 
        for (int i = 0; i < dict.Count; i++) { 
            Console.WriteLine(strVal[i]); 
        } 
    } 
}

আউটপুট

Displaying the values... 
One 
Two 
Three 
Four 
Five 
Six 
Seven 
Eight

উদাহরণ ২: ভিন্ন কি-ভ্যালু পেয়ারের সাথে মানগুলো প্রদর্শন

using System; 
using System.Collections; 
using System.Collections.Specialized; 
public class Demo { 
    public static void Main(){ 
        OrderedDictionary dict = new OrderedDictionary(); 
        dict.Add("One", "John"); 
        dict.Add("Two", "Tim"); 
        dict.Add("Three", "Katie"); 
        dict.Add("Four", "Andy"); 
        dict.Add("Five", "Gary"); 
        dict.Add("Six", "Amy"); 
        ICollection col = dict.Values; 
        String[] strVal = new String[dict.Count]; 
        col.CopyTo(strVal, 0); 
        Console.WriteLine("Displaying the Values..."); 
        for (int i = 0; i < dict.Count; i++) { 
            Console.WriteLine(strVal[i]); 
        } 
    } 
}

আউটপুট

Displaying the Values... 
John 
Tim 
Katie 
Andy 
Gary 
Amy

OrderedDictionary-এর অন্যান্য গুরুত্বপূর্ণ সদস্য

সদস্যবিবরণ
Keysসব কি (key)-এর ICollection রিটার্ন করে
Valuesসব মান (value)-এর ICollection রিটার্ন করে
[key]নির্দিষ্ট কির মান পাওয়া/সেট করা (ইন্ডেক্সার)
Add(key, value)নতুন কি-ভ্যালু পেয়ার যোগ করে
Remove(key)নির্দিষ্ট কির এন্ট্রি সরিয়ে দেয়
Clear()সব এন্ট্রি সরিয়ে দেয়

মূল পয়েন্টস

  • Values প্রপার্টি একটি স্ন্যাপশট (snapshot) রিটার্ন করে - ডিকশনারি পরিবর্তন করলে কালেকশন আপডেট হয় না।
  • CopyTo() মেথড ব্যবহার করে অ্যারে-এ কপি করা যায়।
  • OrderedDictionary System.Collections.Specialized নেমস্পেসে থাকে।
  • জেনেরিক বিকল্প হিসেবে Dictionary<TKey, TValue> (অনর্ডার্ড) বা কাস্টম ইমপ্লিমেন্টেশন ব্যবহার করা যায়।
  1. C#-তে ListDictionary-র ভ্যালুগুলোর ICollection পেতে Values প্রোপার্টি ব্যবহার

  2. C#-তে OrderedDictionary-র শুধুমাত্র পঠনযোগ্য (Read-Only) কপি পেতে AsReadOnly() মেথড ব্যবহার

  3. C#-এ StringDictionary-র মান সংগ্রহ পান

  4. C#-এ লিস্ট থেকে রেঞ্জ অনুযায়ী উপাদান পাওয়া