In this case, we'll have to be creative. Simply ignoring the arguments extrapolates the C # language design.
First, the method, to be overwritten, must be virtual
:
public virtual int AnyMethod()
{
throw new NotImplementedException ( "This method is not implemented" );
}
Second, it is possible to make an implementation that receives N arguments of a given type. Or even object
, but this if you like to live with emotion:
public virtual int AnyMethod(params int[] numeros)
{
throw new NotImplementedException ( "This method is not implemented" );
}
Third, you can instead derive polymorphisms with different arguments in the derived class. Something like this:
public class B: A
{
public override int AnyMethod(params int[] lista)
{
return lista.Sum(x => x);
}
public int AnyMethod(int a, int b)
{
return AnyMethod(new int[] {a, b});
}
}
Then we can have a specific method with 2 arguments and another with N.
public class Program
{
public static void Main()
{
Console.WriteLine(new B().AnyMethod(1, 2));
Console.WriteLine(new B().AnyMethod(1, 2, 3));
}
}
I made you a Fiddle .
At the request of the question's author, I'll also explain one more option, which is reintroducing methods, made by the modifier word new
in method .
new
would be the equivalent of reintroduce
of Delphi, in which we explicitly override a base-class method in the derived class. The difference to override
is that the method of the base class is can be called using base.AnyMethod
, whereas using new
the method of the base class is totally ignored.
In the example scope, if there was a public int AnyMethod(int a, int b)
method in the base class, it would look something like:
public new int AnyMethod(int a, int b)
{
return a + b;
}
This type of operator, as well as the addition of more polymorphisms in derived classes, can be prevented using modifier sealed
"in the class declaration.
I updated the Fiddle by now putting a polymorphism for three operators.