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 8603629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:22:09+00:00 2026-06-12T02:22:09+00:00

In C# – but it may be applicable to other languages – in the

  • 0

In C# – but it may be applicable to other languages – in the context of LSP, how can I ensure that a class inheriting from another (mutable) one will not break the original contract?

For example:
If I have public, internal or protected property setters, there is a risk that inheriting classes will break the original contract.
The same is applicable for virtual methods.

This is especially true if the owner of the parent and inheriting classes are not the same persons as it may introduce a lack of knowledge in the contract and in the intention of the original developer.

Is immutability the only one solution or are there some other ways of doing?
I tend to consider that inheritance corresponds to a “behaves like a”-relationship instead of a “is a”-relationship. Is it a correct logical safeguard?

Here’s an example for the sake of illustration:

public class Foo
{
    public virtual void DummyMethod(int dummyParameter)
    {
        if (dummyParameter > 10) { throw new ArgumentOutOfRangeException(); }
    }
}

public class Bar : Foo
{
    public override void DummyMethod(int dummyParameter)
    {
        if (dummyParameter < 0) { throw new InvalidOperationException(); }
    }
}

I noticed some other questions dealing with this topic (here for example) but I’m looking for general solutions or good practices to apply to avoid facing these issues upfront.

  • 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-12T02:22:10+00:00Added an answer on June 12, 2026 at 2:22 am

    You can prevent violation of the LSP by using the sealed keyword.

    This means you now can’t inherit from this class at all, so you have to decide which you prefer: eliminating the risk of violating the LSP or the freedom to inherit and modify.

    Credit to @Hans Passant for first mentioning sealed in comments.

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

Sidebar

Related Questions

I am reading some og tags from sites but I can't seem to decode
An extension to my previous question: Text cleaning and replacement: delete \n from a
Have a webpage that will be viewed by mainly IE users, so CSS3 is
I have a simple restful service that transforms a JAXB-anntotated beans to response XML
This could be a duplicate question, but I have no idea what search terms
My NSXMLParser breaks on this string: <title>AAA &#8211; BCDEFGQWERTYUIO</title> I parsed it in this
I am getting the value from web service like &gt;&lt;&amp;&nbsp; etc. I want to
I'm building a small parser that scrapes web pages and logs the data on
I am currently running into a problem where an element is coming back from
I am currently scraping some data from the internet and converting into xml documents.

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.