I’m working on a custom struct and I would like to give it the ability to implicitly be created from another type.
Say I have two struct types. Color and ColorX, where Color is a struct already in the framework that I cannot change.
Using implicit operator, to be able to say for example.
Color C;
ColorX CX;
CX = new ColorX();
C = CX;
However, I would like to be able to do it the other way around as well. Either by directly setting it, or by making a cast. Being able to do both would be gold. For example.
C = new Color();
CX = C;
or cast it like so:
CX = (ColorX)C;
Consider all the other useful operators in C#, I’m sure there is a way to do this, I just can’t find the syntax.
Any help is greatly appreciated! Thank you very much.
You would do this with the implicit operator in C#: