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:
public class baseClass { }
public class derivedClass : baseClass {
public void sampleMethod( ) {
Console.WriteLine(“Executing sampleMethod of derivedClass”’);
}
}

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:
public class baseClass {
public void sampleMethod( ) {
Console.WriteLine(“Executing sampleMethod of baseClass”’);
}

}

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 {
public virtual void sampleMethod1( ) {
Console.WriteLine(“Executing sampleMethod1 of baseClass”’);
}
public virtual void sampleMethod2( ) {
Console.WriteLine(“Executing sampleMethod2 of baseClass”’);
}

}
public class derivedClass : baseClass {
public override void sampleMethod1( ) {
Console.WriteLine(“Executing sampleMethod1 of derivedClass”’);
}
public new void sampleMethod2( ) {
Console.WriteLine(“Executing sampleMethod2 of derivedClass”’);
}

}

All that you have to do is:

• Mark the base class methods as “virtual”
• If the derived class is overriding a base class method then mark the derived class method using “override” keyword
• If the derived class is implementing a new method irrespective of the base class method, then mark the derived class method using the keyword “new”

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.

| 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) |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.