在类型转换时常会遇到隐式转换和显式转换。那我们自己定义的类型要怎样去定义隐式转换和显式转换?我们来看一段代码

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转换的更多相关文章

  1. C# 自己定义 implicit和explicit转换

    explicit 和 implicit 属于转换运算符,如用这两者能够让我们自己定义的类型支持相互交换explicti 表示显式转换.如从 A -> B 必须进行强制类型转换(B = (B)A) ...

  2. C++雾中风景5:Explicit's better than implicit.聊聊Explicit.

    关于Explicit还是Implicit一直是编程语言中能让程序员们干起架的争议.那些聪明的老鸟总是觉得Implicit的规则让他们能够一目十行,减少样板代码的羁绊.而很多时候,Implicit的很多 ...

  3. 【转】C#中的implicit 和 explicit

    The implicit and explicit keywords in C# are used when declaring conversion operators. Let's say tha ...

  4. Scala中的Implicit(隐式转换,隐式参数,隐式类)

    文章来自:http://www.cnblogs.com/hark0623/p/4196452.html  转发请注明 代码如下: /** * 隐式转换 隐式参数 隐式类 */ //隐式转换 class ...

  5. implicit和 explicit关键字

    implicit 关键字用于声明隐式的用户定义类型转换运算符. 如果可以确保转换过程不会造成数据丢失,则可使用该关键字在用户定义类型和其他类型之间进行隐式转换. class Digit { publi ...

  6. Implicit and Explicit Multithreading MULTITHREADING AND CHIP MULTIPROCESSORS

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION The concept of thread ...

  7. implicit和explicit的基本使用

    class MyAge { public int Age { get; set; } public static implicit operator MyAge(int age) { return n ...

  8. 操作符(运算符)重载 或者叫 二元运算符 operator + 与 转换式操作符 implicit operator explicit operator

    static void Main(string[] args) { rational r1 = new rational(5); rational r2 = new rational(51); rat ...

  9. C#自定义转换(implicit 或 explicit)

    C#的类型转换分为显式转换和隐式转换,显式转换需要自己声明转换类型,而隐式转换由编译器自动完成,无需我们声明,如: //long需要显式转换成int long l = 1L; int i = (int ...

随机推荐

  1. uboot代码1:uboot启动大体流程, stage1 + stage2

    start.S stage 1: reset: set the cpu to svc32 mode disable the watchdog mask all IRQs(INTMSK, INTSUBM ...

  2. BZOJ 1112: [POI2008]砖块Klo1112( BST )

    枚举每个长度为k的区间, 然后用平衡树找中位数进行判断, 时间复杂度O(nlogn). 早上起来精神状态不太好...连平衡树都不太会写了...果断去看了会儿番然后就A了哈哈哈 ------------ ...

  3. 幻世(OurDream)2D图形引擎使用教程9——处理操作输入(3)

    声明:本教程版权归Lizcst Software Lab所有,欢迎转载,但是转载必须保留本段声明文字,并注明文章来源:http://blog.csdn.net/kflizcst 谢谢合作! 现在我们该 ...

  4. virtualbox从.VDI备份文件新建/恢复虚拟机(包括恢复各个备份节点)

    一.前言: ubuntu上的virtualbox中的虚拟机如果关机不当会导致整个虚拟机坏掉,而且采用各种debug方式都难以让它重新启动.这时你只能用之前备份的各个VDI文件来恢复系统了.还有另一种场 ...

  5. python编写网络抓包分析脚本

    python编写网络抓包分析脚本 写网络抓包分析脚本,一个称手的sniffer工具是必不可少的,我习惯用Ethereal,简单,易用,基于winpcap的一个开源的软件 Ethereal自带许多协议的 ...

  6. hdu1104 Remainder bfs找算式是否有解……

    须要注意的是,进行模运算剪枝-- #include<iostream> #include<queue> #include<cstdlib> #include< ...

  7. Netty IO线程模型学习总结

    Netty框架的 主要线程是IO线程.线程模型的好坏直接决定了系统的吞吐量.并发性和安全性. Netty的线程模型遵循了Reactor的基础线程模型.以下我们先一起看下该模型 Reactor线程模型 ...

  8. fzu 2035 Axial symmetry(枚举+几何)

    题目链接:fzu 2035 Axial symmetry 题目大意:给出n个点,表示n边形的n个顶点,判断该n边形是否为轴对称图形.(给出点按照图形的顺时针或逆时针给出. 解题思路:将相邻两个点的中点 ...

  9. QT使用scrollarea显示图片,完美解决方案

    需求: 在界面上点击“显示图片”按钮,会调用scrollarea窗口显示图片,窗口大小能根据图片大小自动调整,但是最大为1024*768,图片过大就要有滚动条来显示 IDE环境: QT Creator ...

  10. Swift - 字符串(String)用法详解

    下面对String常用的属性和方法做个总结 1,判断是否为空:isEmpty 1 2 3 var str:String if str.isEmpty{ } 2,获取字符数量:countElements ...