views:

324

answers:

5

I am getting the following Compiler Warning:

'Resources.Foo.GetType()' hides inherited member 'object.GetType()'. Use the new keyword if hiding was intended.    Resources.NET\Resources\Class1.cs   123 20 Resources

for this (very simplified) code:

public interface IFoo
{
    int GetType();
    string GetDisplayName();
}

public class Foo : IFoo
{
    public int GetType() { return 3; }   *** Warning: points to here (line 123)***
    public string GetDisplayName() { return "Foo"; }
}

Note, what I don't get is why the GetDisplayName() function does NOT get a warning. I am clearly overlooking something.

I have poked around Stack Overflow and googled this error and it seems to apply to class inheritance, not interfaces. I am really confused why this would be triggered for the interface (which defines its methods as virtual).

Thanks in advance for the insight.

+10  A: 

The GetType method is defined on the System.Object class, which is the ultimate base class of all classes of the Framework.

This method is used to get the Type of the current instance in runtime, and I think that you are not intending to override it (you have a int return type).

I suggest you to rename the method to GetFooType or something else, or implement your interface explicitly to avoid runtime problems.

CMS
+2  A: 

It not complaining about your implementation of the interface, but rather your reimplementation of GetType() that's already on "object" - from which Foo automatically inherits. "Object" does not have a GetDisplayName method and therefore raises no complaint for that one. Try renaming the method or adding in the "new" modifier if you really want to hide object.GetType() though it's not recommended.

Arj
+3  A: 

All types in .NET derive from System.Object.

System.Object already defines a method named GetType. If you define another method 'GetType' in your class and if someone creates an object of your class, they can never call the GetType method made available by System.Object. This is the reason you are getting the warning that 'GetType' hides the method defined in System.Object. You don't get this warning for GetDisplayName because System.Object(or for that matter any parent class) does not define this method.

I would recommend that you change your method name to something else as generally .NET developers expect GetType method call to refer to System.Object implementation.

SolutionYogi
+4  A: 

It refers to your method hiding GetType from object, not from your interface. Since your class inherits object and implements IFoo, and both provide a GetType method, by implicitly implementing the interface you will hide the GetType inherited from object. If you still want your interface method to be called GetType, you can implement it explicitly instead in Foo:

int IFoo.GetType()
{
    return 3;
}
Fredrik Mörk
A: 

System.Object class already has the GetType() method

you should use the new or override keyword to solve this problem

Omu