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

结果

相关推荐
weixin_307779132 分钟前
在Linux服务器上使用Jenkins和Poetry实现Python项目自动化
linux·开发语言·python·自动化·jenkins
润 下2 分钟前
C语言——深入解析C语言指针:从基础到实践从入门到精通(四)
c语言·开发语言·人工智能·经验分享·笔记·程序人生·其他
Empty_7776 分钟前
Python编程之常用模块
开发语言·网络·python
小火柴12310 分钟前
利用R绘制箱线图
开发语言·r语言
wheeldown20 分钟前
【Linux】Linux 进程通信:System V 共享内存(最快方案)C++ 封装实战 + 通信案例,4 类经典 Bug 快速修复
linux·运维·服务器·开发语言
小年糕是糕手30 分钟前
【数据结构】双向链表“0”基础知识讲解 + 实战演练
c语言·开发语言·数据结构·c++·学习·算法·链表
将车24435 分钟前
C++实现二叉树搜索树
开发语言·数据结构·c++·笔记·学习
梵得儿SHI1 小时前
Java 反射机制核心类详解:Class、Constructor、Method、Field
java·开发语言·反射·class·constructor·java反射·java反射机制
hbqjzx1 小时前
记录一个自动学习的脚本开发过程
开发语言·javascript·学习
Sirens.1 小时前
Java核心概念:抽象类、接口、Object类深度剖析
java·开发语言·github