CS0809: Obsolete member overrides non-obsolete member

C# compiler warning CS0809 "Obsolete member overrides non-obsolete member" exists to tell us that marking an override as [Obsolete] is pointless. The warning is essentially saying that if you add the [Obsolete] attribute to an override, hoping to see diagnostics on invocations of that override, you will be disappointed, because there will be none, due to the way the compiler works.

However, contrary to what this warning is trying to say, I am here to tell you that adding [Obsolete] to an override is not pointless; it is actually quite useful. Read on to see why it is useful, and why you should therefore suppress warning CS0809.

What you might expect

Consider the following program:

using System;

class B
{
	public virtual void M() { }
}

class D : B
{
	[Obsolete( "Do not call this." )]
	public override void M() { }
}

static class Program
{
	static void Main()
	{
		D d = new D();
		d.M(); // you might expect a warning here due to the obsolete attribute above.
	}
}

Depending on exactly how you use the [Obsolete] attribute, you might expect one of these:

Attribute on D.M()Diagnostic you might expect
on invocations of d.M()
[Obsolete]warning CS0612
[Obsolete( "Do not call this." )]warning CS0618
[Obsolete( "Do not call this.", true )]error CS0619

What happens instead

When you try to compile the above code, you get the following warning:

Program.cs(11,23): warning CS0809: Obsolete member 'D.M()' overrides non-obsolete member 'B.M()'

In other words, the compiler does not issue a diagnostic on the invocation of d.M(); instead, it issues warning CS0809 on the declaration of D.M().

Why warning CS0809 exists

According to the section §12.5 Member lookup of the C# language specification, when the C# compiler resolves a method invocation, it collects the candidate members, and then discards those which include an override modifier.

So, d.M() binds to B.M(), the basemost declaration of the method, even though the variable is of type D. (At runtime, of course, virtual dispatch will still invoke D.M().)

Consequently, the compiler does not consider the [Obsolete] attribute on the override. This means that if you mark an override as obsolete, expecting invocations of it to be flagged with CS0612, CS0618, or CS0619, you will be surprised, because no such diagnostics will be issued.

That is what CS0809 is trying to tell you: your [Obsolete] attribute has no effect on an override.

Why you should suppress it nonetheless

Here is a plot twist: Visual Studio is not bound by the C# language specification, and it does not do as the C# compiler does.

When the syntax highlighter of Visual Studio examines a method invocation, it considers the actual override being invoked; and if that override is marked as obsolete, it renders the invocation with the styling for obsolete members.

Therefore, marking an override as obsolete does have a benefit: anyone reading or writing code that invokes it will see that it is obsolete.

That is why there is actually merit in using the [Obsolete] attribute on overrides that should never be directly invoked, and why CS0809 should therefore be disabled:

#pragma warning disable CS0809 // Obsolete member overrides non-obsolete member
	[Obsolete( "Do not call this." )]
	public override void M() { }
#pragma warning restore CS0809

If you would prefer to not have #pragma warning clauses all over your code base, you can do this once and for the entire project, in the .csproj file:

<PropertyGroup>
	<NoWarn>$(NoWarn);CS0809</NoWarn>
</PropertyGroup>

But why?

At this point you might be asking: why would anyone want to mark an override as obsolete?

I explain this one here: C# methods you are better off never calling.

Made using Obsidian and Hugo
Theme based on hugo-theme-stack by Jimmy Cai
Privacy policy ·  Cookie settings