Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I override, not hide, a member variable (field) in a C# subclass?

I want this to tell me the Name of both ItemA and ItemB. It should tell me "Subitem\nSubitem", but instead it tells me "Item\nSubitem". This is because the "Name" variable defined in the Subitem class is technically a different variable than the "Name" variable defined in the base Item class. I want to change the original variable to a new value. I don't understand why this isn't the easiest thing ever, considering virtual and override work perfectly with methods. I've been completely unable to find out how to actually override a variable.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine(ItemA.Name);
            System.Console.WriteLine(ItemB.Name);
            System.Console.ReadKey();
        }
        static Item ItemA = new Subitem();
        static Subitem ItemB = new Subitem();
    }
    public class Item
    {
        public string Name = "Item";
    }
    public class Subitem : Item
    {
        new public string Name = "Subitem";
    }
}
like image 989
Eagle-Eye Avatar asked Jan 13 '12 01:01

Eagle-Eye


People also ask

Can I override variable?

Because variables in Java do not follow polymorphism and overriding is only applicable to methods but not to variables. And when an instance variable in a child class has the same name as an instance variable in a parent class, then the instance variable is chosen from the reference type.

How do you override a field?

Fields can't be overridden; they're not accessed polymorphically in the first place - you're just declaring a new field in each case. It compiles because in each case the compile-time type of the expression is enough to determine which field called number you mean.

Can you override variables in C#?

In C# 8.0 and earlier, the return types of an override method and the overridden base method must be the same. You cannot override a non-virtual or static method. The overridden base method must be virtual , abstract , or override . An override declaration cannot change the accessibility of the virtual method.

How do you override a derived class method?

An override method is a new implementation of a member that is inherited from a base class. The overridden base method must be virtual, abstract, or override. Here the base class is inherited in the derived class and the method gfg() which has the same signature in both the classes, is overridden.


2 Answers

You cannot override variables in C#, but you can override properties:

public class Item {     public virtual string Name {get; protected set;} } public class Subitem : Item {     public override string Name {get; protected set;} } 

Another approach would be to change the value in the subclass, like this:

public class Item {     public string Name = "Item"; } public class Subitem : Item {     public Subitem()     {         Name = "Subitem";     } } 
like image 136
Sergey Kalinichenko Avatar answered Oct 04 '22 15:10

Sergey Kalinichenko


There is no concept of polymorphism (which is what makes override do its thing) with fields (what you call a variables). What you need to use here instead is a property.

public class Item
{
    public virtual string Name
    {
        get { return "Item"; }
    }
}

public class SubItem : Item
{
    public override string Name
    {
        get { return "Subitem"; }
    }
}
like image 35
Adam Robinson Avatar answered Oct 04 '22 16:10

Adam Robinson