What is the purpose of interlocked class in .NET?

Interlocked class belongs to System.Threading namespace. When two threads try to update same variables or when two threads are concurrently executing there are chances of many error occurrences and incorrect data updation.



In those situations, interlocked class and its methods are very helpful to manipulate on variables accessible by one or more threads without throwing any error or exception. Methods provided by interlocked class are listed below:

Increment - Used to increment a variable’s value and store its result. Int32 and Int64 integers are its legal parameters.
Decrement– Used to decrement a variable’s value and store its result. Int32 and Int64 integers are its legal parameters.
Exchange– Used to exchange values between variables. This method has seven overloaded versions based on the different types it can accept as parameter.
CompareExchange - Compares two variables and stores the result of comparison in another variable. This method also has seven overloaded versions.
Add – Adds two integer variables and updates the result in the first integer variable. It is used to add integers of type Int32 as well as Int64.
Read – Reads an integer variable. It is used to read an integer of type Int64.

| What is Private Access Modifier in C#? | What is Protected Access Modifier in C#? | What is Protected Internal Access Modifier in C#? | What is Public Access Modifier in C#? | What is the difference between virtual and abstract keywords in .NET? | What is the importance of Microsoft Application Blocks in .NET Architecture? | What is the need for Factory Method in C# | What is the purpose of ArrayList in .NET? | What is the purpose of Datareader in ADO.NET? | What is the purpose of Dataset in ADO.NET? | What is the purpose of finally block in C#? | What is the purpose of interlocked class in .NET? | What is the purpose of main() function in C# | What is the purpose of ManualResetEvent in .NET? | What is the purpose of sealed method in C#? | What is the purpose of Thread.Join() method in .NET? | What is the purpose of Thread.Sleep() method in .NET? | What is the purpose of throw keyword in C#? | What is the usage of ENUM in .NET? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.