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

  • SEARCH
  • Home
  • 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 801039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:22:55+00:00 2026-05-14T23:22:55+00:00

I want to know if one class is inheriting from another, is it better

  • 0

I want to know if one class is inheriting from another, is it better to have the classes share a lock object that is defined at the base class or to have a lock object defined at each inheritance level.

A very simple example of a lock object on each level of the class

public class Foo {
  private object thisLock = new object();
  private int ivalue;

  public int Value { 
    get {
      lock( thisLock ) {
        return ivalue;
      }
    }
    set {
      lock( thisLock ) {
        ivalue= value;
      }
    }
  }
}

public class Foo2: Foo {
  private object thisLock2 = new object();

  public int DoubleValue { 
    get {
      lock( thisLock2 ) {
        return base.Value * 2;
      }
    }
    set {
      lock( thisLock2 ) {
        base.Value = value / 2;
      }
    }
  }
}

public class Foo6: Foo2 {
  private object thisLock6 = new object();

  public int TripleDoubleValue { 
    get {
      lock( thisLock6 ) {
        return base.DoubleValue * 3;
      }
    }
    set {
      lock( thisLock6 ) {
        base.DoubleValue = value / 3;
      }
    }
  }
}

A very simple example of a shared lock object

public class Foo {
  protected object thisLock = new object();
  private int ivalue;

  public int Value { 
    get {
      lock( thisLock ) {
        return ivalue;
      }
    }
    set {
      lock( thisLock ) {
        ivalue= value;
      }
    }
  }
}

public class Foo2: Foo {     
  public int DoubleValue { 
    get {
      lock( thisLock ) {
        return base.Value * 2;
      }
    }
    set {
      lock( thisLock ) {
        base.Value = value / 2;
      }
    }
  }
}
public class Foo6: Foo2 {     
  public int TripleDoubleValue { 
    get {
      lock( thisLock ) {
        return base.DoubleValue * 3;
      }
    }
    set {
      lock( thisLock ) {
        base.DoubleValue = value / 3;
      }
    }
  }
}

Which example is the preferred way to manage locking within an inherited class?

  • 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-05-14T23:22:55+00:00Added an answer on May 14, 2026 at 11:22 pm

    You can find your second option quite often in the .NET Framework, using a property instead of a field though (e.g., the CommunicationObject.ThisLock Property):

    public class Foo {
      private readonly object thisLock = new object();
      private int ivalue;
    
      protected object ThisLock {
        get {
          return thisLock;
        }
      }
    
      public int Value { 
        get {
          lock( ThisLock ) {
            return ivalue;
          }
        }
        set {
          lock( ThisLock ) {
            ivalue= value;
          }
        }
      }
    }
    

    and

    public class Foo2: Foo {     
      public int DoubleValue { 
        get {
          lock( ThisLock ) {
            return base.Value * 2;
          }
        }
        set {
          lock( ThisLock ) {
            base.Value = value / 2;
          }
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have one domain link text i want to know that does google crawl
I want to know how to move from one class to other class in
I have div with 6 classes. I want to know if that div has
Summary Let's say I have two C# 4.0 classes, one inheriting from the other:
HI, i need a XML parser. i want to know that which one is
I want to know how to create a label that contains two icons, one
I create a web service RESTful, know i want to use one service from
I know Java forbids inheriting from multiple classes and allows implementing any number of
If I have minimum 2 classes. One class creates the Bitmap and the other
I have a JAR with 4 classes, each one has Main method. I want

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.