When a virtual method is called on an object reference, the actual type of that object is used to decide which method implementation to use. When a method of a base class is overridden in a derived class, the version in the derived class is used, even if the calling code didn't know that the object was an instance of the derived class. For instance:
public class Base { public virtual void SomeMethod() { } }
public class Derived : Base { public override void SomeMethod() { } }
Base b = new Derived(); b.SomeMethod();
It will end up calling Derived.SomeMethod if that overrides Base.SomeMethod. Now, if you use the new keyword instead of override, the method in the derived class doesn't override the method in the base class, it merely hides it. In that case, code like this:
public class Base { public virtual void SomeOtherMethod() { } }
public class Derived : Base { public new void SomeOtherMethod() { } }
Base b = new Derived(); Derived d = new Derived(); b.SomeOtherMethod(); d.SomeOtherMethod();
It will first call Base.SomeOtherMethod (line 3), then Derived.SomeOtherMethod (line 4). They're effectively two entirely separate methods which happen to have the same name, rather than the derived method overriding the base method.
If you don't specify either new or overrides, the resulting output is the same as if you specified new, but you'll also get a compiler warning. |
Posted By: eTechPlanet |
Date: 30 November 2009 04:45:28 AM | The differences between the two C# keywords is due to polymorphism. When a virtual method is called on a reference, the actual type of the object to which the reference refers is used to determine which method implementation should be used. When a method of a base class is overridden in a derived class (subclass), the version defined in the derived class is used. This is so even should the calling application be unaware that the object is an instance of the derived class.
The following example illustrates the override keyword:
public class BaseClass
{
public virtual void AMethod()
{
}
}
public class DerivedClass : BaseClass
{
public override void AMethod()
{
}
}
// ...
BaseClass baseClass = new DerivedClass();
baseClass.AMethod();
In the preceding example, DerivedClass.AMethod will be called; because, it overrides BaseClass.AMethod.
If the new keyword were used instead of the override keyword, then the method in the derived class would not override the base class method, but would hide it instead. The following is an example illustrating the new keyword:
public class BaseClass
{
public virtual void AMethod()
{
}
}
public class DerivedClass : BaseClass
{
public new void AMethod()
{
}
}
// ...
BaseClass baseClass = new DerivedClass();
baseClass.AMethod();
DerivedClass derivedClass = new DerivedClass();
derivedClass.AMethod();
The preceding example will call BaseClass.AMethod first, followed by DerivedClass.AMethod. Effectively, the two methods are unrelated except for the fact that they have the same name.
If neither the new nor overrides keyword is specified, the result is the same as if the new were specified. However, a compiler warning will be generated to ensure that the programmer is aware that a method in the base class is being hidden.
Term papers |
Posted By: Johndecruse |
Date: 10 February 2010 02:33:31 AM |
|