Question

In CIL, what is the behaviour of a virtual method which lacks the flag virtual with respect to inheritance (hiding and overriding)? I searched through ECMA335 but couldn't find an exact description of the expected behaviour.

For example, let's have the base class A and the subclass B.

  1. If A contains a method M with the flag virtual, and B contains a method M without the flag virtual, will B.M hide A.M or will it override it?
  2. If A contains a method M with the flag virtual, and B contains a method M with .override A.M, but without the flag virtual, is the virtual implied, is B.M "virtual" only here (but, say, B.M can't be overriden in subclasses of B) or is this construction simply illegal (i.e. methods with .override must have the virtual flag)?

I'd like to know which part(s) of the spec answers these questions, so I'll be able to answer similar questions on my own in the future (I did search through it a lot before asking here).

Was it helpful?

Solution

The representation is described in §23.1.10 Flags for methods [MethodAttributes], as VtableLayoutMask. When the mask is applied to the attributes, the result will be either ReuseSlot or NewSlot. ReuseSlot corresponds to the override keyword in C#, and NewSlot corresponds to the lack of override keyword or a method explicitly marked with new.

Hiding and overriding are different concepts. For example, if a method Foo in class X overrides a method of the same name in BaseX, then X.Foo also hides BaseX.

Now for the specific 2 questions in the original post:

  1. If the CIL definition is not marked newslot, then if a method B.M hides A.M then B.M will also override A.M. (Partition II, §10.3.1)

  2. Partition II, §10.3.2 states the following (emphasis mine):

    The remaining information specifies the virtual method that provides the implementation.

    Combined with §15.4.2.2, it's clear that the .override directive can only refer to a method which is explicitly marked virtual.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top