Can I override a property in c#? How?

C#InheritancePropertiesPolymorphismOverriding

C# Problem Overview


I have this Base class:

abstract class Base
{
  public int x
  {
    get { throw new NotImplementedException(); }
  }
}

And the following descendant:

class Derived : Base
{
  public int x
  {
    get { //Actual Implementaion }
  }
}

When I compile I get this warning saying Derived class's definition of x is gonna hide Base's version of it. Is is possible to override properties in c# like methods?

C# Solutions


Solution 1 - C#

You need to use virtual keyword

abstract class Base
{
  // use virtual keyword
  public virtual int x
  {
    get { throw new NotImplementedException(); }
  }
}

or define an abstract property:

abstract class Base
{
  // use abstract keyword
  public abstract int x { get; }
}

and use override keyword when in the child:

abstract class Derived : Base
{
  // use override keyword
  public override int x { get { ... } }
}

If you're NOT going to override, you can use new keyword on the method to hide the parent's definition.

abstract class Derived : Base
{
  // use new keyword
  public new int x { get { ... } }
}

Solution 2 - C#

Make the base property abstract and override or use the new keyword in the derived class.

abstract class Base
{
  public abstract int x { get; }
}

class Derived : Base
{
  public override int x
  {
    get { //Actual Implementaion }
  }
}

Or

abstract class Base
{
  public int x { get; }
}

class Derived : Base
{
  public new int x
  {
    get { //Actual Implementaion }
  }
}

Solution 3 - C#

Change property signature as shown below:

Base class

public virtual int x 
{ get { /* throw here*/ } }

Derived class

public override int x 
{ get { /*overriden logic*/ } }

If you do not need any implementation in Base class just use abstract property.

Base:

public abstract int x { get; }

Derived:

public override int x { ... }

I would suggest you using abstract property rather than trhowing NotImplemented exception in getter, abstact modifier will force all derived classes to implement this property so you'll end up with compile-time safe solution.

Solution 4 - C#

abstract class Base
{
 
  public virtual int x
  {
    get { throw new NotImplementedException(); }
  }
}

or

abstract class Base
{
  // use abstract keyword
  public abstract int x
  {
    get;
  }
}

In both case you have to write in the derived class

public override int x
  {
    get { your code here... }
  }

difference between the two is that with abstract you force the derived class to implement something, and with virtaul you can provide a default behavior that the deriver can use as is, or change.

Solution 5 - C#

abstract class Base 
{ 
  // use abstract keyword 
  public virtual int x 
  { 
    get { throw new NotImplementedException(); } 
  } 
} 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionatoMerzView Question on Stackoverflow
Solution 1 - C#Jeffrey ZhaoView Answer on Stackoverflow
Solution 2 - C#jrummellView Answer on Stackoverflow
Solution 3 - C#sllView Answer on Stackoverflow
Solution 4 - C#Felice PollanoView Answer on Stackoverflow
Solution 5 - C#Matt SeymourView Answer on Stackoverflow