Comparing Version Number In C#

This is example to comparing different version number in c#

Many times, in our application like desktop, mobile, we require comparing the different version numbers of an application. As the application version number contains many details, like major, minor, build, revision details, this kind of version format is difficult to compare.
.NET has provided a class named Version to do so. Go through the details here.
  1. var localVersion = new Version("1.3");    
  2. var serverVersion = new Version("1.3");    
  3. var result = serverVersion.CompareTo(localVersion);    
  5. Console.WriteLine(result);   // result: 0  
  8. /* Example 2 */  
  10. var localVersion = new Version("1.2");  
  11. var serverVersion = new Version("1.3");  
  12. var result = serverVersion.CompareTo(localVersion);  
  14. Console.WriteLine(result); // result: 1  
As you can see from the above example, we can simply pass a version number to constructor of Version Class and using CompareTo method, we can compare 2 different versions.
Following are a few more overloaded constructors of the Version class.
  1. public Version(string version);  
  2. public Version(int major, int minor);  
  3. public Version(int major, int minor, int build);  
  4. public Version(int major, int minor, int build, int revision);  
So, we can also pass major, minor, build and revision number to Version constructor and can compare 2 different versions easily.