Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's this syntax called? SomeMoneyFormat f = "€ 5,00";

Tags:

c#

I have seen some form of this some time ago, but I cannot recall what it was called, and therefore have no clue on how to implement something like this:

SomeMoneyFormat f = "€ 5,00";

Which calls some overload function that can parse the string into a SomeMoneyFormat object.

like image 303
Jan Jongboom Avatar asked May 17 '10 15:05

Jan Jongboom


2 Answers

When you do not specify that it should be cast it is an implicit cast

   public static implicit operator SomeMoneyFormat(string d) 
   {
      return new SomeMoneyFormat(d);
   }

Then € 5,00 is passed as the string d

more about this here: http://msdn.microsoft.com/en-us/library/z5z9kes2(VS.71).aspx

Also, I can add that this should only be done when there is no risk of losing data. For example converting a double to an int will lose some precision, so it is an explicit cast. Otherwise it would be easy to cast by accident and lose data.

like image 77
Oskar Kjellin Avatar answered Oct 21 '22 17:10

Oskar Kjellin


Looks like an implicit type conversion to me.

like image 43
Jason Punyon Avatar answered Oct 21 '22 16:10

Jason Punyon