Pure virtual methods in C#?

David Brewer picture David Brewer · Feb 9, 2011 · Viewed 59.8k times · Source

I've been told to make my class abstract:

public abstract class Airplane_Abstract

And to make a method called move virtual

 public virtual void Move()
        {
            //use the property to ensure that there is a valid position object
            double radians = PlanePosition.Direction * (Math.PI / 180.0);

            // change the x location by the x vector of the speed
            PlanePosition.X_Coordinate += (int)(PlanePosition.Speed * Math.Cos(radians));

            // change the y location by the y vector of the speed
            PlanePosition.Y_Coordinate += (int)(PlanePosition.Speed * Math.Sin(radians));
        }

And that 4 other methods should be "pure virtual methods." What is that exactly?

They all look like this right now:

public virtual void TurnRight()
{
    // turn right relative to the airplane
    if (PlanePosition.Direction >= 0 && PlanePosition.Direction < Position.MAX_COMPASS_DIRECTION)
        PlanePosition.Direction += 1;
    else
        PlanePosition.Direction = Position.MIN_COMPASS_DIRECTION;  //due north
}

Answer

Jon Skeet picture Jon Skeet · Feb 9, 2011

My guess is that whoever told you to write a "pure virtual" method was a C++ programmer rather than a C# programmer... but the equivalent is an abstract method:

public abstract void TurnRight();

That forces concrete subclasses to override TurnRight with a real implementation.