How is Versioning Achieved in C# (C Sharp)Versioning in C# is all about maintaining compatibility between versions of base class and derived class even when it gets evolved in future. Versioning is achieved using three modifiers: virtual, new and override. This article will give you an overview on why versioning is needed and how it is achieved.
Why is Versioning Required? Assume that
you have the following classes: In this example,
you have a class called baseClass. Class derivedClass derives from baseClass
and it has its own method called sampleMethod. At a later point of time,
baseClass also defines a method called sampleMethod as shown below: } Now baseClass and derivedClass have same method called sampleMethod. This gives an assumption that derivedClass has overridden baseClass method sampleMethod. Is this assumption right? No, because derivedClass has an independent method and it is no way related to baseClass method. How do you avoid this confusion? You have to change the name of sampleMethod either in baseClass or derivedClass and change the corresponding method calls. What if both the classes belong to different libraries and owned by two developers? Then coordination is required. All such manual work can be avoided and compatibility can be easily achieved using versioning. How is Versioning Achieved? As already mentioned, versioning is achieved using virtual, new and override modifiers. Consider the following example: public class
baseClass { } } All that you have to do is: Mark
the base class methods as virtual In this example, derivedClass is overriding baseClass method sampleMethod1 and derivedClass has a new method sampleMethod2 that has no dependencies with sampleMethod2 of baseClass. If neither new nor override keyword is used in derivedClass for any of the methods, then a warning message will be displayed. Using these keywords, you will have a clear idea on which all methods are overridden. At a later point of time even if you introduce a method in base class that is already there in derived class, derived class method will be associated with new keyword. Hence it means that derived class method implementation is new and no way related to base class implementation. Thus compatibility is achieved.
FREE
Subscription
Subscribe
to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________ |