MCQs > IT & Programming > C# > There is a class that has a public int counter field that is accessed by multiple threads. This int is only incremented or decremented. To increment this field, three thread-safe approaches are mentioned below: A) lock(this.locker) this.counter++; B) Interlocked.Increment(ref this.counter); C) Change the access modifier of counter to public volatile Which statement is incorrect with regards to these approaches?

C# MCQs

There is a class that has a public int counter field that is accessed by multiple threads. This int is only incremented or decremented. To increment this field, three thread-safe approaches are mentioned below:

A) lock(this.locker) this.counter++;

B) Interlocked.Increment(ref this.counter);

C) Change the access modifier of counter to public volatile

Which statement is incorrect with regards to these approaches?

Answer

Correct Answer: B is the best approach as it effectively does the read, increment, and write in 'one hit' which can't be interrupted.

Explanation:

Note: This Question is unanswered, help us to find answer for this one

C# Skill Assessment

Overall Skill Level-Poor

Your Skill Level: Poor

Retake Quizzes to improve it

search

C# Skill Assessment

Overall Skill Level-Poor

Your Skill Level: Poor

Retake Quizzes to improve it