Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I assign a suffix to my custom value type?

For money I am using a custom value type which holds only one decimal field. Simplified code is as follows.

public struct Money
{
    private decimal _value;

    private Money(decimal money)
    {
        _value = Math.Round(money, 2, MidpointRounding.AwayFromZero);
    }

    public static implicit operator Money(decimal money)
    {
        return new Money(money);
    }

    public static explicit operator decimal(Money money)
    {
        return money._value;
    }
}

While using this struct in my project sometimes an ambiguity arises. And also sometimes I am setting an object with a constant number which is supposed to be a Money. For now I am initializing the object like,

object myObject=(Money)200;

Can I assign a suffix for my custom type Money. I'd like to initialize the object with the following.

object myObject=200p;
like image 405
serdar Avatar asked Jul 09 '14 12:07

serdar


1 Answers

You can't assign custom suffixes with C#. The closest thing you can do is creating extension method for integers:

public static Money Para(this int value) // you can do same for decimals
{
   return (Money)((decimal)value);
}

Usage:

var amount = 200.Para();
like image 138
Sergey Berezovskiy Avatar answered Oct 08 '22 06:10

Sergey Berezovskiy