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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:50:59+00:00 2026-05-23T20:50:59+00:00

I have a simple basic question about C# and inheritance. In the below example,

  • 0

I have a simple basic question about C# and inheritance.

In the below example, I’m showing 2 ways of setting a must set properties (CaptionDisplayText and Firstname):

public abstract class BaseClass
{
    private string _firstName;

    protected BaseClass(string captionDisplayText)
    {
        this.CaptionDisplayText = captionDisplayText;

        this._firstName = this.GetFirstName();
    }

    protected string CaptionDisplayText { get; private set; }

    protected abstract string GetFirstName();
}

public class DerivedClass : BaseClass
{
   protected DerivedClass():base(String.Empty)
   {

   }

   protected override string GetFirstName()
   {
       return String.Empty;
   }
}

Approach 1, CaptionDisplayText, introduces a property and sets it in the constructor whereas approach 2 introduces an abstract method which is overridden in the Derived class.

I know Approach2 is bad if the method was virtual rather than abstract but here it’s abstract which means it is executed before the constructor of the base class and that will be fine.

I’m having 10-15 properties/fields which have to be set by my derived classes so I’m thinking which approach would be more maintainable, readable and testable.

I think approach 1 is more testable whereas approach 2 is much more readable. Approach 2 would also be testable as the mock object will have to implement the abstract members anyway.

Which way is best in your view and is there a third way?

Many thanks,

  • 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-23T20:51:00+00:00Added an answer on May 23, 2026 at 8:51 pm

    Doing logic in the constructor is not the way to go if you do want to make the class testable. So by doing this._firstName = this.GetFirstName(); will introduce extra test effort, the code is called even if you do not want to do anything with the first name. In the first approach you could have filled it with a null, which indicates it is not important for your test.

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

Sidebar

Related Questions

first post, so play nice! I have a fairly basic question about Python dictionaries.
I have a general question about WPF performance. We have a relatively simple forms
In my database I have a simple table that contains values showing the basic
I've got a basic question about obj-c coding, say I have two classes represented
The basic idea of a masterpage is simple -- you have a block of
Currently, I have some basic code to play a simple tone whenever a button
I am implementing a very simple file database. I have 2 basic operations: void
This, I'm sure is a pretty basic question about JavaScript, so apologies in advance.
I've got a relatively simple question about asp.net MVC models. I've got a model
My question is about memory use and objects in actionscript 2. If I have

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.