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

C#-তে Hashtable-এর উপাদানগুলি অ্যারে ইনস্ট্যান্সে কপি করা

Hashtable.CopyTo() মেথড ব্যবহার করে হ্যাশটেবিলের সব কী/মান জোড়া একটি DictionaryEntry অ্যারেতে কপি করা যায়। লক্ষ্য অ্যারেতে নির্দিষ্ট সূচক থেকে কপি শুরু করা সম্ভব।

উদাহরণ ১: সম্পূর্ণ কপি (সূচক ০)

using System;
using System.Collections;

public class Demo {
    public static void Main() {
        Hashtable hash = new Hashtable();
        hash.Add("1", "AB");
        hash.Add("2", "CD");
        hash.Add("3", "EF");
        hash.Add("4", "GH");
        hash.Add("5", "IJ");
        
        Console.WriteLine("Hashtable কী এবং মান জোড়া...");
        foreach(DictionaryEntry entry in hash) {
            Console.WriteLine("{0} and {1} ", entry.Key, entry.Value);
        }
        
        Console.WriteLine("অ্যারে ইনস্ট্যান্সে কপি করা হলো...");
        DictionaryEntry[] dictArr = new DictionaryEntry[hash.Count];
        hash.CopyTo(dictArr, 0);
        
        for (int i = 0; i < dictArr.Length; i++) {
            Console.WriteLine("Key = " + dictArr[i].Key + ", Value = " + dictArr[i].Value);
        }
    }
}

আউটপুট

Hashtable কী এবং মান জোড়া...
1 and AB
2 and CD
3 and EF
4 and GH
5 and IJ

অ্যারে ইনস্ট্যান্সে কপি করা হলো...
Key = 1, Value = AB
Key = 2, Value = CD
Key = 3, Value = EF
Key = 4, Value = GH
Key = 5, Value = IJ

উদাহরণ ২: অফসেট ২ সহ কপি (অ্যারে আকার ৫)

using System;
using System.Collections;

public class Demo {
    public static void Main() {
        Hashtable hash = new Hashtable(5);
        hash.Add("1", "AB");
        hash.Add("2", "CD");
        
        Console.WriteLine("Hashtable কী এবং মান জোড়া...");
        foreach(DictionaryEntry entry in hash) {
            Console.WriteLine("{0} and {1} ", entry.Key, entry.Value);
        }
        
        Console.WriteLine("অ্যারে ইনস্ট্যান্সে কপি করা হলো...");
        DictionaryEntry[] dictArr = new DictionaryEntry[5];
        hash.CopyTo(dictArr, 2); // সূচক ২ থেকে শুরু
        
        for (int i = 0; i < dictArr.Length; i++) {
            Console.WriteLine("Key = " + dictArr[i].Key + ", Value = " + dictArr[i].Value);
        }
    }
}

আউটপুট

Hashtable কী এবং মান জোড়া...
1 and AB
2 and CD

অ্যারে ইনস্ট্যান্সে কপি করা হলো...
Key = , Value = 
Key = , Value = 
Key = 1, Value = AB
Key = 2, Value = CD
Key = , Value = 

দ্রষ্টব্য: Hashtable-এর ক্রম অনিশ্চিত (non-deterministic), তাই কপি করার সময় ক্রম ভিন্ন হতে পারে। আধুনিক কোডে Dictionary<TKey, TValue> ব্যবহার করা ভালো।

  1. C#-এ একটি ইন্টিজার অ্যারের ধনাত্মক উপাদানগুলিকে নিগেটিভ করুন

  2. সি শার্প প্রোগ্রাম: অ্যরেতে শেষ থেকে উপাদান বাদ দেওয়া

  3. C#-এ জ্যাগড অ্যারে (Jagged Array) এর এলিমেন্টের টাইপ

  4. C#-এ ইন্টিজার অ্যারের গড় বের করা