この例では、 Point4D 構造体の等価性と不等値をテストする方法を示します。
次のコードは、Point4D等値メソッドを使用して、Point4D構造体の等価性と不等値をテストする方法を示しています。
Point4D構造体は、オーバーロードされた等値 (==) 演算子を使用して等しいかどうかをテストしてから、オーバーロードされた等値 (!=) 演算子を使用して不等値を調べ、最後にPoint3D構造体とPoint4D構造体が静的なEquals メソッドを使用して等しいかどうかをチェックします。
例
// instantiate Points
Point4D point4D1 = new Point4D();
Point4D point4D2 = new Point4D(15, 40, 60, 75);
Point3D point3D1 = new Point3D(15, 40, 60);
// result variables
Boolean areEqual;
Boolean areNotEqual;
String stringResult;
// defining x,y,z,w of point1
point4D1.X = 10;
point4D1.Y = 5;
point4D1.Z = 1;
point4D1.W = 4;
// checking if Points are equal
areEqual = point4D1 == point4D2;
// areEqual is False
// checking if Points are not equal
areNotEqual = point4D1 != point4D2;
// areNotEqual is True
if (Point4D.Equals(point4D1, point3D1))
{
// the if condition is not true, so this block will not execute
stringResult = "Both objects are Point4D structures and they are equal";
}
else
{
// the if condition is false, so this branch will execute
stringResult = "Parameters are not both Point4D strucutres, or they are but are not equal";
}
こちらも参照ください
GitHub で Microsoft と共同作業する
このコンテンツのソースは GitHub にあります。そこで、issue や pull request を作成および確認することもできます。 詳細については、共同作成者ガイドを参照してください。
.NET Desktop feedback