Algorithm
Code Examples
#1 Code Example- C# Programming Array
Code -
C# Programming
using System;
namespace AccessArray {
class Program {
static void Main(string[] args) {
// create an array
int[] numbers = {1, 2, 3};
//access first element
Console.WriteLine("Element in first index : " + numbers[0]);
//access second element
Console.WriteLine("Element in second index : " + numbers[1]);
//access third element
Console.WriteLine("Element in third index : " + numbers[2]);
Console.ReadLine();
}
}
}
Copy The Code &
Try With Live Editor
Output
Element in second index : 2
Element in third index : 3
#2 Change Array Elements in C# programming
Code -
C# Programming
using System;
namespace ChangeArray {
class Program {
static void Main(string[] args) {
// create an array
int[] numbers = {1, 2, 3};
Console.WriteLine("Old Value at index 0: " + numbers[0]);
// change the value at index 0
numbers[0] = 11;
//print new value
Console.WriteLine("New Value at index 0: " + numbers[0]);
Console.ReadLine();
}
}
}
Copy The Code &
Try With Live Editor
Output
New Value at index 0: 11
#3 Using for loop in C# Programming
Code -
C# Programming
using System;
namespace AccessArrayFor {
class Program {
static void Main(string[] args) {
int[] numbers = { 1, 2, 3};
for(int i=0; i < numbers.Length; i++) {
Console.WriteLine("Element in index " + i + ": " + numbers[i]);
}
Console.ReadLine();
}
}
}
Copy The Code &
Try With Live Editor
Output
Element in index 1: 2
Element in index 2: 3
#4 Using foreach loop in C# Programming
Code -
C# Programming
using System;
namespace AccessArrayForeach {
class Program {
static void Main(string[] args) {
int[] numbers = {1, 2, 3};
Console.WriteLine("Array Elements: ");
foreach(int num in numbers) {
Console.WriteLine(num);
}
Console.ReadLine();
}
}
}
Copy The Code &
Try With Live Editor
Output
1
2
3
#5 Find Minimum and Maximum Element in C# programming
Code -
C# Programming
using System;
// provides us various methods to use in an array
using System.Linq;
namespace ArrayMinMax {
class Program {
static void Main(string[] args) {
int[] numbers = {51, 1, 3, 4, 98};
// get the minimum element
Console.WriteLine("Smallest Element: " + numbers.Min());
// Max() returns the largest number in array
Console.WriteLine("Largest Element: " + numbers.Max());
Console.ReadLine();
}
}
}
Copy The Code &
Try With Live Editor
Output
Largest Element: 98
#6 Find the Average of an Array In C# Programming
Code -
C# Programming
using System;
// provides us various methods to use in an array
using System.Linq;
namespace ArrayFunction {
class Program {
static void Main(string[] args) {
int[] numbers = {30, 31, 94, 86, 55};
// get the sum of all array elements
float sum = numbers.Sum();
// get the total number of elements present in the array
int count = numbers.Count();
float average = sum/count;
Console.WriteLine("Average : " + average);
// compute the average
Console.WriteLine("Average using Average() : " + numbers.Average());
Console.ReadLine();
}
}
}
Copy The Code &
Try With Live Editor
Output
Average using Average() : 59.2
Demonstration
C# Programming Arrays