Can I assign a suffix to my custom value type?

For money, I use a custom value type that contains only one decimal field. The 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; } } 

When using this structure in my project, ambiguity sometimes occurs. And also sometimes I set object with a constant number, which should be Money . At the moment, I am initializing the object, for example,

 object myObject=(Money)200; 

Is it possible to assign a suffix to my custom Money type. I would like to initialize the object as follows.

 object myObject=200p; 
+6
source share
1 answer

You cannot assign your own suffixes using C #. The closest you can do is create an extension method for integers:

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

Using:

 var amount = 200.Para(); 
+7
source

Source: https://habr.com/ru/post/971973/


All Articles