igorjrr
igorjrr

Reputation: 902

Overriding C# variable in derived class with different data type

My base class declares:

public double Value;

My derived class declares:

public new decimal Value;

In the tip to use the new keyword, it says: "...Use the new keyword if hiding was intended.".

This explanation doesn't seem correct. When using the new keyword, it is not hiding the base variable. It keeps both variables with the same name, and even with different values.

I understand there is a design issue on my side, but I'd like to know why the new keyword doesn't really hide the base class variable. Also, why the new keyword is even available if it would mean a design issue (changing data type in the derived class). Is using dynamic an option here? The issue is: for most part, double is fine. However in a few derived classes, it must be decimal.

This question is focused on the use of the new keyword for variables in derived classes, not really a discussion of differences between double and decimal.

Thank you.

Upvotes: 3

Views: 3555

Answers (1)

Louis Go
Louis Go

Reputation: 2598

You mixed the concepts about (new, override) and (property, field).

I'll provide some information about them and provide my suggestion. My suggestion is on the 3rd part.

If this post doesn't answer your question, please edit your post with use case.


new is not override

new is to "hide" base class's property/field, which is NOT overriding virtual functions.

To sum up:

  1. override is used when base class' virtual function need to be altered or abstract function need to be defined.

If child class calls a function foo of base class which calls a virtual function bar, the CHILD version of bar would be called, not base version.

  1. For new,

If a child class calls a function foo of base class which calls a function bar, the BASE version of bar would be called, not child version.

I rarely use new, so can't suggest about how to use it.

Ref Difference between new and override


You cannot override a Field.

public class foo
{
    public double field; // This is field
    public int property  // This is property
    {
        get{ return 1  };
    }
}
  1. Property is a wrapper of get/set functions.

  2. Property could be overridden (because of #1), but fields could not be overridden.

Ref What is the difference between a field and a property?


Suggestion

What I come to mind is to implement Value as an object property

public class foo
{
    public virtual object Value
    {
        get; set;
    }
}

Then wrapping it to desired type, but this would cost box and unboxing.

However if your case is not performance critical, it should be okay.

public class bar : foo
{
    public override object Value
    {
        get{ // something different
        }; 
        set{ // something different
        };
    }

    public double DesiredValue
    {
        get{  return (double)Value };
    }
}

Upvotes: 6

Related Questions