标签: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