■ オペレータ(Operator)
* 演算子のオーバーロード
* 独自作成したクラスなどに使えそう
構文
public static 戻り値の型 operator演算子 (引数)
例
public static bool operator ==(DataList source1, DataList source2)
{
return ... // boolの戻り値
}
public class DataList {}
■ サンプル
public class DataList
{
public DataList(int age, string name)
{
this.age = age;
this.name = name;
}
private int age;
private string name;
public static bool operator ==(DataList source1, DataList source2)
{
return source1.age == source2.age
&& source1.name == source2.name;
}
public static bool operator !=(DataList source1, DataList source2)
{
return source1.age != source2.age
|| source1.name != source2.name;
}
}
private void buttonC_Click(object sender, EventArgs e)
{
DataList a1 = new DataList(12, "Mike");
DataList a2 = new DataList(12, "Mike");
DataList a3 = new DataList(22, "Paul");
if (a1 == a2)
{
this.label1.Text = "True";
}
else
{
this.label1.Text = "False";
}
if (a1 == a3)
{
this.label2.Text = "True";
}
else
{
this.label2.Text = "False";
}
}