ArrayList के लिए एक सिंक्रोनाइज़्ड रैपर बनाने के लिए, कोड इस प्रकार है -
उदाहरण
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList arrList = new ArrayList();
arrList.Add("AB");
arrList.Add("CD");
arrList.Add("EF");
arrList.Add("GH");
arrList.Add("IJ");
arrList.Add("KL");
Console.WriteLine("ArrayList elements...");
foreach(string str in arrList) {
Console.WriteLine(str);
}
Console.WriteLine("ArrayList is synchronized? = "+arrList.IsSynchronized);
}
} आउटपुट
यह निम्नलिखित आउटपुट देगा -
ArrayList elements... AB CD EF GH IJ KL ArrayList is synchronized? = False
उदाहरण
आइए एक और उदाहरण देखें -
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList arrList = new ArrayList();
arrList.Add("AB");
arrList.Add("CD");
arrList.Add("EF");
arrList.Add("GH");
arrList.Add("IJ");
arrList.Add("KL");
Console.WriteLine("ArrayList elements...");
foreach(string str in arrList) {
Console.WriteLine(str);
}
Console.WriteLine("ArrayList is synchronized? = "+arrList.IsSynchronized);
ArrayList arrList2 = ArrayList.Synchronized(arrList);
Console.WriteLine("ArrayList is synchronized? = "+arrList2.IsSynchronized);
}
} आउटपुट
यह निम्नलिखित आउटपुट देगा -
ArrayList elements... AB CD EF GH IJ KL ArrayList is synchronized? = False ArrayList is synchronized? = True