您的位置:首页 > 编程语言 > C#

c# implicit explicit关键字(隐式和显式数据类型转换)

2014-10-06 19:29 776 查看
implicit关键字用于声明隐式的用户定义类型转换运算符。(explicit反之)explicit则用于显示转换用户自定义类型。
static implicit operator target_type ( source_type identifier ){......}
隐式转换可以通过消除不必要的类型转换来提高源代码的可读性。但是,因为可以在未指定的情况下发生隐式转换,因此必须注意防止令人不愉快的后果。

一般情况下,隐式转换运算符应当从不引发异常并且从不丢失信息,以便可以在不知晓的情况下安全使用它们。如果转换运算符不能满足那些条件,则应将其标记为 explicit 作为显示转换数据。

下边是在网上找的几个简单例子

例1

class Digit
{
public Digit(double d) { val = d; }
public double val;

// User-defined conversion from Digit to double
public static implicit operator double(Digit d)
{
return d.val;
}
//  User-defined conversion from double to Digit
public static implicit operator Digit(double d)
{
return new Digit(d);
}
}
class Program
{
static void Main(string[] args)
{
Digit dig = new Digit(7);
//This call invokes the implicit "double" operator
double num = dig;
//This call invokes the implicit "Digit" operator
Digit dig2 = 12;
Console.WriteLine("num = {0} dig2 = {1}", num, dig2.val);
Console.ReadLine();
}
}


例2

//基本数据类型到用户自定义类型
class Distance
{
private int feet;
private double inches;

//默认构造函数
public Distance()
{
feet = 0;
inches = 0.0;
}

//带有单参数的构造函数
public Distance(double metres)
{
double f;
f = 3.28 * metres;
this.feet = (int)f;
this.inches = 12 * (f - feet);
}

//由一个double隐式构造一个Distance
public static implicit operator Distance(double metres)
{
return new Distance(metres);
}

//由一个Distance显式返回一个double
public static explicit operator double(Distance d)
{
double metres;
metres = d.inches / 12 + (double)d.feet;
return (metres / 3.28);
}

public override string ToString()
{
return String.Format("{0}英尺{1}英寸 ", this.feet, this.inches);
}

}

class DistanceDemo
{
public static void Main()
{
Distance d1 = 1.25;
Console.WriteLine(d1);

double d = (double)d1;
Console.WriteLine(d);
}
}


例3

using System;

namespace Hunts.Keywords
{
// 定义一个人民币结构。数据类型转换的语法对于结构和类是一样的
public struct RMB
{
// 注意:这些数的范围可能不能满足实际中的使用
public uint Yuan;
public uint Jiao;
public uint Fen;
public RMB(uint yuan, uint jiao, uint fen)
{
if (fen > 9)
{
jiao += fen / 10;
fen = fen % 10;
}
if (jiao > 9)
{
yuan += jiao / 10;
jiao = jiao % 10;
}
this.Yuan = yuan;
this.Jiao = jiao;
this.Fen = fen;
}

public override string ToString()
{
return string.Format("¥{0}元{1}角{2}分", Yuan, Jiao, Fen);
}

// 一些操作
public static RMB operator +(RMB rmb1, RMB rmb2)
{
return new RMB(rmb1.Yuan + rmb2.Yuan, rmb1.Jiao + rmb2.Jiao, rmb1.Fen + rmb2.Fen);
}

public static implicit operator float(RMB rmb)
{
return rmb.Yuan + (rmb.Jiao / 10.0f) + (rmb.Fen / 100.00f);
}

public static explicit operator RMB(float f)
{
uint yuan = (uint)f;
uint jiao = (uint)((f - yuan) * 10);
uint fen = (uint)(((f - yuan) * 100) % 10);
return new RMB(yuan, jiao, fen);
}

// more
}

class App
{
static void Main()
{
RMB r1, r2, r3, r4;
// 记得小学时的某次捐款,我把口袋里藏好的一块钱加6张一毛钱以及13个一分钱的硬币都贡献出去了:(
r1 = new RMB(1, 6, 13);
// 其实当时其他人都已经交过了,他们总共交了:
r2 = new RMB(46, 9, 3);
// 那么加上我的就是:
r3 = r1 + r2;
Console.WriteLine("r3 = {0}", r3.ToString());
// 隐式转换
float f = r3;
Console.WriteLine("float f= {0}", f);
// 显式转换
r4 = (RMB)f;
Console.WriteLine("r4 = {0}", r4.ToString());
//如果不进行显示转换,将出现错误 CS0266: 无法将类型“float”隐式转换为“Hunts.Keywords.RMB”。存在一个显式转换(是否缺少强制转换?)
Console.Read();
}
}
}


出处:http://www.cnblogs.com/chengxiaohui/articles/1914190.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: