Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

find the max number in an array c#

// Find the max number of this Array using C# build in function
WriteLine("Find the max number of this Array using C# build in function" + Environment.NewLine+ "{ 1,2,3,4,5,6,7,8,9}");
int[] array = new[] { 1,2,3,4,5,6,7,8,9};
var maxNumber = array.Max();
WriteLine($"Max Number Of this Array:{maxNumber}");
Comment

c# array max

----------------------------------------------------
  HIGHEST
----------------------------------------------------
var highest 0;
var index;
var x = 0;
int[] arry = new int[]{1,2,3,4,5,6,7,8};

foreach (dynamic i in arry)
{
  if (i > highest){ highest = i; index = x; }
  x++;
}
----------------------------------------------------
  OR
----------------------------------------------------
using System.Linq;

int maxValue = anArray.Max();
----------------------------------------------------

  
  
  
----------------------------------------------------
  LOWEST
----------------------------------------------------
var highest = 100000000000;
var index;
var x = 0;
int[] arry = new int[]{1,2,3,4,5,6,7,8};

foreach (dynamic i in arry)
{
  if (i < highest){ highest = i; index = x; }
  x++;
}
----------------------------------------------------
Comment

max index array c#

for ( int i = 0 ; i < ar.Length ; i++ )
			{
				if ( ar [ i ] > ar[imax])
					imax = i;
			}
			return imax;
		
Comment

find the max number in an array c#

// Find the max number of this array using LINQ
 Console.WriteLine("Find the max number of this Array using C# build in function { 1,2,3,4,5,6,7,8,9}");
 int[] array = new[] { 1,2,3,4,5,6,7,8,9};
 var result= array.Select((n, i) => new { Value = n, Index = i }).FirstOrDefault(s => s.Value == array.Max());
 Console.WriteLine($"Max Number Of this Array:{result?.Value} and index no: {result?.Index}");
Comment

PREVIOUS NEXT
Code Example
Csharp :: vb.net center form in screen 
Csharp :: convert list string to list enum c# 
Csharp :: unity dotween sequence 
Csharp :: c# import class from another file 
Csharp :: get mouse inpuit new input system 
Csharp :: onmousedown() not working unity 
Csharp :: c# setting window size 
Csharp :: remove from list based on condition c# 
Csharp :: How to use the protected keyword in C# 
Csharp :: c# validate xml 
Csharp :: foreach c# linq example 
Csharp :: get index brushes properties c# 
Csharp :: c# jagged array initialization 
Csharp :: unity make a gambeobject array 
Csharp :: unique field in class model .net core 
Csharp :: c# custom event handler with parameters 
Csharp :: c# Remove String In C# 
Csharp :: C# ValidationAttribute required when 
Csharp :: c# out parameter 
Csharp :: int to char c# 
Csharp :: c# split multiple options 
Csharp :: curl rest api keycloak 
Csharp :: Reading emails from Gmail in C# 
Csharp :: visual studio console.writeline not showing in output window 
Csharp :: c# array to label 
Csharp :: c# resize multidimensional array 
Csharp :: c# read excel file columns using epplus 
Csharp :: check if multiple variables are null c# 
Csharp :: c# switch when 
Csharp :: hashtable in c# 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =