What is the purpose of hidebysig in a MSIL method?

From ECMA 335, section 8.10.4 of partition 1:

The CTS provides independent control
over both the names that are visible
from a base type (hiding) and the
sharing of layout slots in the derived
class (overriding). Hiding is
controlled by marking a member in the
derived class as either hide by name
or hide by name-and-signature. Hiding
is always performed based on the kind
of member, that is, derived field
names can hide base field names, but
not method names, property names, or
event names. If a derived member is
marked hide by name, then members of
the same kind in the base class with
the same name are not visible in the
derived class; if the member is marked
hide by name-and-signature then only a
member of the same kind with exactly
the same name and type (for fields) or
method signature (for methods) is
hidden from the derived class.
Implementation of the distinction
between these two forms of hiding is
provided entirely by source language
compilers and the reflection library;
it has no direct impact on the VES
itself.

(It’s not immediately clear from that, but hidebysig means “hide by name-and-signature”.)

Also in section 15.4.2.2 of partition 2:

hidebysig is supplied for the use of
tools and is ignored by the VES. It
specifies that the declared method
hides all methods of the base class
types that have a matching method
signature; when omitted, the method
should hide all methods of the same
name, regardless of the signature.

As an example, suppose you have:

public class Base
{
    public void Bar()
    {
    }
}

public class Derived : Base
{
    public void Bar(string x)
    {
    }
}

...

Derived d = new Derived();
d.Bar();

That’s valid, because Bar(string) doesn’t hide Bar(), because the C# compiler uses hidebysig. If it used “hide by name” semantics, you wouldn’t be able to call Bar() at all on a reference of type Derived, although you could still cast it to Base and call it that way.

EDIT: I’ve just tried this by compiling the above code to a DLL, ildasming it, removing hidebysig for Bar() and Bar(string), ilasming it again, then trying to call Bar() from other code:

Derived d = new Derived();
d.Bar();

Test.cs(6,9): error CS1501: No overload for method 'Bar' takes '0' arguments

However:

Base d = new Derived();
d.Bar();

(No compilation problems.)

Leave a Comment