
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
Array SyncRoot Property in C#
The Array.SyncRoot property is used to get an object that can be used to synchronize access to the Array. The classes that have arrays can also use the SyncRoot property to implement their own synchronization.
Enumerating through a collection is not a thread safe procedure. The other threads may modify the collection even when the collection is synchronized. This would eventually cause the enumerator to throw an exception. For this, you need to lock the collection.
Let us see an example to work with Array.SyncRoot property −
Example
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { Array arr = new int[] { 23, 11, 32, 18, 87 }; lock(arr.SyncRoot) { foreach (Object val in arr) Console.WriteLine(val); } } }
Output
23 11 32 18 87
Above, we have set a lock on the array −
lock(arr.SyncRoot)
Advertisements