Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C#

C# में Collection में तत्वों की संख्या प्राप्त करें

Collection<T> वर्ग Count गुण प्रदान करता है जो संग्रह में निहित तत्वों की संख्या लौटाता है। यह ICollection<T> इंटरफ़ेस का हिस्सा है।

उदाहरण 1: स्ट्रिंग संग्रह

using System;
using System.Collections.ObjectModel;

public class Demo
{
    public static void Main()
    {
        Collection<string> col = new Collection<string>();
        col.Add("Andy");
        col.Add("Kevin");
        col.Add("John");
        col.Add("Kevin");
        col.Add("Mary");
        col.Add("Katie");
        col.Add("Barry");
        col.Add("Nathan");
        col.Add("Mark");
        Console.WriteLine("Count of elements = " + col.Count);
        Console.WriteLine("Iterating through the collection...");
        var enumerator = col.GetEnumerator();
        while (enumerator.MoveNext())
        {
            Console.WriteLine(enumerator.Current);
        }
    }
}

आउटपुट

Count of elements = 9
Iterating through the collection...
Andy
Kevin
John
Kevin
Mary
Katie
Barry
Nathan
Mark

उदाहरण 2: पूर्णांक संग्रह के साथ Contains और Clear

using System;
using System.Collections.ObjectModel;

public class Demo
{
    public static void Main()
    {
        Collection<int> col = new Collection<int>();
        col.Add(10);
        col.Add(20);
        col.Add(30);
        col.Add(40);
        col.Add(50);
        col.Add(60);
        col.Add(70);
        col.Add(80);
        Console.WriteLine("Elements in the Collection...");
        foreach (int val in col)
        {
            Console.WriteLine(val);
        }
        Console.WriteLine("Does the collection has the element 70? = " + col.Contains(70));
        Console.WriteLine("Count of elements = " + col.Count);
        col.Clear();
        Console.WriteLine("Count of elements (updated) = " + col.Count);
    }
}

आउटपुट

Elements in the Collection...
10
20
30
40
50
60
70
80
Does the collection has the element 70? = True
Count of elements = 8
Count of elements (updated) = 0

Collection<T> एक आधार वर्ग है जिसका उपयोग कस्टम संग्रह बनाने के लिए किया जा सकता है। यह Count, Add, Remove, Clear, Contains और इंडेक्सर एक्सेस जैसी मानक संग्रह कार्यक्षमता प्रदान करता है।

  1. - C# में Collection से सभी तत्व हटाएं

    C# में Collection<T> वर्ग की Clear() विधि का उपयोग करके संग्रह से सभी तत्व एक साथ हटाए जा सकते हैं। यह विधि संग्रह को खाली कर देती है और Count प्रॉपर्टी को 0 पर सेट कर देती है। उदाहरण 1: पूर्णांक Collection साफ करना using System; using System.Collections.ObjectModel; public class Demo {

  1. - C# में Collection में किसी तत्व की जांच कैसे करें

    C# में Collection<T> किसी विशिष्ट तत्व को रखता है या नहीं, यह जांचने के लिए Contains() मेथड का उपयोग किया जाता है। यह मेथड एक बूलियन मान लौटाता है। उदाहरण 1 using System; using System.Collections.ObjectModel; public class Demo { public static void Main() { Collection col = new Col

  1. - C# में List.GetRange() से तत्वों की रेंज प्राप्त करना

    C# में List<T>.GetRange() मेथड का उपयोग करके सूची के एक सन्निकट (contiguous) भाग को एक नई सूची के रूप में प्राप्त किया जा सकता है। यह ArraySegment या LINQ के Skip()/Take() के समान है लेकिन सूची-विशिष्ट है। उदाहरण using System; using System.Collections.Generic; public class Demo { public