Polymorphic Implementation of Fields and Properties in C#: Best Practices with Abstract Properties

Nov 24, 2025 · Programming · 12 views · 7.8

Keywords: C# | Polymorphism | Abstract Properties

Abstract: This article provides an in-depth exploration of three approaches to achieving polymorphism for fields and properties in C#, with a focus on the advantages of abstract properties. Through comparative analysis of abstract properties, field hiding, and constructor initialization, it elaborates why abstract properties represent the only correct choice for genuine polymorphic behavior. Complete code examples and thorough technical analysis help developers grasp core concepts of polymorphism in object-oriented programming.

Implementation of Polymorphism in C#

In object-oriented programming, polymorphism is a fundamental concept that allows derived classes to implement methods or properties defined in a base class in different ways. In C#, achieving polymorphic behavior for properties requires careful selection of language features.

Comparative Analysis of Three Implementation Approaches

Let us analyze in detail the three implementation approaches proposed by the questioner, understanding the advantages, disadvantages, and applicable scenarios of each.

Approach One: Abstract Property Implementation

Abstract properties represent the only correct way to achieve genuine polymorphic behavior. By defining abstract properties, we force derived classes to provide concrete implementations, ensuring type safety and behavioral consistency.

abstract class Parent
{
    abstract public int MyInt { get; }
}

class Father : Parent
{
    public override int MyInt
    {
        get { return CalculateFatherValue(); }
    }
    
    private int CalculateFatherValue()
    {
        // Complex calculation logic
        return 42;
    }
}

class Mother : Parent
{
    public override int MyInt
    {
        get { return CalculateMotherValue(); }
    }
    
    private int CalculateMotherValue()
    {
        // Different calculation logic
        return 24;
    }
}

The advantages of this method include:

Approach Two: Problems with Field Hiding

Using fields and attempting to hide base class fields with the new keyword presents significant drawbacks:

abstract class Mother
{
    public int MyInt = 0;
}

class Daughter : Mother
{
    public new int MyInt = 1; // Using new keyword to hide base class field
}

Issues with this approach:

Approach Three: Limitations of Constructor Initialization

The method of setting values in constructors through protected fields:

abstract class Aunt
{
    protected int MyInt;
}

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

Limitations of this approach:

Advanced Applications of Abstract Properties

In practical development, abstract properties can be applied to various scenarios. The following example demonstrates how to use polymorphic properties in base class methods:

abstract class Animal
{
    public abstract string SpeciesName { get; }
    
    public virtual string GetDescription()
    {
        return $"This animal is a {SpeciesName}";
    }
}

class Dog : Animal
{
    public override string SpeciesName 
    { 
        get { return "Canis lupus familiaris"; } 
    }
}

class Cat : Animal
{
    public override string SpeciesName 
    { 
        get { return "Felis catus"; } 
    }
    
    public override string GetDescription()
    {
        return $"This feline is scientifically known as {SpeciesName}";
    }
}

Performance and Design Considerations

While abstract properties provide optimal polymorphic support, performance-sensitive scenarios require consideration:

Best Practices Summary

Based on the above analysis, we can derive the following best practices:

  1. Prefer Abstract Properties: When genuine polymorphic behavior is required
  2. Consider Read-only Properties: Use read-only properties to simplify design if setting functionality is unnecessary
  3. Avoid Field Hiding: The new keyword typically indicates design problems
  4. Use Protected Fields Cautiously: Only when direct field access is genuinely needed
  5. Maintain Consistency: Use uniform approaches throughout inheritance hierarchies

By adhering to these principles, developers can create C# code that aligns with object-oriented design principles while maintaining excellent maintainability.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.