Search code examples
c#clrcil

What is the purpose of newslot attribute?


I am learning intermediate language via Common Language Infrastructure book. It states that virtual method of type can be marked with newslot attribute. But that attribute really confuses me, I cannot understand its meaning.

Here is quotation:

II.10.3 - Introducting and overriding virtual methods

A virtual method of a base type is overridden by providing a direct implementation of the method (using a method definition, see §II.15.4) and not specifying it to be newslot (§II.15.4.2.3). An existing method body can also be used to implement a given virtual declaration using the .override directive (§II.10.3.2).

II.10.3.1 Introducing a virtual method

A virtual method is introduced in the inheritance hierarchy by defining a virtual method (§II.15.4). The definition can be marked newslot to always create a new virtual method for the defining class and any classes derived from it:

1) If the definition is marked newslot, the definition always creates a new virtual method, even if a base class provides a matching virtual method. A reference to the virtual method via the class containing the method definition, or via a class derived from that class, refers to the new definition (unless hidden by a newslot definition in a derived class). Any reference to the virtual method not via the class containing the method definition, nor via its derived classes, refers to the original definition.

2) If the definition is not marked newslot, the definition creates a new virtual method only if there is not virtual method of the same name and signature inherited from a base class.

It follows that when a virtual method is marked newslot, its introduction will not affect any existing references to matching virtual methods in its base classes.

What is the meaning of phrase "definition creates a new virtual method"? I thought that introduction of virtual method always creates new method. I thought that newslot is useless attribute. Can anyone provide examples to clarify that text, please?


Solution

  • Are you familiar with the new modifier in method definitions in C#? - same sort of concept.

    You're saying "this method is not the same as any method that my base class exposes, either now or in the future". Even where we have used the exact same name. People calling method X through variables of my base class or further up my inheritance chain get method X from my base class. People calling method X on variables of my class or classes derived from it get this method X (unless the derived classes introduce their own newslot X method)

    What's key here is that at the IL level, overriding is the assumed default, if an overridable method is available:

    If the definition is not marked newslot, the definition creates a new virtual method only if there is not virtual method of the same name and signature inherited from a base class.

    At the IL level, you can mark an override as virtual, whereas in C# you cannot. That's why newslot is the important distinguishing feature.