Skip to main content

What is the lock keyword used for?

Junior C#
Quick Answer The lock keyword prevents two threads from entering the same block of code at the same time. One thread acquires the lock; others wait. Always lock on a private dedicated object รขโ‚ฌโ€ not this รขโ‚ฌโ€ to avoid accidental interference from outside code.

Answer

The lock keyword prevents multiple threads from accessing a shared resource at the same time.

It ensures thread safety.

S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice