- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get Synchronize access to an Array in C#?
To get synchronize access to an array, the code is as follows −
Example
using System; public class Demo { public static void Main() { Array intArr = new int[] {5, 10, 15, 20, 25, 30, 35, 40 }; Console.WriteLine("Integer array..."); foreach (int i in intArr) Console.WriteLine(i); Console.WriteLine("After applying lock on array..."); lock(intArr.SyncRoot) { foreach (Object ob in intArr) Console.WriteLine(ob); } } }
Output
This will produce the following output −
Integer array... 5 10 15 20 25 30 35 40 After applying lock on array... 5 10 15 20 25 30 35 40
Example
Let us see another example −
using System; public class Demo { public static void Main() { Array strArr = new String[] {"Harry", "Tom", "Kevin", "Ryan", "Katie", "Amy" }; Console.WriteLine("String array..."); foreach (string i in strArr) Console.WriteLine(i); Console.WriteLine("After applying lock on array..."); lock(strArr.SyncRoot) { foreach (Object ob in strArr) Console.WriteLine(ob); } } }
Output
This will produce the following output −
String array... Harry Tom Kevin Ryan Katie Amy After applying lock on array... Harry Tom Kevin Ryan Katie Amy
- Related Articles
- How to get Synchronize access to the StringCollection in C#
- How to get Synchronize access to the StringDictionary in C#
- How to get Synchronize access to the ListDictionary in C#?
- How to get Synchronize access to the HybridDictionary in C#?
- How to get Synchronize access to the Stack in C#?
- How to access elements from an array in C#?
- How to access an array element in C language?
- How to synchronize an ArrayList in Java?
- How to access elements of an array using pointer notation in C#?
- How many ways to synchronize an ArrayList in Java?
- How to access elements from jagged array in C#?
- MongoDB query to access an object in an array
- C++ Program to Access Elements of an Array Using Pointer
- How to declare, create, initialize and access an array in Java?
- How to access properties of an array of objects in JavaScript?

Advertisements