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?
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:
All that you have to do is:
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.
to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________
to our mailing list and receive new articles
: We never rent, trade, or sell my email lists to
Visit .NET Programming Tutorial Homepage
| All about Conceptual Analysis on .NET Remoting | Building desktop applications in .Net | Building Distributed Applications Efficiently Using .Net Remoting | C# (C Sharp) Unified Type System: Reference Types | C# (C Sharp) Unified Type System: Value Types | Data access in .Net | Delegates Vs Interfaces in C# (C Sharp) | How is Integration between Java and .NET Achieved | How is Versioning Achieved in C# (C Sharp) | Implementing Authorization and authentication in ASP.net | Implementing design patterns in .Net | List of Query Keywords in C# (C Sharp) |