I asked questions about what I think might be the solution , but someone pointed out that I am getting into the XY problem and that I should just ask about my exact problem.
I have a structure that I want other people to use in their programs. It is necessary to be able to implicitly convert this type from other existing types, but at the same time, some information must be stored after this assignment. Here is a simple example of a problem:
using System;
public struct MyStruct {
public string SomethingImportant;
public MyStruct(string s) {
SomethingImportant = s;
}
public bool SomeFunction(string s) {
return s == SomethingImportant;
}
public static implicit operator MyStruct(double x) {
return new MyStruct();
}
}
public class MyClass {
MyStruct child = new MyStruct("important");
public MyClass() {
Console.WriteLine(child.SomethingImportant);
child = 7.5;
Console.WriteLine(child.SomethingImportant);
}
}
After the structure is replaced with a new structure from an implicit conversion, the information stored in SomethingImportant
will be lost. This would be a natural place to overload the assignment operator, but unfortunately this is not possible in C #.
, , , . , , , , .
- , #? , , MyStruct.Update(double x)
, , , , , . - , , .
!