C#/Xamarin Save max read value of Variable

Solution 1:

Move the declaration out of the loop and then 2 choices:

if (x > maxvalue) maxvalue = x; 

or

maxvalue = Math.Max(maxvalue, x);

Now you reset maxvalue to 0.0 in every iteration so every value > 0 becomes maxvalue.

So

float maxvalue = 0.00;
while (device == connected)
{
     float x = (float) read.device(1);     
     maxvalue = Math.Max(maxvalue, x);
}

I recommend using Math.Max always instead of if statement as as it also handles corner cases for floats like NaN.