C#运算符的重载

以下是C#中的运算符

类别

运算符

类别

运算符

算术运算符

+ - * / %

逻辑运算符

& | ^ ~ && || !

字符串连接运算符

+

增量和减量运算符

++ --

移位运算符

<<  >>

比较运算符

==  != <>  <=  >=

赋值运算符

= + += -= *= /= %= &=...

成员访问运算符

.

索引器运算符

[]

数据类型转换运算符

()

条件(三元)运算符

?:

委托连接、删除预算符

+ -

对象创建运算符

new

类型信息运算符

sizeof  is  typeof  as

溢出异常控制运算符

checked unchecked

命名空间限定符

::

间接寻址运算符

* -> & []

空接合运算符

? ??

运算符关键字

sizeof(用于1.01.1)

运算符

* -> &


1.运算符的重载

class Fraction
    {
        int num, den;
        public Fraction(int num, int den)
        {
            this.num = num;
            this.den = den;
        }

        // overload operator +
        public static Fraction operator +(Fraction a, Fraction b)
        {
            return new Fraction(a.num * b.den + b.num * a.den,
               a.den * b.den);
        }

        // overload operator *
        public static Fraction operator *(Fraction a, Fraction b)
        {
            return new Fraction(a.num * b.num, a.den * b.den);
        }

        // user-defined conversion from Fraction to double
        public static implicit operator double(Fraction f)
        {
            return (double)f.num / f.den;
        }
    }

    class Test
    {
        static void Main()
        {
            Fraction a = new Fraction(1, 2);
            Fraction b = new Fraction(3, 7);
            Fraction c = new Fraction(2, 3);
            Console.WriteLine((double)(a * b + c));
        }
    }
    /*
    Output
    0.880952380952381
    */

2. implicit 关键字用于声明隐式的用户定义类型转换运算符。

        class Digit
        {
            public Digit(double d) { val = d; }
            public double val;
            // ...other members

            // User-defined conversion from Digit to double
            public static implicit operator double(Digit d)
            {
                return d.val;
            }
            //  User-defined conversion from double to Digit
            public static implicit operator Digit(double d)
            {
                return new Digit(d);
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Digit dig = new Digit(7);
                //This call invokes the implicit "double" operator
                double num = dig;
                //This call invokes the implicit "Digit" operator
                Digit dig2 = 12;
                Console.WriteLine("num = {0} dig2 = {1}", num, dig2.val);
                Console.ReadLine();
            }
        }
    }


3.explicit 关键字用于声明必须使用强制转换来调用的用户定义的类型转换运算符。

class Celsius
{
    public Celsius(float temp)
    {
        degrees = temp;
    }
    public static explicit operator Fahrenheit(Celsius c)
    {
        return new Fahrenheit((9.0f / 5.0f) * c.degrees + 32);
    }
    public float Degrees
    {
        get { return degrees; }
    }
    private float degrees;
}

class Fahrenheit
{
    public Fahrenheit(float temp)
    {
        degrees = temp;
    }
    // Must be defined inside a class called Farenheit:
    public static explicit operator Celsius(Fahrenheit f)
    {
        return new Celsius((5.0f / 9.0f) * (f.degrees - 32));
    }
    public float Degrees
    {
        get { return degrees; }
    }
    private float degrees;
}

class MainClass
{
    static void Main()
    {
        Fahrenheit f = new Fahrenheit(100.0f);
        Console.Write("{0} fahrenheit", f.Degrees);
        Celsius c = (Celsius)f;

        Console.Write(" = {0} celsius", c.Degrees);
        Fahrenheit f2 = (Fahrenheit)c;
        Console.WriteLine(" = {0} fahrenheit", f2.Degrees);
    }
}
/*
Output:
100 fahrenheit = 37.77778 celsius = 100 fahrenheit
*/




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值