I have a generics class that I used to write data to IsolatedStorage.
I can use an static implicit operator T() to convert from my Generic class to the Generic Parameter T
e.g.
MyClass<double> foo = new MyClass(187.0);
double t = foo;
My question is, how can I do the reverse?
MyClass<double> foo = new MyClass(187.0);
double t = 0.2d;
foo = t;
The implicit operator has to be static, so I’m not sure how I can pass in the instance of my class?
EDIT:
If you want to be able to change the value of
Tin your class, I would recommend exposing it as a property like:That will allow you to change the value, instead of the behavior of the implicit operator returning an entirely new instance of the class.
You can and can’t using implicit operators. Doing something like
will implicitly convert from
MyTypetointand frominttoMyTyperespectively. However, you’re right, since they are static, theinttoMyTypespecifically will have to create a new instance ofMyTypeand return that.So this code:
wouldn’t replace the value in
foowitht, the implicit operator would return an entirely newMyClassfromtand assign that toFoo.