【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();
        }
    }
}

结果

相关推荐
倔强青铜33 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
u_topian4 小时前
【个人笔记】Qt使用的一些易错问题
开发语言·笔记·qt
珊瑚里的鱼4 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
AI+程序员在路上4 小时前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt
xingshanchang4 小时前
Matlab的命令行窗口内容的记录-利用diary记录日志/保存命令窗口输出
开发语言·matlab
Risehuxyc4 小时前
C++卸载了会影响电脑正常使用吗?解析C++运行库的作用与卸载后果
开发语言·c++
时光追逐者5 小时前
C#/.NET/.NET Core技术前沿周刊 | 第 46 期(2025年7.7-7.13)
c#·.net·.netcore
AI视觉网奇5 小时前
git 访问 github
运维·开发语言·docker
不知道叫什么呀5 小时前
【C】vector和array的区别
java·c语言·开发语言·aigc
mit6.8245 小时前
Why C# and .NET are still relevant in 2025
c#