Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-1185 Overriding methods should do more than simply call the same method in the super class
  3. RSPEC-3454

C#: Overriding members should do more than simply call the same member in the base class

    XMLWordPrintable

    Details

    • Type: Language-Specification
    • Status: Active
    • Resolution: Unresolved
    • Labels:
      None
    • Message:
      Remove this [method|property] "XXXX" to simply inherit its behavior.
    • Impact:
      Unknown 'null' severity
    • Likelihood:
      Unknown 'null' severity

      Description

      Overriding a method just to call the same method from the base class without performing any other actions is useless and misleading. The only time this is justified is in sealed overriding methods, where the effect is to lock in the parent class behavior. This rule ignores overrides of Equals and GetHashCode.

      NOTE: In some cases it might be dangerous to add or remove empty overrides, as they might be breaking changes.

      Noncompliant Code Example

      public override void Method() // Noncompliant
      {
        base.Method(); 
      }
      

      Compliant Solution

      public override void Method()
      {
        //do something else
      }
      

      Exceptions

      If there is an attribute in any level of the overriding chain, then the overridden member is ignored.

      public class Base
      {
        [Required]
        public virtual string Name { get; set; }
      }
      
      public class Derived : Base
      {
        public override string Name
        {
          get 
          { 
            return base.Name; 
          }
          set 
          { 
            base.Name = value; 
          }
        }
      }
      

      If there is a documentation comment on the overriding method, it will be ignored:

      public class Foo : Bar
      {
          /// <summary>
          /// Keep this method for backwards compatibility.
          /// </summary>
          public override void DoSomething()
          {
              base.DoSomething();
          }
      }
      

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            ann.campbell.2 Ann Campbell
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated: