- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# - Switch Expressions
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# - Foreach Loop
- C# - Goto Statement
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - LINQ
- C# - IEnumerable vs IEnumerator
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Tasks and Parallel Programming
- C# - Multithreading
- C# - Extension Methods
C# ArrayList - Synchronized() Method
The C# ArrayList Synchronized() method is used to create a synchronized (thread-safe) wrapper around an existing ArrayList. This method makes sure that all operations on the ArrayList are thread-safe when accessed by multiple threads.
Syntax
Following is the syntax of the C# ArrayList Synchronized() method −
public static ArrayList Synchronized(ArrayList list);
Parameters
This method accepts an ArrayList that needs to be synchronized.
Return value
This method Returns a thread-safe (synchronized) wrapper around the provided ArrayList.
Example 1: Create an ArrayList Thread-Safty
Following is the basic example of the Synchronized() method to make an ArrayList thread safty −
using System;
using System.Collections;
using System.Threading;
class Program
{
static void Main()
{
ArrayList myList = new ArrayList() { "A", "B", "C" };
// Creating a synchronized (thread-safe)
ArrayList syncList = ArrayList.Synchronized(myList);
Console.WriteLine("Original List:");
// Locking ensure the thread safety
lock (syncList.SyncRoot)
{
foreach (var item in syncList)
{
Console.Write(item + " ");
}
}
Console.WriteLine();
}
}
Output
Following is the output −
Original List: A B C
Example 2: Simulating Multi-Threaded Access
Here, in this example, we create an ArrayList. We then wrapped with thread-safe, and after that, we simulated with multithread to add elements in the ArrayList −
using System;
using System.Collections;
using System.Threading;
class Program
{
static void Main()
{
ArrayList myList = new ArrayList() { "A", "B", "C" };
// Creating a synchronized wrapper
ArrayList syncList = ArrayList.Synchronized(myList);
// Simulating multi-threaded access
Thread thread1 = new Thread(() =>
{
lock (syncList.SyncRoot)
{
syncList.Add("D");
Console.WriteLine("Thread 1 added D");
}
});
Thread thread2 = new Thread(() =>
{
lock (syncList.SyncRoot)
{
syncList.Add("E");
Console.WriteLine("Thread 2 added E");
}
});
thread1.Start();
thread2.Start();
thread1.Join();
thread2.Join();
// Displaying the updated synchronized list
Console.WriteLine("Updated List:");
lock (syncList.SyncRoot)
{
foreach (var item in syncList)
{
Console.Write(item + " ");
}
}
}
}
Output
Following is the output −
Thread 1 added D Thread 2 added E Updated List: A B C D E
Example 3: Adding Elements in a Thread-Safe Manner
The following example creates an ArrayList and then adds the element in the ArrayList in a thread-safe manner. Use the Synchronized method to make the ArrayList thread-safe −
using System;
using System.Collections;
class Example
{
static void Main()
{
ArrayList myList = new ArrayList() { 1, 2, 3, 4, 5 };
// Creating a synchronized (thread-safe) wrapper
ArrayList syncList = ArrayList.Synchronized(myList);
// Adding elements in a thread-safe manner
lock (syncList.SyncRoot)
{
syncList.Add(6);
syncList.Add(7);
}
Console.WriteLine("Synchronized List:");
lock (syncList.SyncRoot)
{
foreach (var item in syncList)
{
Console.Write(item + " ");
}
}
}
}
Output
Following is the output −
Original List: 1 2 3 4 5 Exception: Collection was of a fixed size.