2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-01-27 13:42:22 +01:00
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++``.
2020-06-30 12:47:33 +02:00
2021-02-02 15:02:10 +01:00
2021-02-08 19:11:39 +01:00
NOTE++:++ In some cases it might be dangerous to add or remove empty overrides, as they might be breaking changes.
2020-06-30 12:47:33 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:47:33 +02:00
----
public override void Method() // Noncompliant
{
base.Method();
}
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:47:33 +02:00
----
public override void Method()
{
//do something else
}
----
2023-05-03 11:06:20 +02:00
=== Exceptions
2020-06-30 12:47:33 +02:00
If there is an attribute in any level of the overriding chain, then the overridden member is ignored.
2021-02-02 15:02:10 +01:00
2023-05-25 14:18:12 +02:00
[source,csharp]
2020-06-30 12:47:33 +02:00
----
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:
2021-02-02 15:02:10 +01:00
2023-05-25 14:18:12 +02:00
[source,csharp]
2020-06-30 12:47:33 +02:00
----
public class Foo : Bar
{
/// <summary>
/// Keep this method for backwards compatibility.
/// </summary>
public override void DoSomething()
{
base.DoSomething();
}
}
----
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Remove this [method|property] "XXXX" to simply inherit its behavior.
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== on 7 Apr 2017, 09:53:45 Valeri Hristov wrote:
See the following blog post for additional information about breaking changes:
https://blogs.msdn.microsoft.com/ericlippert/2010/03/29/putting-a-base-in-the-middle/
include::../comments-and-links.adoc[]
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]