Why Invoke BeginInvoke & EndInvoke are virtual when internal class is sealed? [duplicate]
Asked Answered
O

1

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?

Older answered 2/2, 2018 at 12:10 Comment(2)
How did you obtain that code?Lophophore
I've read in the following article: link . and when i check in the ildasm, it shows the same concept of sealed & virtual.Older
L
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.

Lophophore answered 2/2, 2018 at 12:20 Comment(1)
Ohh okay, I'm getting it. Thanks man..!Older

© 2022 - 2024 — McMap. All rights reserved.