C# Array SyncRoot

Description

Array SyncRoot gets an object that can be used to synchronize access to the Array.

Syntax

Array.SyncRoot has the following syntax.


public Object SyncRoot { get; }

Example

The following code example shows how to lock an array during the entire enumeration by using the SyncRoot property.


using System;/*  w  w w  .  j  av a2 s.c  o m*/
public class MainClass{
   public static void Main(String[] argv){
        Array myArray = new int[] { 1, 2, 4 };
        lock(myArray.SyncRoot) 
        {
            foreach (Object item in myArray)
                Console.WriteLine(item);
        }
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version