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

C#-এ SortedList-এ উপাদানের সংখ্যা পান

SortedList ক্লাসের Count প্রপার্টি কী-মান জোড়ার মোট সংখ্যা রিটার্ন করে। এটি O(1) অপারেশন। 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

উদাহরণ ২: সংখ্যা এবং এনিউমারেটর

using System;
using System.Collections;

public class Demo {
    public static void Main(String[] args){
        SortedList sortedList = new SortedList();
        sortedList.Add("One", "1");
        sortedList.Add("Two", "2");
        sortedList.Add("Three", "3");
        sortedList.Add("Four", "4");
        sortedList.Add("Five", "5");
        sortedList.Add("Six", "6");
        sortedList.Add("Seven", "7");
        sortedList.Add("Eight", "8");
        sortedList.Add("Nine", "9");
        sortedList.Add("Ten", "10");
        
        Console.WriteLine("SortedList কী-মান জোড়ার সংখ্যা = " + sortedList.Count);
        Console.WriteLine("SortedList-এ পুনরাবৃত্তি করার জন্য এনিউমারেটর...");
        IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
        while (demoEnum.MoveNext())
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
    }
}

আউটপুট

SortedList কী-মান জোড়ার সংখ্যা = 10
SortedList-এ পুনরাবৃত্তি করার জন্য এনিউমারেটর...
Key = Eight, Value = 8
Key = Five, Value = 5
Key = Four, Value = 4
Key = Nine, Value = 9
Key = One, Value = 1
Key = Seven, Value = 7
Key = Six, Value = 6
Key = Ten, Value = 10
Key = Three, Value = 3
Key = Two, Value = 2

তুলনা: SortedList অ্যারে-ভিত্তিক, SortedDictionary ট্রি-ভিত্তিক। ছোট ডেটা এবং কম ইনসারশন/ডিলিশনে SortedList দ্রুত।

  1. C#-এ StringCollection-র স্ট্রিং সংখ্যা পাওয়া

  2. C#-তে SortedList-এর মাধ্যমে পুনরাবৃত্তি করার জন্য Enumerator পেতে GetEnumerator() ব্যবহার

  3. সি শার্পে ফাইলের বাইট সংখ্যা পাওয়ার পদ্ধতি

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