Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
#DOTNET 33
#.NET 26
#ASP.NET 25
ASP.NET 24
brh 22
#C# 14
.NET 13
WCF 11
c# 9
#MultiThreading 7

Archive · View All
January 2011 10
September 2011 6
May 2011 6
December 2011 5
October 2011 5
June 2011 5
February 2011 3
November 2012 2
August 2012 2
April 2012 2

Implicit operator overloading

Jan 20 2011 8:50PM by Neeraj Kaushik   

An implicit keyword is used to declare an implicit user-defined type conversion operator. In other word, this gives a power to your C# class, which can accepts any reasonably convertible data type without type casting. And such kind of class can also be assigned to any convertible object or variable. If you want to create implicit operator function, here is a signature of creating them in C#.
«access specifier» static implicit operator «converting type» («convertible type» rhs)
Above signature states that operator accepts «convertible type» and converts into «converting type».

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 scene

Such kind of implicit operator overloading is not supported by all languages, then how does csharp incorporate such a nice feature. The answer lies in an assembly code generated by the csharp compiler. Following table shows the C# syntax with 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

IL syntax in above table makes it clear that C# compiler generates op_Implicit function which returns «converting type» and accepts «converting type».

Tags: #.NET, #DOTNET, #ASP.NET, ASP.NET, DotNet, c#, brh,


Neeraj Kaushik
55 · 4% · 1132
1
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

"Implicit operator overloading" rated 5 out of 5 by 1 readers
Implicit operator overloading , 5.0 out of 5 based on 1 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]