8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

.NETのVersionクラスで1.0と1.0.0を比較しようとして嵌った

8
Last updated at Posted at 2016-12-02

そもそものバージョン情報に関する規約でそうなってるのかもしれないけどちょっと面白かったのでメモ

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var v1 = new Version("1.0");
            var v2 = new Version("1.0.0");
            var v3 = new Version("1.0");

            Console.WriteLine(v1.CompareTo(v2));
            Console.WriteLine(v2.CompareTo(v1));
            Console.WriteLine(v1.CompareTo(v3));
            Console.Read();
        }
    }
}

Version.CompareToは、引数として渡されたバージョンよりも自身のほうが新しければ1, 同じであれば0, 古ければ-1を返すようになっているので、出力として期待していたのは

0
0
0

だった
しかし、実際の出力は

-1
1
0

となっていたので、0であってもBuildがついているほうが新しいと判定されるらしい
変数の中身を出力してみると

キャプチャ.PNG

v1では指定していないはずのBuildの部分が-1になっていた
なので、実質v1 = 1.0.-1v2 = 1.0.0の比較になるのでv2のほうが新しいと判定されている様子

8
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?