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

结果

相关推荐
老胖闲聊2 分钟前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot
Blossom.1186 分钟前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn
曹勖之36 分钟前
基于ROS2,撰写python脚本,根据给定的舵-桨动力学模型实现动力学更新
开发语言·python·机器人·ros2
豆沙沙包?1 小时前
2025年- H77-Lc185--45.跳跃游戏II(贪心)--Java版
java·开发语言·游戏
军训猫猫头1 小时前
96.如何使用C#实现串口发送? C#例子
开发语言·c#
liuyang-neu2 小时前
java内存模型JMM
java·开发语言
不爱写代码的玉子3 小时前
HALCON透视矩阵
人工智能·深度学习·线性代数·算法·计算机视觉·矩阵·c#
我很好我还能学3 小时前
【面试篇 9】c++生成可执行文件的四个步骤、悬挂指针、define和const区别、c++定义和声明、将引用作为返回值的好处、类的四个缺省函数
开发语言·c++
蓝婷儿4 小时前
6个月Python学习计划 Day 16 - 面向对象编程(OOP)基础
开发语言·python·学习
渣渣盟4 小时前
基于Scala实现Flink的三种基本时间窗口操作
开发语言·flink·scala