c#冷门系列之implicit和explicit

一、介绍

今天刷博客看到implicit和explicit正好复习下知识点,写下该博客作为复习证据

implicit用于隐式转换用户定义类型,explicit显示转换用户定义类型,好了废话不多说,直接上代码,一瞧便知

创建实体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

 Student stu = new Student();
 stu.Age = 25;
 stu.CreateTime = DateTime.Now;
 stu.Id = 1;
 stu.Name = "czj";

 StudentDto stuDto = stu;

三、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类型需要 强制类型 装换

                Student stu = new Student();
                stu.Age = 25;
                stu.CreateTime = DateTime.Now;
                stu.Id = 1;
                stu.Name = "czj";

                StudentDto stuDto = (StudentDto)stu;

四、全代码

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; }

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

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

    }


static void Main(string[] args)
        {
            //implicit
            {
                //Student stu = new Student();
                //stu.Age = 25;
                //stu.CreateTime = DateTime.Now;
                //stu.Id = 1;
                //stu.Name = "czj";

                //StudentDto stuDto = stu;
            }
            //explicit
            {
                Student stu = new Student();
                stu.Age = 25;
                stu.CreateTime = DateTime.Now;
                stu.Id = 1;
                stu.Name = "czj";

                StudentDto stuDto = (StudentDto)stu;
            }

            Console.ReadKey();
        }

五、补充

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

Mapster可以参考我的文章 https://blog.csdn.net/czjnoe/article/details/106820749

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值