Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8645807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:38:43+00:00 2026-06-12T12:38:43+00:00

I have a class with two properties and two methods. Like the one below

  • 0

I have a class with two properties and two methods. Like the one below for example. (please ignore the data types or return types, it’s just a typical scenario)

// The methods could be invoked by multiple threads
    public class Stock
    { 

    private static int FaceValue {get; set;}
    private static int Percent (get; set;}

    //    method that updates the two properties
    Public void UpdateStock()
    {
      FaceValue += 1;
      Percent = FaceValue * 100;
    }

    //   method that reads the two properties
    public int[] GetStockQuote()
    {
        return new int[] { FaceValue, Percent};
    }

    }

I need to ensure this class is thread safe. I could use lock(obj) in both the methods as one technique to make it threadsafe but what would be the best technique to make it thread safe, considering the following:

  1. There are only two properties that is read/updated. So, not sure if locking inside the methods is a good technique.

  2. Will it be enough if I just make the properties thread safe rather than the methods or the class ?

  3. Also, is there a way to make the whole class thread safe rather than individual methods or properties ? Any recommended lock techniques from .Net 4.0 ?

Just wondering if I am thinking through this right or may be I am over complicating it considering these. Many thanks in advance to help me get this clear.

Mani

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:38:44+00:00Added an answer on June 12, 2026 at 12:38 pm

    In general, a lock is probably the simplest approach here.

    A potentially better alternative would be to make this class immutable. If you make it so you can’t change the values within the class once it’s created, you no longer have to worry when reading the values, as there’s no way for them to be modified.

    In this case, that could be done by having a constructor that takes the two values, and changing UpdateStock to be more like:

    public Stock GetUpdatedStock()
    {
         // Create a new instance here...
         return new Stock(this.FaceValue + DateTime.Now.MilliSecond, this.FaceValue * 100);
    }
    

    Edit:

    Now that you’ve made FaceValue and Percent static, you will need synchronization. A lock is likely the simplest option here.

    With a single value, you could potentially use the Interlocked class to handle updates atomically, but there is no way to do an atomic update of both values*, which is likely required for the thread safety to be done properly. In this case, synchronizing via a lock will solve your issue.

    *Note: This could possibly be done without a lock via Interlocked.CompareExchange if you put both values within a class, and exchanged the entire class instance – but that’s likely a lot more trouble than it’s worth.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a 'Contact' class with two properties : firstName and lastName. When I
I've the problem: I have to serialize class to XML file. But two properties
I have a class with two constructors, one that takes no arguments and one
I have a class with two constructors that look like this: public MyClass(SomeOtherClass source)
I'm have a class with two methods, A QuickSort() and a ReverseArray(). When I
I have one class with two important functions: public class Foo { //plenty of
I have two class libraries MyLibrary.dll and MyLibraryEditor.dll for a Unity runtime and editor
I have two class files hudlayer.m and actionlayer.m I have a method named jump
So i have two Class's Class A and Class B . I have a
I have a class with two constructors (C#). Here is the code snippet: public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.