C#中的运算符重载,(急……)
C#中的运算符重载:<和>=是一对吗?也就是在重载<运算符时,也必须重载>=运算符,对吗?还是重载>运算符? --------------------编程问答-------------------- public class Int{
public Int32 i;
public static Boolean operator <(Int i1, Int i2)
{
if (i1.i < i2.i)
return true;
else
return false;
}
}
如果这样,错误如下:
Error requires a matching operator '>' to also be defined --------------------编程问答-------------------- <和>是一对
这两个必须同时重载
--------------------编程问答--------------------
public class Int
{
public Int32 i;
public static Boolean operator <(Int i1, Int i2)
{
if (i1.i < i2.i)
return true;
else
return false;
}
public static Boolean operator >(Int i1, Int i2)
{
if (i1.i > i2.i)
return true;
else
return false;
}
}
static void Main(string[] args)
{
Int i1 = new Int();
Int i2 = new Int();
i1.i = 1;
i2.i = 2;
if (i1 < i2)
{
Console.WriteLine("i1 < i2");
}
else
{
Console.WriteLine("i1 >= i2");
}
}
正确的 --------------------编程问答-------------------- <>、<=和>=分别是两对比较运算符。。。。。后面的自己应该懂了。。 --------------------编程问答-------------------- 谢了,我的疑虑解除了,这下我明白了。
补充:.NET技术 , C#