Consider this example,
public class NumberAsIs
{
public NumberAsIs(double number) { _number = number; }
public double Value => _number;
private double _number;
}
public class Percentage
{
public Percentage(double number) { _number = number; }
public double Value => _number * 0.01;
private double _number;
}
class Program
{
static void Main(string[] args)
{
var a = new NumberAsIs(1);
var b = new Percentage(2);
var c = a.Value + b.Value; // Expect c = 1.02
}
}
Because the only purpose of NumberAsIs and Percentage is to call them for their Value method, is there a way to just call them as value types? For example,
class Program
{
static void Main(string[] args)
{
var a = new NumberAsIs(1);
var b = new Percentage(2);
double c = a + b; // Expect c = 1.02
}
}
I'm not looking to redefine the operator+, but to access the numeric value just by calling the object. If it's not possible just fine and say no, asking just in case there is a workaround/sugar syntax I'm not aware of.
The question is roughly related to this other: Value type class definition in C#?