As everybody knows using virtual keyword for a method in a class allows us to override it in its derived sub-class. However, method still has to stick to the same signature e.g. return value, argument and etc.
Question: in C#, how can I can override the signature as well? is this possible?
Thanks.
It is not possible. You can create an overload, though, if you need it.
Edit
To make my case against
new, please consider the case where you have a base object, and one that inherits from it. By placingnewbefore the method of which you’re attempting to “change” the method signature, you’re actually wasting four key strokes.Now,
Inheritedhas two methods:You are not actually changing the method signature. Thus, the
newkeyword isn’t even needed. You will get the same effect without it:The use of
newis to override a non-virtual member of a base class:But you will run into issues with this if you don’t pay attention to this subtlety. With
virtualyou get a virtual look-up table that will map your functions to the correct call. Withnew, you will not. If you pass inInheritedwhere aBaseobject is required (this will work, of course), theBaseclass’sFoo()will be called! Not, theInherited“override” ofFoo().This is not the same behavior as overriding a virtual member.