Get Started!

C# Compare Semantic Version

How to compare a semantic version, say v2.4.11, to another, say v2.5.1, in C#.

public int CompareTo(Site other){

  // Check Major Version
  if( this.nMajorVersion < other.nMajorVersion ){
    return -1;
  }
  if( this.nMajorVersion > other.nMajorVersion ){
    return 1;
  }

  // Minor Version
  if( this.nMinorVersion < other.nMinorVersion ){
    return -1;
  }
  if( this.nMinorVersion > other.nMinorVersion ){
    return 1;
  }

  // Patch
  if( this.nPatchVersion < other.nPatchVersion ){
    return -1;
  }
  if( this.nPatchVersion > other.nPatchVersion ){
    return 1;
  }

  return 0;

}