Access to a class property without using the dot operator

I need to overload some operators when called using double types. To do this, I create a class MyDouble, which inherits from Double. MyDouble looks something like this:

class MyDouble : Double { Double value; // operator overloads go here } 

I want to abstract the value property from the user so that it can be used as a double. Basically I want the user to be able to do this:

 MyDouble a = 5; //a.value gets assigned 5 Console.WriteLine(a); //prints a.value 

I do not want the user to specifically target the value property. Is it possible? How can i do this?

+2
source share
1 answer

You can define an implicit conversion operator, for example:

 class MyDouble { public Value {get; private set;} public Double(double value) { Value = value; } // Other declarations go here... public static implicit operator double(MyDouble md) { return md.Value; } public static implicit operator MyDouble(double d) { return new MyDouble(d); } } 
+5
source

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


All Articles