Search code examples
c#functionreturnthisextension-methods

use extension function to change variable value


Is there a way to use an extension method to change a variable to the return value?

I have a basic remap function

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);

I want distance to have the value of the return without explicitly setting it like

distance = distance.Remap(0, 2, 120, 80);


Solution

  • ref Should do this just fine.

    By making the this float value a ref parameter, You can alter the input by setting it as your return value.

     public static float Remap(this ref float value, float min1, float max1, float min2, float max2)
        => value = (value - min1) / (max1 - min1) * (max2 - min2) + min2;