Search code examples
c#overridingvirtualabstract

Confused between virtual and override for a property in c#


I am translating a c++ file to C# and I came across this line of code.

public property str1 Headers 
{
    virtual str1 get() override { return headers; }
}

So, when I am writing this in C#, how would it be done? Is it a virtual or an overriden property? This is defined in a class which inherits from a base class that defines this property as an abstract one.

EDIT: I think it's a property in c# and I have translated as follows. But the get method is giving an error.

public str1 Headers
{
        override get  { return headers; }
        //virtual get { return headers; }
}

Solution

  • Since you are overriding a property inherited from a base class, just use the override modifier on your property. For Properties, the override modifier applies to both get and set accessors.

    public class YourClass : SomeBaseClass
    {
         public override str1 Headers
         {
             get 
             {
                  return headers;
             }
         }
    }