【C#】explicit、implicit与operator

字面解释

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

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

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

专业解释

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

用法

cs 复制代码
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 关键字用于在类或结构声明中声明运算符。

示例

cs 复制代码
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();
        }
    }
}

结果

相关推荐
带娃的IT创业者1 分钟前
实战:用 Python 搭建 MCP 服务 —— 模型上下文协议(Model Context Protocol)应用指南
开发语言·python·mcp
minji...4 分钟前
C++ STL之list的使用
开发语言·c++
万粉变现经纪人5 分钟前
如何解决pip安装报错ModuleNotFoundError: No module named ‘python-dateutil’问题
开发语言·ide·python·pycharm·pandas·pip·httpx
Sammyyyyy16 分钟前
macOS是开发的终极进化版吗?
开发语言·macos·开发工具
青草地溪水旁27 分钟前
23 种设计模式
开发语言·c++·设计模式
草履虫建模30 分钟前
在 RuoYi 中接入 3D「园区驾驶舱」:Vue2 + Three.js + Nginx
运维·开发语言·javascript·spring boot·nginx·spring cloud·微服务
编码浪子30 分钟前
趣味学RUST基础篇(函数式编程闭包)
开发语言·算法·rust
MC皮蛋侠客40 分钟前
使用python test测试http接口
开发语言·python·http
胡耀超1 小时前
5、Python-NumPy科学计算基础
开发语言·人工智能·python·深度学习·numpy
点灯小铭1 小时前
基于MATLAB的车牌识别系统
开发语言·单片机·数码相机·matlab·毕业设计·课程设计