[246] Redundant Qualifier?

I just noticed a section of code that overrides Equals and also uses a call to object.Equals that seem to be erroneously highlighted as a redundant qualifier. The basic code is like this:

public class Test
{
    public override bool Equals(object obj)
    {
        ...

        if(base.GetType() != object.GetType())
        {
            return false;
        }

        if(!object.Equals(var, obj.Var)
        {
            return false;
        }

        ...
        return true;
    }
}

Removing the qualifier may result in an infinitely recursive call and definitely alters behavior. I'm also curious if any of you understand what the call to base.GetType() is actually doing here? This class does not inherit from any other classes, and I can't quite make out whether base.GetType() is a redundant qualifier to Test.GetType() or to object.GetType().

I've also noticed similar situations in which a call to a base class method from a non-overridden method is highlighted as a redundant qualifier, but the call to the base class method was intentional and provides slightly different functionality. Unfortunately I lost my place while debugging the code and haven't found that particular code again yet.

2 comments
Comment actions Permalink
  • the base qualifier is definitely redundant

  • object.GetType() should give you a compile error, you probably meant obj.GetType()

  • the object qualifier is redundant as well, as you call the static overload with 2 arguments

  • GetType() is implemented in the object class and is not virtual, so they always call the same implementation, so all qualifiers are redundant (unless you use the keyword new somewhere)

  • The call to GetType() is a more or less standard part of any Equals implementations to prevent an instance of a subtype to become equal to an instance of the supertype.

  • calls to virtual methods are indeed altered by using the base qualifier, in which case it is indeed not redundant. There was an error in R# flagging this as redundant. I don't know if it is still there. BTW: IMHO the only place where you should use the base qualifier is in constructors (may even be a: must use) or in the overriding method itself. Though it will probably be possible to come up with a situation where you might want the base call from another method in your class.



HTH,
Erwin

  "Lothan" <lothan@newsguy.com> schreef in bericht news:ecbu6u$7f3$1@is.intellij.net...
  I just noticed a section of code that overrides Equals and also uses a call to object.Equals that seem to be erroneously highlighted as a redundant qualifier. The basic code is like this:

  public class Test
  {
      public override bool Equals(object obj)
      {
          ...

          if(base.GetType() != object.GetType())
          {
              return false;
          }

          if(!object.Equals(var, obj.Var)
          {
              return false;
          }

          ...
          return true;
      }
  }

  Removing the qualifier may result in an infinitely recursive call and definitely alters behavior. I'm also curious if any of you understand what the call to base.GetType() is actually doing here? This class does not inherit from any other classes, and I can't quite make out whether base.GetType() is a redundant qualifier to Test.GetType() or to object.GetType().

  I've also noticed similar situations in which a call to a base class method from a non-overridden method is highlighted as a redundant qualifier, but the call to the base class method was intentional and provides slightly different functionality. Unfortunately I lost my place while debugging the code and haven't found that particular code again yet.

0
Comment actions Permalink

wrote in message news:eccmsh$575$1@is.intellij.net...
 

  • the base qualifier is definitely redundant

 

  • object.GetType() should give you a compile error, you probably meant obj.GetType()

 

  • the object qualifier is redundant as well, as you call the static overload with 2 arguments

 

  • GetType() is implemented in the object class and is not virtual, so they always call the same implementation, so all qualifiers are redundant (unless you use the keyword new somewhere)

 

  • The call to GetType() is a more or less standard part of any Equals implementations to prevent an instance of a subtype to become equal to an instance of the supertype.

 

  • calls to virtual methods are indeed altered by using the base qualifier, in which case it is indeed not redundant. There was an error in R# flagging this as redundant. I don't know if it is still there. BTW: IMHO the only place where you should use the base qualifier is in constructors (may even be a: must use) or in the overriding method itself. Though it will probably be possible to come up with a situation where you might want the base call from another method in your class.



  HTH,
  Erwin

    "Lothan" <lothan@newsguy.com> schreef in bericht news:ecbu6u$7f3$1@is.intellij.net...
    I just noticed a section of code that overrides Equals and also uses a call to object.Equals that seem to be erroneously highlighted as a redundant qualifier. The basic code is like this:

    public class Test
    {
        public override bool Equals(object obj)
        {
            ...

            if(base.GetType() != object.GetType())
            {
                return false;
            }

            if(!object.Equals(var, obj.Var)
            {
                return false;
            }

            ...
            return true;
        }
    }

    Removing the qualifier may result in an infinitely recursive call and definitely alters behavior. I'm also curious if any of you understand what the call to base.GetType() is actually doing here? This class does not inherit from any other classes, and I can't quite make out whether base.GetType() is a redundant qualifier to Test.GetType() or to object.GetType().

    I've also noticed similar situations in which a call to a base class method from a non-overridden method is highlighted as a redundant qualifier, but the call to the base class method was intentional and provides slightly different functionality. Unfortunately I lost my place while debugging the code and haven't found that particular code again yet.

0

Please sign in to leave a comment.