9

I've observed that Whenever the compiler encountered a delegate declaration like the following:

public delegate string StringOperation(string myString);

Then the compiler is generating the following code:

public sealed class StringOperation: System.MulticastDelegate  
{  
   public StringOperation (object target, int method);  
   public virtual void Invoke(string myString);  
   public virtual IAsyncResult BeginInvoke(string myString,  
   AsyncCallback callback, object obj);  
   public virtual void EndInvoke(IAsyncResult result);  
}

My question is, why would it generate virtual methods when the class itself is a sealed class?

There is no point of creating virtual methods as we cannot override them right?

Rand Random
  • 7,300
  • 10
  • 40
  • 88

1 Answers1

0

I am not familiar with this piece of the framework, but Reflector gives this as the definition of BeginInvoke:

[MethodImpl(0, MethodCodeType=MethodCodeType.Runtime)]
public virtual IAsyncResult BeginInvoke(string myString, AsyncCallback callback, object @object);

My eye was drawn to the MethodImpl attribute. MethodCodeType.Runtime means:

Specifies that the method implementation is provided by the runtime.

So I guess it is virtual because the runtime will override its functionality. And the runtime could easily pass the sealed class.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325