I have knowledge of java and have been learning c# for the last couple of days. Now I have come across the "virtual" keyword which, as suggested at this link, is used to allow the corresponding methods, properties etc. to be overriden in the subclasses. Now I think we can override methods even without using the "virtual" keyword. Then why it is necessary?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
You need the
virtual
keyword if you really want tooverride
methods in sub classes. Otherwise the base implementation will be hidden by the new implementation, just as if you had declared it with thenew
keyword.Hiding the methods by "overriding" them without the base method being declared
virtual
leaves you without polymorphism, that means: if you "cast" a specialized version to the "base" version and call a method, always the base classes implementation will be used instead of the overridden version - which is not what you'd expect.Example:
virtual
is a way of defining that a method has a default implementation, but that that implementation may be overriden in a child class. Other than by using virtual, you cannot override a method directly without using thenew
keyword (which is generally bad practice).A good example of the implementation of
virtual
is theToString()
method. Every object in C# is guaranteed to be able to callToString()
because every object inherits from the baseSystem.Object
class, which contains a virtual methodToString()
. Derived classes can override this however, and provide their own implementation which may be more useful to the users of the object.Update: I recently wrote a blog post which goes into this topic in a bit of depth. Check it out here.
No, you can't. Contrary to Java, in C# members are sealed by default and you cannot override them unless you marked them with the
virtual
keyword.Take a look at this example:
In this example the subclass
S
is instantiated and assigned to object variableo1
. In theConsole.WriteLine
statement's parameter it is being cast into the base classB
, then methodm1
is called. Because we have usedvirtual
in the base classB
andoverride
in the subclassS
, we're gettingas output. If you remove
virtual
in the method declaration ofm1
inB
andoverride
in the subclassS
then you're gettingas output, which means that the cast also has the effect that the original declaration of method
m1
in the base classB
is used.N.B. If you're using the
new
keyword in subclassS
, such asassuming that the
virtual
keyword in the base classB
is absent, you're getting the outputas well. If you would not cast it to
B
, the new method would be used. This is called shadowing (or hiding) a method (the original method of the base class).Summary:
To override a method, which should be effective also if you cast to the base class, use the
virtual
keyword in the base class andoverride
in the sub class.If you intend to override the method, which should be active in the sub class only, use the
new
keyword in the sub classes method declaration. As you have seen it works also without it, but it is better if it is there so everyone knows that this is a new version of the method.