[.net]瞭解implicit operator隱性轉型

  • 1321
  • 0
  • 2016-03-24

[.net]瞭解implicit operator隱性轉型

參考資料中的作者寫得很清楚了,所以我就直接做簡單的翻譯:

如果有一個類別叫做Currency(現金),他的member是符號(string,例如$)以及數字(decimal代表金額),傳統的方式要new的話,大概一般都是這麼寫的:

Currency cur = new Currency(100.50, "$"); 

於是乎微軟就想出了一些又酷又炫(個人是覺得不酷不炫,只是搞的很難懂拉...)的寫法,叫做implicit operator,在new的時候,可以像是下面這樣:

Currency cur = 100; //Currency directly accepts value
//Instantiating currency using currency sign.
Currency cur1 = "€";
cur1.Value = 100;    //Implicit typecasting from number to Currency

//new之後,要再修改符號或是金額也都可以直接改
cur1 = "☺";
string signSmile = cur1;
cur1 = 777;
decimal amt = cur1;


//What about this
long lCur = cur;    //Implicit typecasting from Currency to long 
decimal dCur = cur; //Implicit type casting from Currency to decimal 

上面的寫法就是可以將一個Currency物件以符號或是金額的方式直接new

而且還可以把new出來的變數直接帶入到其他的符號或是金額

Currency Class內容如下,其中寫法的技巧可以直接看註解:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TestMongoDBWebForm
{
    using System;
    using System.Globalization;

    /// 
/// Summary description for Currency. ///

 

public class Currency { decimal _value; string _sign; public Currency(decimal value, string sign) { _value = value; _sign = sign; } ///

 

/// Gets or sets the currency value. ///

 

public decimal Value { get { return _value; } set { _value = value; } } ///

 

/// Gets or sets the Currency sign. ///

 

public string Sign { get { return _sign; } set { _sign = value; } } public override string ToString() { return string.Format("{0}{1}", Sign, Value); } ///

 

/// Creates Currency object from string supplied as currency sign. /// New物件的時候,可以直接用string帶入,例如Currency cur1 = "☏" /// 或是重新設定物件的符號時,直接把符號assign給物件也可以,cur1 = "☏" ///

 

public static implicit operator Currency(string rhs) { Currency c = new Currency(0, rhs); //Internally call Currency constructor return c; } ///

 

/// Creates a currency object from decimal value. ///

 

public static implicit operator Currency(decimal rhs) { //NumberFormatInfo.CurrentInfo.CurrencySymbol是當地國家預設的金錢符號 Currency c = new Currency(rhs, NumberFormatInfo.CurrentInfo.CurrencySymbol); //Internally call Currency constructor return c; } ///

 

/// Creates a decimal value from Currency object, used to assign currency to decimal. /// 這個就是可以把Currency物件assign給一個decimal,例如:decimal amt = cur1; ///

 

public static implicit operator decimal(Currency rhs) { return rhs.Value; } ///

 

/// Creates a long value from Currency object, used to assign currency to long. ///

 

public static implicit operator long(Currency rhs) { return (long)rhs.Value; } ///

 

/// 這個是自己加的 /// 這個就是可以把Currency物件assign給一個string,例如:string mySymbol = cur1; ///

 

public static implicit operator string(Currency rhs) { return (string)rhs.Sign; } } }

 

個人覺得implicit operator是為了讓寫法更簡潔拉,如果把這個技巧應用在類別庫,那麼caller可以快速的將簡單類別(ex:string,long,decimal...)直接assign給一個物件

 

 

 

而且物件自動就會知道這個簡單類別的value要放在自己內部的哪一個欄位。

 

 

 

參考文章:

 

 

 

Understanding Implicit Operator Overloading in C#

 

 

 

http://www.codeproject.com/Articles/15191/Understanding-Implicit-Operator-Overloading-in-C