C#之自己定义的implicit和explicit转换
在类型转换时常会遇到隐式转换和显式转换。那我们自己定义的类型要怎样去定义隐式转换和显式转换?我们来看一段代码
public class Rational
{
private Int32 _inner_int = 0; public Rational()
{ } public Rational(Int32 num)
{
this._inner_int = num;
} public Int32 ToInt32() { return this._inner_int; } // Implicitly constructs and returns a Rational from an Int32
public static implicit operator Rational(Int32 num)
{
return new Rational(num);
} // Explicitly returns an Int32 from a Rational
public static explicit operator Int32(Rational r)
{
return r.ToInt32();
} public override string ToString()
{
//return base.ToString();
String s = String.Format("{0}", this._inner_int);
return s;
}
}
測试代码
class Program
{
static void Main(string[] args)
{
Rational r1 = 10;
Console.WriteLine(r1); Int32 i = r1;
Console.WriteLine(i);
Console.ReadLine();
}
}
这时编辑会报错,见下图
从提示能够看到,是由于Int32 i=r1时缺少了显式转换。如今我们加入显示转换,改动后的代码及输出结果例如以下:
结果正常输出为10.
那为什么会这样呢?究其原因是在Rational转换成 Int32时,指定了explicit(显式的),所以必需要指定转换类型Int32。假设将explicit换成implicit(隐式),原来的代码将能够正常执行。
改动后的Rational
public class Rational
{
private Int32 _inner_int = 0; public Rational()
{ } public Rational(Int32 num)
{
this._inner_int = num;
} public Int32 ToInt32() { return this._inner_int; } // Implicitly constructs and returns a Rational from an Int32
public static implicit operator Rational(Int32 num)
{
return new Rational(num);
} // Explicitly returns an Int32 from a Rational
public static <span style="color:#ff0000;">implicit</span> operator Int32(Rational r)
{
return r.ToInt32();
} public override string ToString()
{
//return base.ToString();
String s = String.Format("{0}", this._inner_int);
return s;
}
}
測试代码及输出结果
可见explicit和implicit影响着类型的显式转换和隐式转换。
事实上在Rational r1=10已经运行了隐式转换,相应的转换代码例如以下:
// Implicitly constructs and returns a Rational from an Int32
public static implicit operator Rational(Int32 num)
{
return new Rational(num);
}
假设将implicit换成explicit,Rational r1=10也将会报错(能够自行測试)。
转载请注明出处:http://blog.csdn.net/xxdddail/article/details/38057563
C#之自己定义的implicit和explicit转换的更多相关文章
- C# 自己定义 implicit和explicit转换
explicit 和 implicit 属于转换运算符,如用这两者能够让我们自己定义的类型支持相互交换explicti 表示显式转换.如从 A -> B 必须进行强制类型转换(B = (B)A) ...
- C++雾中风景5:Explicit's better than implicit.聊聊Explicit.
关于Explicit还是Implicit一直是编程语言中能让程序员们干起架的争议.那些聪明的老鸟总是觉得Implicit的规则让他们能够一目十行,减少样板代码的羁绊.而很多时候,Implicit的很多 ...
- 【转】C#中的implicit 和 explicit
The implicit and explicit keywords in C# are used when declaring conversion operators. Let's say tha ...
- Scala中的Implicit(隐式转换,隐式参数,隐式类)
文章来自:http://www.cnblogs.com/hark0623/p/4196452.html 转发请注明 代码如下: /** * 隐式转换 隐式参数 隐式类 */ //隐式转换 class ...
- implicit和 explicit关键字
implicit 关键字用于声明隐式的用户定义类型转换运算符. 如果可以确保转换过程不会造成数据丢失,则可使用该关键字在用户定义类型和其他类型之间进行隐式转换. class Digit { publi ...
- Implicit and Explicit Multithreading MULTITHREADING AND CHIP MULTIPROCESSORS
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION The concept of thread ...
- implicit和explicit的基本使用
class MyAge { public int Age { get; set; } public static implicit operator MyAge(int age) { return n ...
- 操作符(运算符)重载 或者叫 二元运算符 operator + 与 转换式操作符 implicit operator explicit operator
static void Main(string[] args) { rational r1 = new rational(5); rational r2 = new rational(51); rat ...
- C#自定义转换(implicit 或 explicit)
C#的类型转换分为显式转换和隐式转换,显式转换需要自己声明转换类型,而隐式转换由编译器自动完成,无需我们声明,如: //long需要显式转换成int long l = 1L; int i = (int ...
随机推荐
- SSH框架之Hibernate(1)——映射关系
ORM的实现思想就是将关系数据库中表的数据映射成对象.以对象的形式展现,这样开发者就能够把对数据库的操作转化为对这些对象的操作.Hibernate正是实现了这样的思想,达到了方便开发者以面向对象的思想 ...
- android 屏幕尺寸的理解
对android设备屏幕尺寸单位的理解 一.android移动设备(手机和平板)常用的关于屏幕的一些单位: 1.px:像素点,应该是一个统一的单位,与我们国际单位米(M)应该是一回事,它应该是屏幕尺寸 ...
- WCF技术剖析之十一:异步操作在WCF中的应用(上篇)
原文:WCF技术剖析之十一:异步操作在WCF中的应用(上篇) 按照操作执行所需的资源类型,我们可以将操作分为CPU绑定型(CPU Bound)操作和I/O绑定型(I/O Bound)操作.对于前者,操 ...
- 用 Eclipse 下载 Git 仓库中代码
1. 安装 Git 插件 可以按照 通过Eclipse从subversion站点下载源码 中的方法安装,也可以在 Eclipse Marketplace 中搜索 EGit 进行安装(Help --&g ...
- 基于visual Studio2013解决面试题之1202最大公共字符串
题目
- iOS "The sandbox is not in sync with the Podfile.lock"解决方式
更新Cocoapod之后出现故障: diff: /../Podfile.lock: No such file or directory diff: Manifest.lock: No such fil ...
- linux命令:使用man, 导出man
要查一个命令怎么使用,使用"man 命令", eg: man find, man ls; "info 命令"貌似也可以看, info find, info ls ...
- SRM 582 Div II Level Two SpaceWarDiv2
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12556 #include <iostream> # ...
- 基本HTML5文件结构
作者 : Dolphin 原文地址:http://blog.csdn.net/qingdujun/article/details/28129039 基本HTML5文件结构: <!--<!D ...
- SpringMVC经典系列-12基于SpringMVC的文件上传---【LinusZhu】
注意:此文章是个人原创.希望有转载须要的朋友们标明文章出处,假设各位朋友们认为写的还好,就给个赞哈,你的鼓舞是我创作的最大动力.LinusZhu在此表示十分感谢,当然文章中如有纰漏.请联系linusz ...