
- C# Basic Tutorial
- 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# - Decision Making
- C# - Loops
- 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# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
What is the difference between Monitor and Lock in C#?
Both Monitor and lock provides a mechanism that synchronizes access to objects. lock is the shortcut for Monitor.Enter with try and finally.
Lock is a shortcut and it's the option for the basic usage. If we need more control to implement advanced multithreading solutions using TryEnter() Wait(), Pulse(), & PulseAll() methods, then the Montior class is your option.
Example for Lock −
Example
class Program{ static object _lock = new object(); static int Total; public static void Main(){ AddOneHundredLock(); Console.ReadLine(); } public static void AddOneHundredLock(){ for (int i = 1; i <= 100; i++){ lock (_lock){ Total++; } } }
Example for Monitor −
Example
class Program{ static object _lock = new object(); static int Total; public static void Main(){ AddOneHundredMonitor(); Console.ReadLine(); } public static void AddOneHundredMonitor(){ for (int i = 1; i <= 100; i++){ Monitor.Enter(_lock); try{ Total++; } finally{ Monitor.Exit(_lock); } } } }
- Related Articles
- Difference Between Monitor and Television
- Difference Between Semaphore and Monitor in OS
- Difference between Object level lock and Class level lock in Java
- What is the difference between $ and @ in R?
- What is the difference between == and === in JavaScript?
- What is the difference between /* */ and /** */ comments in Java?
- What is the difference between NA and in R?
- What is the difference between | and || operators in c#?
- What is the difference between >> and >>> operators in Java?
- What is the difference between comments /*...*/ and /**...*/ in JavaScript?
- What is the difference between = and == operators in Python?
- What is the difference between the != and operators in Python?
- What is the difference between = and: = assignment operators?
- What is the difference between IAS and IPS?
- What is the difference between g++ and gcc?

Advertisements