Re: About virtual and abstract method
- From: "Ben Voigt" <rbv@xxxxxxxxxxxxx>
- Date: Thu, 12 Apr 2007 08:54:13 -0500
"David Zha0" <zhplive@xxxxxxx> wrote in message
news:OCwHpFQfHHA.4596@xxxxxxxxxxxxxxxxxxxxxxx
Hi,
"when we call a virtual method, the runtime will check the instance who
called the method and then choose the suitable override method, this may
causes the performance drop down", is this right?
Yes, because the exact behavior of the call can be different for each
object. Although the JIT compiler may be able to inline virtual calls
anyway for the most common cases. This is really only a concern for short
methods, where the callvirt overhead is a large fraction of total execution
time.
And, why not use "new" instead of using "virtual"?
New creates a new method slot, so you don't override the existing method.
That is to say, that a call made through a base class (or interface)-typed
variable won't call your version.
And the last question, what is the differences between a abstract method
and
a interface?
An abstract method can exist in a class alongside non-abstract methods. For
instance:
abstract class Checksum32Computer
{
public abstract bool Calc(byte[] array, int offset, int length, out int
checksum);
public bool Calc(byte[] array, out int checksum) { return Calc(array, 0,
array.Length, out checksum); }
}
But you only get single inheritance of abstract classes.
.
- References:
- About virtual and abstract method
- From: David Zha0
- About virtual and abstract method
- Prev by Date: Re: Disadvantages of C#??? just curious
- Next by Date: Re: Converting to generics
- Previous by thread: About virtual and abstract method
- Next by thread: Re: About virtual and abstract method
- Index(es):
Relevant Pages
|