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 ...
随机推荐
- Java基础06 组合
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们已经尝试去定义类.定义类,就是新建了一种类型(type).有了类,我们接着构造 ...
- POJ 1042 Gone Fishing (贪心)(刘汝佳黑书)
Gone Fishing Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 30281 Accepted: 9124 Des ...
- Eclipse用法和技巧六:自动生成get和set方法1
java的类中,除了常量声明为静态且公有的,一般的对象数据作用域,都是声明为私有的.这样做能保护对象的属性不会被随意改变,调试的时候也会方便很多:在类的公有方法中大一个调用栈就能看到哪里改了属性值.声 ...
- 24篇HTTP博客
http://www.cppblog.com/woaidongmao/category/11721.html
- Spring MVC Cookie example
In this post we will see how to access and modify http cookies of a webpage in Spring MVC framework. ...
- Beaker 1.6.4 : Python Package Index
Beaker 1.6.4 : Python Package Index Beaker 1.6.4 Download Beaker-1.6.4.tar.gz A Session and Caching ...
- 《学习opencv》笔记——矩阵和图像操作——cvSetIdentity,cvSolve,cvSplit,cvSub,cvSubS and cvSubRS
矩阵和图像的操作 (1)cvSetIdentity函数 其结构 void cvSetIdentity(//将矩阵行与列相等的元素置为1.其余元素置为0 CvArr* arr//目标矩阵 ); 实例代码 ...
- 树莓派的.bashrc和.bash_aliases文件
在你的home文件夹中,你能够找到一个包括用户配置的隐藏文件.bashrc. 你能够依据自己的须要改动这个文件. 文件里为你提供了一些实用的调整设置.默认情况下当中一些设置是被凝视掉的. 比如,一些l ...
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...
- hdu 1536 SG函数模板题
S-Nim Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...