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

C#-তে SortedList-র সব উপাদান সরাতে Clear() মেথড ব্যবহার

SortedList ক্লাস (namespace: System.Collections) একটি সর্টেড কী-ভ্যালু সংগ্রহ যা কী অনুযায়ী স্বয়ংক্রিয় সর্টেড থাকে। এর Clear() মেথড সব কী-ভ্যালু জোড়া একসাথে সরিয়ে দেয় এবং Count শূন্য করে দেয়।

উদাহরণ ১: বড় SortedList Clear()

using System;
using System.Collections;

public class Demo {
    public static void Main(string[] args) {
        SortedList sortedList = new SortedList();
        sortedList.Add("A", "1");
        sortedList.Add("B", "2");
        sortedList.Add("C", "3");
        sortedList.Add("D", "4");
        sortedList.Add("E", "5");
        sortedList.Add("F", "6");
        sortedList.Add("G", "7");
        sortedList.Add("H", "8");
        sortedList.Add("I", "9");
        sortedList.Add("J", "10");

        Console.WriteLine("SortedList উপাদানসমূহ...");
        foreach (DictionaryEntry d in sortedList) {
            Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
        }

        Console.WriteLine("SortedList কী-ভ্যালু জোড়া সংখ্যা = " + sortedList.Count);  // ১০

        // সব উপাদান সরানো
        sortedList.Clear();

        Console.WriteLine("SortedList-র কী-ভ্যালু জোড়া সংখ্যা (আপডেট) = " + sortedList.Count);  // ০
    }
}

আউটপুট

SortedList উপাদানসমূহ...
Key = A, Value = 1
Key = B, Value = 2
Key = C, Value = 3
Key = D, Value = 4
Key = E, Value = 5
Key = F, Value = 6
Key = G, Value = 7
Key = H, Value = 8
Key = I, Value = 9
Key = J, Value = 10
SortedList কী-ভ্যালু জোড়া সংখ্যা = 10
SortedList-র কী-ভ্যালু জোড়া সংখ্যা (আপডেট) = 0

উদাহরণ ২: ছোট SortedList Clear()

using System;
using System.Collections;

public class Demo {
    public static void Main(string[] args) {
        SortedList sortedList = new SortedList();
        sortedList.Add("A", "1");
        sortedList.Add("B", "2");
        sortedList.Add("C", "3");

        Console.WriteLine("SortedList কী-ভ্যালু জোড়া সংখ্যা = " + sortedList.Count);  // ৩

        Console.WriteLine("SortedList উপাদানসমূহ...");
        foreach (DictionaryEntry d in sortedList) {
            Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
        }

        sortedList.Clear();

        Console.WriteLine("SortedList কী-ভ্যালু জোড়া সংখ্যা (আপডেট) = " + sortedList.Count);  // ০
    }
}

আউটপুট

SortedList কী-ভ্যালু জোড়া সংখ্যা = 3
SortedList উপাদানসমূহ...
Key = A, Value = 1
Key = B, Value = 2
Key = C, Value = 3
SortedList কী-ভ্যালু জোড়া সংখ্যা (আপডেট) = 0

SortedList-এর মূল বৈশিষ্ট্য

  • সর্টেড: কী অনুযায়ী স্বয়ংক্রিয় সর্টেড (IComparer ব্যবহার করে)।
  • ইন্ডেক্স অ্যাক্সেস: GetKey(index), GetByIndex(index), [index] সাপোর্ট করে।
  • দুই অ্যারে: আন্ডারলাইংলে দুটি অ্যারে (keys[], values[]) রক্ষা করে।
  • নন-জেনেরিক: object কী এবং ভ্যালু (বক্সিং/আনবক্সিং ওভারহেড)।
  • পারফরম্যান্স: Insert/Remove O(n) - অ্যারে শিফটিং দরকার।

SortedList vs SortedDictionary vs Dictionary

বৈশিষ্ট্যSortedListSortedDictionaryDictionary
নেমস্পেসSystem.CollectionsSystem.Collections.GenericSystem.Collections.Generic
টাইপ সেফটিনন-জেনেরিকজেনেরিকজেনেরিক
সর্টেডহ্যাঁহ্যাঁনা
ইন্ডেক্স অ্যাক্সেসহ্যাঁ O(1)নানা
ইনসারশন/ডিলিশনO(n)O(log n)O(1) গড়ে
মেমরিকম (অ্যারে)উচ্চ (ট্রি নোডস)মাঝারি

আধুনিক জেনেরিক সংস্করণ: SortedList<TKey, TValue>

using System.Collections.Generic;

var sortedList = new SortedList<string, int>();
sortedList.Add("A", 1);
sortedList.Add("B", 2);
// ...
sortedList.Clear();  // একই আচরণ, টাইপ-সেফ

সুপারিশ: নতুন কোডে SortedList<TKey, TValue> (জেনেরিক) ব্যবহার করুন। নন-জেনেরিক SortedList লিগ্যাসি কোডের জন্য।

  1. জাভাস্ক্রিপ্টে কিউ (Queue) থেকে ডিকিউ (Dequeue) অপারেশন

  2. C#-এ লিস্ট থেকে ডুপ্লিকেট এলিমেন্ট সরানো

  3. C# অ্যারে থেকে এলিমেন্ট ডিলিট/রিমুভ কীভাবে করবেন?

  4. জাভাতে ArrayList-এ থেকে সব এলিমেন্ট রিমূভ করা