Icollection.Issynchronized Property In C#

Within the .NET Framework, the "ICollection.IsSynchronized" property serves to signify whether the collection allows synchronized access in a thread-safe manner. When the "IsSynchronized" property is present, it denotes that the collection supports synchronized access to its elements, facilitating simultaneous usage by multiple threads while upholding data integrity. Conversely, if "IsSynchronized" is false, the collection lacks thread safety, necessitating manual synchronization by the programmer to prevent race conditions. By querying the IsSynchronized property, one can determine the synchronization status of the collection based on the Boolean value (true or false) it returns.

  1. Thread Safety

Data consistency and integrity play a vital role in a multi-threaded environment where multiple threads interact with a shared collection simultaneously. The term used to describe a data structure's capability to handle concurrent access from various threads without causing data corruption is referred to as thread safety.

  1. Thread-Safe Collections

Thread-safe synchronized collection classes are integrated into the .NET framework. These specialized collections guarantee that all operations on the collection are synchronized while also adhering to the ICollection interface.

The IsSynchronized Property

This attribute is a component of the ICollection interface, signaling the synchronization status of the collection. When the IsSynchronized property is set to true, the collection ensures thread safety by internally synchronizing access to its elements. Conversely, when IsSynchronized is false, the collection lacks inherent thread safety, necessitating the application of external synchronization techniques.

Syntax:

It has the following syntax:

Example

public bool IsSynchronized { get; }
  • Access Modifier: The public keyword indicates that any code running in the same assembly or any assembly that references it can access the property.
  • Return Type: In this case, the property returns a Boolean value (true or false), and the return type is bool.
  • Property Name: IsSynchronized is the descriptive property name indicating whether the collection is synchronized.
  • { get; }: It is the property's automatically implemented getter accessor. It indicates that while the property is read-only (accessible), it cannot be modified (set) outside the defined class. The get accessor retrieves the value of the property.
  • Key Points to Remember:

  • The collection offers a synchronized (thread-safe) wrapper on the underlying collection if IsSynchronized returns true. The collection automatically ensures thread safety for all operations.
  • Thread safety must be handled externally using methods like locks (lock keyword) or other synchronization primitives if IsSynchronized returns false, indicating that the collection does not support synchronization.
  • Example 1: Using ArrayList

The .NET Framework provides an arraylist collection class that automatically adjusts its size based on the addition or removal of elements.

Example

using System;
using System.Collections;
class C# Tutorial
{
    static void Main(string[] args)
    {
        // Create a synchronized ArrayList
        ArrayList synchronized_List = ArrayList.Synchronized(new ArrayList());
        // Add some elements to the synchronized list
        synchronized_List.Add("Rose");
        synchronized_List.Add("Lilly");
        synchronized_List.Add("Lotus");
        // Check if the collection is synchronized
        bool isSync = synchronized_List.IsSynchronized;
        Console.WriteLine($"Is the ArrayList synchronized? {isSync}");
    }
}

Output:

Output

Is the ArrayList synchronized? True

Explanation:

In this instance, the ArrayList is employed to generate a synchronized ArrayList using the Synchronized method. Following this, we insert several elements into the synchronized list and check the value of the IsSynchronized property, which should return a true value indicating that the ArrayList is synchronized.

Example 2: Using Hashtable

The .NET Framework contains a collection class known as Hashtable, which stores pairs of keys and values.

Example

using System;
using System.Collections;
class C# Tutorial
{
    static void Main(string[] args)
    {
        // Create a synchronized Hashtable
        Hashtable synchronizedHashtable = Hashtable.Synchronized(new Hashtable());
        // Add some key-value pairs to the synchronized hashtable
        synchronizedHashtable.Add(1, "Rose");
        synchronizedHashtable.Add(2, "Lotus");
        synchronizedHashtable.Add(3, "Lilly");
        // Check if the collection is synchronized
        bool isSync = synchronizedHashtable.IsSynchronized;
        Console.WriteLine($"Is the Hashtable synchronized? {isSync}");
    }
}

Output:

Output

Is the Hashtable synchronized? True

Explanation:

In this instance, the Hashtable.Synchronized function is employed to generate a synchronized Hashtable. Subsequently, we insert several key-value pairs into the synchronized hashtable and confirm that the IsSynchronized property value is true, indicating the synchronization of the hashtable.

Use Cases:

  • The IsSynchronized methods comes in useful when several threads must get to and modify the same collection simultaneously in multi-threaded applications.
  • Developers may use it to determine if a collection is thread-safe by default or whether further synchronization techniques are needed.
  • Conclusion:

In summary, the ICollection.IsSynchronized attribute in C# offers details on whether a collection is synchronized for safe access by multiple threads. Guaranteeing data coherence and avoiding race conditions when working with collections is a crucial aspect of multi-threaded programming.

Input Required

This code uses input(). Please provide values below: