Bre*_*ker 3 c# operator-overloading
有人能指出我需要实现的接口,以便让基本的数学运算符(即+, - ,*,/)在自定义类型上运行吗?
Sta*_* R. 17
您必须使用运算符重载.
public struct YourClass
{
public int Value;
public static YourClass operator +(YourClass yc1, YourClass yc2)
{
return new YourClass() { Value = yc1.Value + yc2.Value };
}
}
Run Code Online (Sandbox Code Playgroud)