✅ Writing a scuffed way to use an "assignment" (=) operator
Obviously, there's no such thing as an assignment operator. I'd like to find some way around that. I have a class that looks something like this:
struct MyType<T> { public int AField; public T Value { get { // Stuff to do with AField } set { // Stuff to do with AField } } public Type<T>(int aField) { AField = aField; }}
struct MyType<T> { public int AField; public T Value { get { // Stuff to do with AField } set { // Stuff to do with AField } } public Type<T>(int aField) { AField = aField; }}
I'd like to be able to assign values to this:
MyType<float> myType = new(20);// What I want:myType = 30.5f;// What I have to do at the moment:myType.Value = 30.5f;
MyType<float> myType = new(20);// What I want:myType = 30.5f;// What I have to do at the moment:myType.Value = 30.5f;
Is there any hacky, bodged, whatever way of doing this? I can't do it like this, obviously, because operators are static:
public static implicit operator MyType<T>(T value) { MyType<T> ret = new(this.AField); ret.Value = value; return ret;}
public static implicit operator MyType<T>(T value) { MyType<T> ret = new(this.AField); ret.Value = value; return ret;}