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

C# में जांचें कि ArrayList रीड-ओनली है या नहीं

ArrayList.IsReadOnly गुण इंगित करता है कि ArrayList रीड-ओनली है या नहीं। मानक ArrayList इंस्टेंस हमेशा false लौटाते हैं। रीड-ओनली रैपर के लिए ArrayList.ReadOnly का उपयोग करें।

उदाहरण 1: सिंक्रनाइज़्ड और फिक्स्ड साइज रैपर

using System;
using System.Collections;

public class Demo
{
    public static void Main(String[] args)
    {
        ArrayList list1 = new ArrayList();
        list1.Add("ABC");
        list1.Add("BCD");
        list1.Add("CDE");
        list1.Add("DEF");
        list1.Add("EFG");
        list1.Add("GHI");
        list1.Add("HIJ");
        list1.Add("IJK");
        list1.Add("JKL");
        list1.Add("KLM");
        Console.WriteLine("Elements in ArrayList...");
        foreach (string res in list1)
        {
            Console.WriteLine(res);
        }
        ArrayList list = ArrayList.Synchronized(list1);
        Console.WriteLine("Is ArrayList synchronized? = " + list.IsSynchronized);
        ArrayList list2 = ArrayList.FixedSize(list1);
        Console.WriteLine("Is ArrayList have a fixed size? = " + list2.IsFixedSize);
        Console.WriteLine("Is the ArrayList read-only? = " + list1.IsReadOnly);
    }
}

आउटपुट

Elements in ArrayList...
ABC
BCD
CDE
DEF
EFG
GHI
HIJ
IJK
JKL
KLM
Is ArrayList synchronized? = True
Is ArrayList have a fixed size? = True
Is the ArrayList read-only? = False

उदाहरण 2: दो मानक ArrayLists

using System;
using System.Collections;

public class Demo
{
    public static void Main(String[] args)
    {
        ArrayList list1 = new ArrayList();
        list1.Add("A");
        list1.Add("B");
        list1.Add("C");
        list1.Add("D");
        list1.Add("E");
        list1.Add("F");
        Console.WriteLine("Elements in ArrayList1...");
        foreach (string res in list1)
        {
            Console.WriteLine(res);
        }
        Console.WriteLine("Is ArrayList1 synchronized? = " + list1.IsSynchronized);
        Console.WriteLine("Is the ArrayList1 read-only? = " + list1.IsReadOnly);
        ArrayList list2 = new ArrayList();
        list2.Add("A");
        list2.Add("B");
        list2.Add("C");
        list2.Add("D");
        list2.Add("E");
        list2.Add("F");
        Console.WriteLine("Elements in ArrayList2...");
        foreach (string res in list2)
        {
            Console.WriteLine(res);
        }
        Console.WriteLine("Is ArrayList synchronized? = " + list2.IsSynchronized);
        Console.WriteLine("Is the ArrayList2 read-only? = " + list2.IsReadOnly);
    }
}

आउटपुट

Elements in ArrayList1...
A
B
C
D
E
F
Is ArrayList1 synchronized? = False
Is the ArrayList1 read-only? = False
Elements in ArrayList2...
A
B
C
D
E
F
Is ArrayList synchronized? = False
Is the ArrayList2 read-only? = False

मानक ArrayList इंस्टेंस रीड-ओनली नहीं होते (IsReadOnly = false)। ArrayList.ReadOnly(list) एक रीड-ओनली रैपर बनाता है। IsFixedSize और IsSynchronized भी IList इंटरफ़ेस से आते हैं। ArrayList.FixedSize आकार-प्रतिबंधित रैपर बनाता है।

  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# में HashSet में निर्दिष्ट तत्व है या नहीं जांचें

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

  1. - C# में ArrayList क्लास का Capacity गुण क्या है?

    ArrayList क्लास में Capacity गुण उन एलिमेंट्स की संख्या को प्राप्त या सेट करता है जिन्हें ArrayList समाहित कर सकता है।Capacity हमेशा Count से अधिक होती है। Capacity गुण के लिए:arrList.Capacityडिफ़ॉल्ट क्षमता 4 होती है। यदि 5 एलिमेंट्स हैं, तो इसकी क्षमता दोगुनी होकर 8 हो जाएगी। यह क्रम चलता रहता है।