码迷,mamicode.com
首页 > Windows程序 > 详细

C# 关键字explicit(显示),implicit(隐式),类型的隐式和显式转换

时间:2017-05-08 01:16:55      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:ada   nbsp   code   span   public   apt   xpl   隐式   opera   

  class Program
    {
        static void Main(string[] args)
        {
            Adaptee ada = new Adaptee();
            Target tar = ada;
        }
    }

    public class Adaptee
    {
        public int code = 1;
    }
    public class Target
    {
        private int data;
        public Target(int data)
        {
            this.data = data;
        }
//可以进行隐式转换
public static implicit operator Target (Adaptee ada) { return new Target(ada.code); } }

 

 

显式转换

  class Program
    {
        static void Main(string[] args)
        {
            Adaptee ada = new Adaptee();
            Target tar = (Target)ada;
        }
    }

    public class Adaptee
    {
        public int code = 1;
    }
    public class Target
    {
        private int data;
        public Target(int data)
        {
            this.data = data;
        }
//必须显示转换
public static explicit operator Target (Adaptee ada) { return new Target(ada.code); } }

 

C# 关键字explicit(显示),implicit(隐式),类型的隐式和显式转换

标签:ada   nbsp   code   span   public   apt   xpl   隐式   opera   

原文地址:http://www.cnblogs.com/xiaoleye/p/6822934.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!