The signature of override method is same as in the original base class System.Object, Since signature only includes the name of method and the types and number of parameter it takes.
Why it is necessary to having same return type for overriding a method?
using System;
namespace Testing_Override_Keyword
{
class OverridingClass
{
public override int ToString()//**Here is error**
{
return 1;
}
}
}
This is error
Return type must be string to match overridden member
Why it is necessary to having same return type for overriding a method?
In once sense, "because that's what the language specification says". From the C# 5 specification section 10.6.4:
A compile-time error occurs unless all of the following are true for an override declaration:
- ...
- The override method and the overridden base method have the same return type.
In another sense, "because otherwise it wouldn't make sense". The point is that a caller should be able to call the virtual method - including using the return value - without caring whether or not it's overridden.
Imagine if someone had written:
object x = new OverridingClass();
string y = x.ToString();
How would that work with your override?
See more on this question at Stackoverflow