Introduction
Everyone knows that in .NET, byte
, char
, int
, long
, etc. are value data types and string
is a reference data type. Even though it is a reference type, string
behaves much like a value type. In other words, it can directly accept values and doesn't require new
to create an instance. For example:
string name = "M. Aamir Maniar";
So far so good. Everyone is well acquainted with this basic demeanor of the string
class. But the fact is that, most of them don't know how to incorporate such things into their own classes. To explain this concept, let's think about the Currency
class. Currency
class fundamentally has two properties:
Sign
//String
: Holds the currency sign like $,£,¥,€,Rs., etc. Value
//Decimal
: Holds the currency value
The class has one constructor which accepts two parameters, i.e. value
and sign
which set the respective properties of the class. So if you are creating an instance of the class, you will do that in the following manner:
Currency cur = new Currency(100.50, "$");
Right? Assuming $
is a default currency sign, don't you think this is a bit tedious way to instantiate such kind of data type. What about the following approach:
Currency cur = 100;
Currency cur = "€";
cur.Value = 100;
long lCur = cur;
decimal dCur = cur;
The Implicit Operator
If you want to incorporate such a feature, an implicit operator overloading comes into the picture. Yes, there is something called implicit operator overloading. According to MSDN, an implicit keyword is used to declare an implicit user-defined type conversion operator. In other words, this gives the power to your C# class, which can accepts any reasonably convertible data type without type casting. And such a kind of class can also be assigned to any convertible object or variable. If you want to create an implicit operator function, here is a signature of creating them in C#.
«access specifier» static implicit operator «converting type» («convertible type» rhs)
The above signature states that the operator accepts «convertible type» and converts into «converting type».
The following code shows you how to create them:
public static implicit operator Currency(string rhs)
{
Currency c = new Currency(0, rhs);
return c;
}
public static implicit operator Currency(decimal rhs)
{
Currency c = new Currency(rhs, NumberFormatInfo.CurrentInfo.CurrencySymbol);
return c;
public static implicit operator decimal(Currency rhs)
{
return rhs.Value;
}
public static implicit operator long(Currency rhs)
{
return (long)rhs.Value;
}
Behind the Scene
Such kind of implicit operator overloading is not supported by all languages, then how does C# incorporate such a nice feature. The answer lies in an assembly code generated by the C# compiler. The following table shows the C# syntax with the corresponding IL syntax.
C# declaration | IL Declaration |
public static implicit operator
Currency(decimal rhs)
|
.method public hidebysig specialname static
class Currency op_Implicit(valuetype [mscorlib]
System.Decimal rhs) cil managed
|
public static implicit operator
decimal(Currency rhs)
|
.method public hidebysig specialname static
valuetype [mscorlib]System.Decimal op_Implicit
(class Currency rhs) cil managed
|
The IL syntax in the above table makes it clear that the C# compiler generates op_Implicit
function which returns «converting type» and accepts «converting type».
Hope this article is useful to you.
Visit my blog here.
History
- 15th August, 2006: Initial post
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.