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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:02:54+00:00 2026-05-10T22:02:54+00:00

I have an abstract base class and I want to declare a field or

  • 0

I have an abstract base class and I want to declare a field or a property that will have a different value in each class that inherits from this parent class.

I want to define it in the baseclass so I can reference it in a base class method – for example overriding ToString to say ‘This object is of type property/field‘. I have got three ways that I can see of doing this, but I was wondering – what is the best or accepted way of doing this? Newbie question, sorry.

Option 1:
Use an abstract Property and override it on the inherited classes. This benefits from being enforced (you have to override it) and it is clean. But, it feels slightly wrong to return a hard-code value rather than encapsulate a field and it is a few lines of code instead of just. I also have to declare a body for ‘set’ but that is less important (and there is probably a way to avoid that which I am not aware of).

abstract class Father {     abstract public int MyInt { get; set;} }  class Son : Father {     public override int MyInt     {         get { return 1; }         set { }     } } 

Option 2
I can declare a public field (or a protected field) and explicitly override it in the inherited class. The example below will give me a warning to use ‘new’ and I can probably do that, but it feels wrong and it breaks the polymorphism, which was the whole point. Doesn’t seem like a good idea…

abstract class Mother {     public int MyInt = 0; }  class Daughter : Mother {     public int MyInt = 1; } 

Option 3
I can use a protected field and set the value in the constructor. This seems pretty tidy but relies on me ensuring the constructor always sets this and with multiple overloaded constructors there is always a chance some code path won’t set the value.

abstract class Aunt {     protected int MyInt; }  class Niece : Aunt {     public Niece()     {         MyInt = 1;     } } 

It’s a bit of a theoretical question and I guess the answer has to be option 1 as it is the only safe option but I am just getting to grips with C# and wanted to ask this of people with more experience.

  • 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. 2026-05-10T22:02:54+00:00Added an answer on May 10, 2026 at 10:02 pm

    Of the three solutions only Option 1 is polymorphic.

    Fields by themselves cannot be overridden. Which is exactly why Option 2 returns the new keyword warning.

    The solution to the warning is not to append the “new” keyword, but to implement Option 1.

    If you need your field to be polymorphic you need to wrap it in a Property.

    Option 3 is OK if you don’t need polymorphic behavior. You should remember though, that when at runtime the property MyInt is accessed, the derived class has no control on the value returned. The base class by itself is capable of returning this value.

    This is how a truly polymorphic implementation of your property might look, allowing the derived classes to be in control.

    abstract class Parent {     abstract public int MyInt { get; } }  class Father : Parent {     public override int MyInt     {         get { /* Apply formula 'X' and return a value */ }     } }  class Mother : Parent {     public override int MyInt     {         get { /* Apply formula 'Y' and return a value */ }     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes (MVC view model) which inherits from one abstract base class.
I have an abstract base class from which many classes are derived. I want
I have an abstract base class and want to implement a function in the
I have an abstract base class that holds a Dictionary. I'd like inherited classes
I have a abstract base class that I have many inherited classes coming off
I have a abstract base C# class with a couple of methods that has
I have an abstract base class in my application. I want to provide an
I have an abstract base class Contact that has two subclasses: Person and Company
I have a abstract class where I want to declare final variables. However, I
I have an abstract class that has two constructors. When another class inherits this

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.