.net转换关键字:operator、explicit与implicit (转)

operatorexplicitimplicit 很少用到,但也不能不知道,发现这篇写很好,一篇

operator

operator 关键字用于在构声明中声明运算符。运算符声明可以采用下列四形式之一:

1.             public static result-type operator unary-operator ( op-type operand )

2.             public static result-type operator binary-operator (

3.                 op-type operand,

4.                 op-type2 operand2

5.                 )

6.             public static implicit operator conv-type-out ( conv-type-in operand )

7.             public static explicit operator conv-type-out ( conv-type-in operand )

参数

1.         result-type 运算符的型。

2.         unary-operator 下列运算符之一:+ - ! ~ ++ — true false

3.         op-type 第一个(或唯一一个)参数的型。

4.         operand 第一个(或唯一一个)参数的名称。

5.         binary-operator 其中一个:+ - * / % & | ^ << >> == != > < >= <=

6.         op-type2 第二个参数的型。

7.         operand2 第二个参数的名称。

8.         conv-type-out 转换运算符的目标类型。

9.         conv-type-in 转换运算符的型。

注意

1.         前两形式声明了用的重内置运算符的运算符。并非所有内置运算符都可以被重可重的运算符)。op-type op-type2 中至少有一个必闭类(即运算符所属的型,或理解自定)。例如,将防止重定整数加法运算符。

2.         后两形式声明了转换运算符。conv-type-in conv-type-out 中正好有一个必是封闭类型(即,转换运算符只能从它的封闭类转换为其他某个型,或从其他某个转换为它的封闭类型)。

3.         运算符只能采用参数,不能采用 ref out 参数。

4.         C# 要求成运算符。如果重==也必!=,否则产编译错误。同,比运算符必返回bool型的是与其他算运算符的根本区

5.         C# 不允=运算符,但如果重例如+运算符,编译器会自使用+运算符的重+=运算符的操作。

6.         运算符重的其就是函数重。首先指定的运算表达式对应的运算符函数,然后再将运算运算符函数的参,接着根据参的型来确定需要用的函数的重程是由编译器完成。

7.         任何运算符声明的前面都可以有一个可属性(C# 程指南)列表。

explicit

explicit 关键字用于声明必使用转换用的用转换运算符。

static implicit operator target_type { source_type identifier }

参数

1.         target_type 标类

2.         source_type 型。

3.         identifier Something

注意

1.         转换运算符将源转换为标类型。源型提供转换运算符。与转换不同,必过强转换的方式来转换运算符。如果转换操作可能致异常或失信息,则应将其标记为 explicit可以防止编译器无提示地用可能生无法预见后果的转换操作。

implicit

implicit 关键字用于声明式的用义类转换运算符。

static implicit operator target_type { source_type identifier }

注意

1.         转换可以通消除不必要的转换来提高源代的可性。但是,因可以在程序未指定的情况下转换,因此必注意防止令人不愉快的后果。一般情况下,转换运算符当从不引异常并且从不失信息,以便可以在程序不知的情况下安全使用它。如果转换运算符不能足那些条件,则应将其标记为 explicit

示例

以下是一个合示例,要展示用法。如要更具体细节的了解,MSDN Library

// keywords_operator.cs
// keywords_operator.cs

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();
        }
    }
}

/*
控制台出:
r3 = 
4866
float f = 48.66
r4 = 
4865
*/

发现r4果少了一分是因在:

uint fen = (uint)(((f - yuan) * 100) % 10);

句中,在将float转换为uint时发生了错误算机以二制存)。解决错误,我可以使用System.Convert中用于理数字的静方法:

uint fen = Convert.ToUInt32(((f - yuan) * 100) % 10);

使用System.Convert理会有些性能的

 

转载于:https://www.cnblogs.com/scorpioyyy/archive/2008/10/06/1304915.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值