【C#】explicit、implicit与operator

字面解释

explicit:清楚明白的;易于理解的;(说话)清晰的,明确的;直言的;坦率的;直截了当的;不隐晦的;不含糊的。

implicit:含蓄的;不直接言明的;成为一部分的;内含的;完全的;无疑问的。

operator:操作人员;技工;电话员;接线员;(某企业的)经营者,专业公司。

专业解释

explicit用于强制转换,implicit用于隐式转换

用法

public static 返回的结果类型 operator unary-operator (参数类型 param)
unary-operator:+ - ! ~ ++ — true false
public static 返回的结果类型 operator binary-operator (参数类型 param1, 参数类型 param)
binary-operator:+ - * / % & | ^ << >> == != > < >= <=
public static implicit operator 返回的结果类型 (参数类型 param )
public static explicit operator 返回的结果类型 (参数类型 param )

explicit、implicit都是与operator一起操作使用的,operator 关键字用于在类或结构声明中声明运算符。

示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConversionOperator
{
    public class IntDouble
    {
        private readonly int intV;
        private readonly double doubleV;

        public IntDouble(int value) : this(value, 0)
        {
        }

        public IntDouble(double value) : this(0, value)
        {
        }

        public IntDouble(int intV, double doubleV)
        {
            this.intV = intV;
            this.doubleV = doubleV;
        }
        //将IntDouble类型隐式转为int类型,返回int类型
        public static implicit operator int(IntDouble intdouble)
        {
            return intdouble.intV;
        }
        //将IntDouble类型显式转为double类型,返回double类型
        public static explicit operator double(IntDouble intdouble)
        {
            return intdouble.doubleV;
        }
        //将int类型隐式转化为IntDouble
        public static implicit operator IntDouble(int intdouble)
        {
            return new IntDouble(intdouble);
        }
        //将double类型显式转化为IntDouble
        public static explicit operator IntDouble(double intdouble)
        {
            return new IntDouble(intdouble);
        }

        public static IntDouble operator ++(IntDouble intdouble)
        {
            var t = intdouble.intV + 1;
            var t2 = intdouble.doubleV + 1;
            var temp = new IntDouble(t, t2);
            return temp;
        }

        public override string ToString()
        {
            return $"intV:{intV},doubleV:{doubleV}";
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            IntDouble doubleV = (IntDouble)2.1;
            Console.WriteLine($"原始数据:{doubleV}");
            doubleV++;
            //此处IntDouble显示转为double类型
            double c = (double)doubleV;
            //此处IntDouble隐示转为int类型
            int c2 = doubleV;
            Console.WriteLine($"int的值:{c2},double的值:{c}");
            Console.WriteLine($"{doubleV}");
            Console.ReadKey();
        }
    }
}

结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Z_W_H_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值