c#保留关键字implicit和explicit的区别

一、介绍 

implicit用于隐式转换用户定义类型,explicit显示转换用户定义类型。

  • explicit 关键字用于声明必须使用强制转换来调用的用户定义的类型转换运算符。
  • implicit 关键字用于声明隐式的用户定义类型转换运算符。 如果可以确保转换过程不会造成数据丢失,则可使用该关键字在用户定义类型和其他类型之间进行隐式转换。

 上代码

创建实体Student

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public DateTime CreateTime { get; set; }
}

 

public class StudentDto
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string CreateTime { get; set; } 
}

假设有需求将实体Student 转换成 StudentDto实体,可以看到StudentDto实体跟Student属性相比,少了Id属性及CreateTime类型不同,哪我们初次遇到此情况会怎么做,可能会单独写个装换方法如下:

public StudentDto Convert(Student stu)
{
     return new StudentDto
     {
         Name = stu.Name,
         Age = stu.Age,
         CreateTime = stu.CreateTime.ToShortDateString()
     };
}

其实c#提供一个较方便的功能,请看代码

二、implicit

public class StudentDto
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string CreateTime { get; set; }
 
        /// <summary>
        /// 可以隐式将 Student 类型转为 StudentDto 类型
        /// </summary> 
        /// <param name="stu"></param>
        public static implicit operator StudentDto(Student stu)
        {
            return new StudentDto
            {
 
                Name = stu.Name,
                Age = stu.Age,
                CreateTime = stu.CreateTime.ToShortDateString()
            };
        }
    }

 使用: 可以看到直接就可以将Stuent类型 赋值个StudentDto

 static void Main(string[] args)
        {
            Student stu = new Student();
            stu.Age = 25;
            stu.CreateTime = DateTime.Now;
            stu.Id = 1;
            stu.Name = "乔武林";
            StudentDto stuDto = stu;
            Console.WriteLine(stuDto.Name);
            Console.Read();
        }

三、explicit

 

 public class StudentDto
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string CreateTime { get; set; }
 
        /// <summary>
        /// 可以显示将 Student 类型转为 StudentDto 类型
        /// </summary>
        /// <param name="stu"></param>
        public static explicit operator StudentDto(Student stu)
        {
            return new StudentDto
            {
 
                Name = stu.Name,
                Age = stu.Age,
                CreateTime = stu.CreateTime.ToShortDateString()
            };
        } 
    }

 使用:Student想要装换StudentDto类型需要 强制类型 装换 

        static void Main(string[] args)
        {
            Student stu = new Student();
            stu.Age = 25;
            stu.CreateTime = DateTime.Now;
            stu.Id = 1;
            stu.Name = "乔武林";
            StudentDto stuDto = (StudentDto)stu;
            Console.WriteLine(stuDto.Name);
            Console.Read();
        }

四、补充

个人感觉现在这种方式很少有人用了吧,如果类型转换不太频繁该方式也能满足需要,可是在项目中需频繁大量进行实体类型转换,可以使用Mapster或AutoMapper。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C#中,有两个关键字implicitexplicit,用于定义类型之间的隐式和显式转换。implicit关键字可用于定义隐式转换操作符,而explicit关键字可用于定义显式转换操作符。 通过使用implicit关键字,可以定义将一个类型隐式转换为另一个类型的操作符。在代码中,可以看到一个示例类StudentDto,它定义了一个implicit操作符,用于将Student类型隐式转换为StudentDto类型。这意味着可以直接将Student类型赋值给StudentDto类型的变量,而不需要进行显式转换。 另一方面,explicit关键字用于定义将一个类型显式转换为另一个类型的操作符。在代码中,如果没有显式转换操作符,当尝试将Payment类型赋值给PaymentDTO类型的变量时,编译器会报错,提示无法隐式转换,但可以进行显式转换。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [C#中的ExplicitImplicit](https://blog.csdn.net/qq_38721111/article/details/117452332)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [c#冷门系列之implicitexplicit](https://blog.csdn.net/czjnoe/article/details/109827755)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

fanhgye

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

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

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

打赏作者

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

抵扣说明:

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

余额充值