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 ...
随机推荐
- QWidget类中默认是忽略inputMethodEvent事件(要获取输入的内容就必须使用这个事件)
因为项目的需要以及主管的要求,准备将工程移植到Qt中,这样就可以比较容易的实现跨平台了.因为之前工程是在windows下开发的,第一个平台又是mobile所以除了底层框架之外其他的都是使用的windo ...
- Android-x86 4.4-r5 发布,PC 上的安卓系统
Android x86 即运行于 x86 PC上的Android操作系统,目前已经支持大部分安卓程序. Android X86平台是由Beyounn和Cwhuang主持设计的.项目的主要目的在于为X8 ...
- 基于perl面向对象开发的微信机器人
<pre name="code" class="html">[root@wx03 lib]# ls -ltr total 40 -rw-r--r-- ...
- jquery mobile左右滑动切换页面
jquery mobile左右滑动切换页面 $(function() {$("body").bind('swiperight', function() { $.mobile.ch ...
- IntelliJ Idea 经常使用快捷键列表
Alt+回车 导入包,自己主动修正Ctrl+N 查找类Ctrl+Shift+N 查找文件Ctrl+Alt+L 格式化代码 Ctrl+Alt+O 优化导入的类和包Alt+Insert 生成代码(如 ...
- linux环境: shell初始化文件, for TCSH, CSH
TCSHELL, CSHELL 配置文件 全局配置文件 /etc/csh.cshrc个人配置文件 ~/.cshrc或~/.tcshrc 参考: 1.配置你的csh/tcsh, https://wik ...
- linux shell脚本:在脚本中实现读取键盘输入,根据输入判断下一步的分支
echo please input “runbip” to run bip. variableName="null" while [ $variableName != " ...
- Qt递归拷贝和删除目录
最近在翻看项目代码时,看到了这两个函数,想到这个功能十分常用,因此拿出来与大家分享,希望对大家有用.几点说明: 1.记得当初写代码那会,是参考了网上的帖子写的,做了一点小修改.因此代码源于网络. 2. ...
- haproxy timeout server 46000 后台超时时间
[root@wx03 ~]# sh ./1.sh Wed Jul 6 19:54:40 CST 2016 <html><body><h1>504 Gateway T ...
- spring mvc接收JSON格式的参数
1.配置spring解析json的库 <dependency> <groupId>org.codehaus.jackson</groupId> ...