Using ildasm and a C# program e.g.
static void Main(string[] args)
{
}
gives:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 2 (0x2)
.maxstack 8
IL_0000: nop
IL_0001: ret
} // end of method Program::Main
What does the hidebysig construct do?
According to Microsoft Docs
As per THE SKEET's answer, in addition the reason for this is that Java and C# allow the client of a class to call any methods with the same name, including those from base classes. Whereas C++ does not: if the derived class defines even a single method with the same name as a method in the base class, then the client cannot directly call the base class method, even if it doesn't take the same arguments. So the feature was included in CIL to support both approaches to overloading.
In C++ you can effectively import one named set of overloads from the base class with a
using
directive, so that they become part of the "overload set" for that method name.From ECMA 335, section 8.10.4 of partition 1:
(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:
As an example, suppose you have:
That's valid, because
Bar(string)
doesn't hideBar()
, because the C# compiler useshidebysig
. If it used "hide by name" semantics, you wouldn't be able to callBar()
at all on a reference of typeDerived
, 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
forBar()
andBar(string)
, ilasming it again, then trying to callBar()
from other code:However:
(No compilation problems.)