|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionEveryone knows that in .NET, string name = "M. Aamir Maniar"; //Directly accepts the value.
So far so good. Everyone is well acquainted with this basic demeanor of the
The class has one constructor which accepts two parameters, i.e.
Currency cur = 100; //Currency directly accepts value
//Instantiating currency using currency sign.
Currency cur = "€";
cur.Value = 100; //Implicit typecasting from number to Currency
//What about this
long lCur = cur; //Implicit typecasting from Currency to long
decimal dCur = cur; //Implicit type casting from Currency to decimal
The Implicit OperatorIf 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: /// <summary>
/// Creates Currency object from string supplied as currency sign.
/// </summary>
/// <param name="rhs">The currency sign like $,£,¥,€,Rs etc. </param>
/// <returns>Returns new Currency object.</returns>
public static implicit operator Currency(string rhs)
{
Currency c = new Currency(0, rhs); //Internally call Currency constructor
return c;
}
/// <summary>
/// Creates a currency object from decimal value.
/// </summary>
/// <param name="rhs">The currency value in decimal.</param>
/// <returns>Returns new Currency object.</returns>
public static implicit operator Currency(decimal rhs)
{
Currency c = new Currency(rhs, NumberFormatInfo.CurrentInfo.CurrencySymbol);
return c;
/// <summary>
/// Creates a decimal value from Currency object,
/// used to assign currency to decimal.
/// </summary>
/// <param name="rhs">The Currency object.</param>
/// <returns>Returns decimal value of the currency</returns>
public static implicit operator decimal(Currency rhs)
{
return rhs.Value;
}
/// <summary>
/// Creates a long value from Currency object, used to assign currency to long.
/// </summary>
/// <param name="rhs">The Currency object.</param>
/// <returns>Returns long value of the currency</returns>
public static implicit operator long(Currency rhs)
{
return (long)rhs.Value;
}
Behind the SceneSuch 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.
The IL syntax in the above table makes it clear that the C# compiler generates Hope this article is useful to you. Visit my blog here. History
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||