C# Interview Question on Parallel.Invoke, lock and Thread Safety

C# Interview Question on Parallel.Invoke, lock and Thread Safety

Recently, I created a YouTube video discussing a very interesting C# interview question related to Parallel.Invoke, lock, thread safety and race conditions. Surprisingly, even experienced developers often get confused by this question.

In this blog, we will deeply analyze the problem and understand what is actually happening behind the scenes.

The Interview Question

public static void ParallelTest()
{
    int counter = 0;

    Parallel.Invoke(
        () =>
        {

            lock (counter)
            {
                counter = counter + 1;
            }
        },
        () =>
        {

            lock (counter)
            {
                counter = counter + 1;
            }
        }
    );

    Console.WriteLine(counter);
}

Options

  • Option A: Runtime Error
  • Option B: Compile-time Error
  • Option C: Will always print 2
  • Option D: Can print either 2 or 1

The Correct Answer

The correct answer is:

✅ Option B: Compile-time Error

Many developers immediately start thinking about race conditions and thread synchronization. However, the code does not even reach runtime.

The reason is simple:

lock(counter)

Here, counter is an int, which is a value type. But the lock statement in C# requires a reference type object.

Therefore, the compiler throws an error before execution even starts.

Why lock Requires a Reference Type

Internally, the lock statement works using object monitors. These monitors are associated with reference type objects.

Value types like int are stored differently and cannot safely act as synchronization objects.

What If We Remove the lock Completely?

Suppose we change the code like this:

public static void ParallelTest()
{
    int counter = 0;

    Parallel.Invoke(
        () =>
        {
            counter = counter + 1;
        },
        () =>
        {
            counter = counter + 1;
        }
    );

    Console.WriteLine(counter);
}

Now the code compiles successfully.

However, this introduces a race condition. Since both threads may try to update the variable simultaneously, the output may become unpredictable.

You may get:

  • 2
  • 1

depending on thread scheduling and execution timing.

What If We Use object counter = 0 ?

This is another interesting variation discussed in the video.

object counter = 0;

Now the code compiles because counter is an object reference.

But this creates another dangerous situation.

Inside the lock block, if the value changes, boxing may create a completely new object reference.

That means different threads may actually lock different objects, completely defeating synchronization.

This is one reason why locking directly on changing objects is considered bad practice.

The Proper Way

The recommended approach is to use a dedicated private object for locking.

private static readonly object locker = new object();

public static void ParallelTest()
{
    int counter = 0;

    Parallel.Invoke(
        () =>
        {
            lock (locker)
            {
                counter++;
            }
        },
        () =>
        {
            lock (locker)
            {
                counter++;
            }
        }
    );

    Console.WriteLine(counter);
}

This ensures all threads synchronize using the exact same reference object.

Important Concepts Covered

  • Parallel.Invoke
  • Thread safety
  • Race conditions
  • lock statement
  • Value types vs reference types
  • Boxing and unboxing
  • Compile-time vs runtime behavior
  • Proper synchronization techniques

Watch the Full Video

I have explained all these concepts in detail in my YouTube video along with practical discussion and interview perspective.

👉 Watch the full video here

About My 1-to-1 .NET Mentorship

I provide friendly and practical one-to-one mentorship for:

  • C#
  • ASP.NET Core
  • MVC
  • Web API
  • Entity Framework
  • SQL Server
  • Blazor
  • .NET Interview Preparation

My focus is always on strong fundamentals, real understanding, debugging skills and interview confidence rather than memorizing answers.

I have more than 20 years of experience in .NET technologies, and all sessions are conducted personally in a comfortable one-to-one environment.

🌐 Website: https://supernovaservices.com

Class Guidelines for Effective 1-on-1 Learning

To keep every session productive and distraction-free, please follow these simple guidelines:

  • Quiet Environment: Join from a calm, private room with minimal background noise. Avoid public or noisy places.
  • No Interruptions: Inform family/roommates in advance. Keep doors closed during class.
  • Mobile on Silent / DND: Set your phone to Silent or Do Not Disturb to prevent calls and notifications.
  • Be Fully Present: Do not multitask. Avoid attending to other calls, visitors, or errands during the session.
  • Stable Setup: Use a laptop/desktop with a stable internet connection and required software installed (Visual Studio/.NET, SQL Server, etc.).
  • Punctuality: Join on time so we can utilize the full session effectively.
  • Prepared Materials (If any): Keep project files, notes, and questions ready for quicker progress.

Following these guidelines helps you focus better and ensures I can deliver the best learning experience in every class.

Schedule a Quick 10-Minute Call

I prefer to start with a short 10-minute free call so I can understand:

  • Your learning objectives and career goals
  • Your current skill level
  • The exact topics you want to learn

Why? Because course content, teaching pace, and fees all depend on your needs — there’s no “one-size-fits-all” pricing. Please leave your details below, and I’ll get back to you to arrange a convenient time for the call.




Note: Payment is made only after your first class, once you’re completely satisfied. However, fees paid after the first class are non-refundable. This helps maintain scheduling commitments and allows me to reserve your preferred time slot with full attention.

Google Review Testimonials

.NET Online Training
Average Rating: 4.9
Votes: 50
Reviews: 50