[c#] C# "No suitable method found to override." -- but there is one

I'm having trouble overriding the method of a parent class in C#. The parent class is setup like so:

public class Base {
    public Base(Game1 game)
    {
        this.game = game;
    }

    public virtual void Draw()
    {
    }
}

...And the child class:

public class Ext : Base {
    public Ext(Game1 game) : base(game)
    {
    }

    public override void Draw(SpriteBatch batch)
    {
    }
}

I know I've successfully overridden a parent method in the past, and right now I'm probably overlooking something incredibly simple... what is it?

EDIT: That was actually a typo: in the actual script, Ext does derive from Base. The problem still persists. Thanks for the quick answers, though. :)

This question is related to c#

The answer is


I ran into a similar situation with code that WAS working , then was not.

Turned while dragging / dropping code within a file, I moved an object into another set of braces. Took longer to figure out than I care to admit.

Bit once I move the code back into its proper place, the error resolved.


I do not see the class Ext being derived from Base.


You're not inheriting from your base class:

public class Ext : Base {
    // constructor

    public override void Draw()
    {
    }
}

You cannot override a function with different parameters, only you are allowed to change the functionality of the overridden method.

//Compiler Microsoft (R) .NET Framework 4.5

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

//Overriding & overloading

namespace polymorpism
{
    public class Program
    {
        public static void Main(string[] args)
        {

            drowButn calobj = new drowButn();

            BtnOverride calobjOvrid = new BtnOverride();

            Console.WriteLine(calobj.btn(5.2, 6.6).ToString());

            //btn has compleately overrided inside this calobjOvrid object
            Console.WriteLine(calobjOvrid.btn(5.2, 6.6).ToString());
            //the overloaded function
            Console.WriteLine(calobjOvrid.btn(new double[] { 5.2, 6.6 }).ToString());

            Console.ReadKey();
        }
    }

    public class drowButn
    {

        //same add function overloading to add double type field inputs
        public virtual double btn(double num1, double num2)
        {

            return (num1 + num2);

        }


    }

    public class BtnOverride : drowButn
    {

        //same add function overrided and change its functionality 
        //(this will compleately replace the base class function
        public override double btn(double num1, double num2)
        {
            //do compleatly diffarant function then the base class
            return (num1 * num2);

        }

        //same function overloaded (no override keyword used) 
        // this will not effect the base class function

        public double btn(double[] num)
        {
            double cal = 0;

            foreach (double elmnt in num)
            {

                cal += elmnt;
            }
            return cal;

        }
    }
}

You need to inherit from the base class.


The signature of your methods is different. But to override a method the signature must be identical.

In your case the base class version has no parameters, the derived version has one parameter.

So what you're trying to do doesn't make much sense. The purpose is that if somebody calls the base function on a variable that has the static type Base but the type Ext at runtime the call will run the Ext version. That's obviously not possible with different parameter counts.

Perhaps you don't want to override at all?


You have forgotten to derive from Base

public class Ext : Base
                 ^^^^^^        

Make sure that you have the child class explicitly inherit the parent class:

public class Ext : Base { // stuff }

Ext needs to inherit the base, so in your definition it should say:

public class Ext : Base { //...

Probably your base class is a different one with the same name. Check by right click on the extended class and go to the definition. Check if the class is correct one.


I ran into this issue only to discover a disconnect in one of my library objects. For some reason the project was copying the dll from the old path and not from my development path with the changes. Keep an eye on what dll's are being copied when you compile.