中級 › == が参照を比較してしまう理由 › パターン1
== が参照を比較してしまう理由
classの==は既定でオブジェクトの参照(同一性)を比較します。
1 class Point { public int X; public int Y; } 2 class Program { 3 static void Main(string[] args) { 4 Point a = new Point { X = 1, Y = 2 }; 5 Point b = __________________________; ^ 6 System.Console.WriteLine(a == b); 7 } 8 }
出力: False
空欄を埋めてビルドを通す
クラスの==は既定では参照の比較です。フィールドの値が同じでも別のインスタンスならfalseになります。
次の問題