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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:56:54+00:00 2026-06-07T20:56:54+00:00

Define the following C# interface: public interface IShape { int NumberOfLineSegments {get;} int Area

  • 0

Define the following C# interface:

public interface IShape
{
    int NumberOfLineSegments {get;}
    int Area {get;}
}

Next, I want to define several rectangle classes: Trapezoid, square,etc. All of these classes differ in their Area() property, but NumberOfLineSegments() always returns 4. Therefore, I would like an ‘interim’ class or interface, called Rectangle (or IRectangle), that looks something like:

public Rectangle : IShape
{
    public int NumberOfLineSegments{get{return 4;}}
}

I want Rectangle to implement only NumberOfLineSegment(), and leave it to its derived classes to implement the rest:

public Square : Rectangle 
{
    public int Area() {get{return length*height;}
}

However, since IShape is an interface, the Rectangle class must implement also Area(), which it knows not how to implement..
Thus I seem to be stuck, either with defining a ‘dummy’ Area() method for Rectangle, or not using inheritence at all.

Is there a way to circumvent this? I have read extensively through Richter’s clr via c#, and in StackOverflow. Thanks in advance!

  • 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-07T20:56:57+00:00Added an answer on June 7, 2026 at 8:56 pm

    There are two options.

    1. Make the implementation virtual and empty (or throw a NotImplementedException), so it does nothing by default until derived.
    2. Make the base class abstract and create abstract signatures for the interface methods you want to force down the chain.

    Number 2 is much more preferable, as it forces derived classes to implement the method, whereas in number 1 derived classes are not forced to override base virtual methods.

    Abstract methods can successfully satisfy interface definitions as the compiler will know that abstract classes themselves cannot be instantiated, and any derived classes are forced to have the abstract method implemented.

    That said, if there are interface members that do not make sense to a particular type, it is usually an indicator to break down your interfaces:

    public interface IShape : ICalculateArea, IHaveLineSegments
    {
    }
    
    public interface ICalculateArea
    {
        float Area { get; }
    }
    
    public interface IHaveLineSegments
    {
        int NumberOfLineSegments { get; }
    }
    
    class Rectangle : IHaveLineSegments
    {
        public int NumberOfLineSegments { get; private set; }
    }
    
    class Square : Rectangle, IShape
    {
        public float Area { get; private set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following simple RESTEasy (JAX-RS) service: @Path(/example-service) public interface ExampleService { @Path(/ping) @GET
I've made the following declaration for interfaces: public interface IBasic { int Data {
How can I achieve something like the following? public interface IGenericRepository { int id
I have a following problem I want to solve ellegantly: public interface IMyclass {
I have the following interfaces defined: public interface IAudit { DateTime DateCreated { get;
I have defined the following Interface [ServiceContract] public interface IHealthProducts { [OperationContract()] ResponseClass OrderSelfSignedHealthCertificate();
I want to define a following function: if(stmtToFinalize) { NSLog(@Finalizing statement stmtToFinalize); if (sqlite3_finalize(stmtToFinalize)
I am rounding the corners of a rectangle by following Define a coloredBoxRect and
I have this following interface : public interface FruitDetails { public String getFruitName(); }
Using the generic dao pattern, I define the generic interface: public interface GenericDao<T extends

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.