# How to get maximum value in three numbers in .net

The following code snippet is for finding maximum value among three numbers.

Example 1: Using If and Else statement
[VB.Net]

``` Private Function Getmaximum(ByVal a As Integer, ByVal b As Integer, ByVal c As Integer) As Integer
Dim MaxValue As Integer
If a > b Then
If a > c Then
MaxValue = a
Else
MaxValue = c
End If
Else
If b > c Then
MaxValue = b
Else
MaxValue = c
End If
End If
Return MaxValue
End Function```

[C#.Net]

```private int Getmaximum(int a, int b, int c)
{
int MaxValue = 0;
if (a > b)
{
if (a > c)
{
MaxValue = a;
}
else
{
MaxValue = c;
}
}
else
{
if (b > c)
{
MaxValue = b;
}
else
{
MaxValue = c;
}
}
return MaxValue;
}```

Example 2: Using List

Our valuable user James Tweedie suggest the another method to use the List and var objects(below in the comment). var type is added in the visual studio 2008 version.

```public double MinimumOf3(double d1, double d2, double d3)
{
var nums = new []{d1, d2, d3}.ToList();
nums.Sort();
return nums;
}

public double MaximumOf3(double d1, double d2, double d3)
{
var nums = new []{d1, d2, d3}.ToList();
nums.Sort();
return nums;
}```

The Sort() method will sort the values in the list object in the ascending order Than you can get the lower limit and upper limit values to find out the minimum and maximum value correspondingly.

## 3 thoughts on “How to get maximum value in three numbers in .net”

1. James Tweedie says:

Hi, This also works and avoids if..then..else:

public double MinimumOf3(double d1, double d2, double d3)
{
var nums = new []{d1, d2, d3}.ToList();
nums.Sort();
return nums;
}

public double MaximumOf3(double d1, double d2, double d3)
{
var nums = new []{d1, d2, d3}.ToList();
nums.Sort();
return nums;
}

Can also be easily extended for more numbers. Also easy to generalise if needed (I only have 3 doubles in my project).

1. Hirendra Sisodiya says:

Hi thanks James.

1. James Tweedie says: