提问人:Matheus Tambosi 提问时间:11/21/2022 更新时间:11/21/2022 访问量:54
使用扩展函数更改变量值 [duplicate]
use extension function to change variable value [duplicate]
问:
有没有办法使用扩展方法将变量更改为返回值?
我有一个基本的重映射函数
public static float Remap(this float value, float min1, float max1, float min2, float max2)
=> (value - min1) / (max1 - min1) * (max2 - min2) + min2;
distance.Remap(0, 2, 120, 80);
我希望距离具有返回值,而无需显式设置它,例如
distance = distance.Remap(0, 2, 120, 80);
答:
1赞
Robbert-Jan Koornstra
11/21/2022
#1
ref
应该这样做就好了。
通过创建 a 参数,
您可以通过将输入设置为返回值来更改输入。this float value
ref
public static float Remap(this ref float value, float min1, float max1, float min2, float max2)
=> value = (value - min1) / (max1 - min1) * (max2 - min2) + min2;
评论
ReMap
float
struct Distance { public Distance(float value) => this.value = value; private float value; }