Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Synchronization of ArrayList in C#
Use the ArrayList.Synchronized Method in C# for synchronization of ArrayList in C#.
Let us see an example to lock the collection using SyncRoot property in C# −
ArrayList arr = new ArrayList();
lock(arr.SyncRoot) {
foreach (object ele in arr) {
}
}
The following is the complete example to check the synchronization status of ArrayList −
Example
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList arr1 = new ArrayList();
arr1.Add("One");
arr1.Add("Two");
arr1.Add("Three");
arr1.Add("Four");
arr1.Add("Five");
arr1.Add("Six");
arr1.Add("Seven");
arr1.Add("Eight");
// set synchronized wrapper around the ArrayList
ArrayList arr2 = ArrayList.Synchronized(arr1);
// sychronization status of first ArrayList
Console.WriteLine("arr1 = {0}", arr1.IsSynchronized ? " synchronized" : "not synchronized");
// sychronization status of second ArrayList
Console.WriteLine("arr2 = {0}", arr2.IsSynchronized ? "synchronized" : "not synchronized");
}
}
Output
arr1 = not synchronized arr2 = synchronized
Advertisements