Progamming PPT
Progamming PPT
C# Programming
Arrays in C#
• Arrays are a fundamental data structure in programming.
• They allow us to store multiple values of the same type in a single variable.
• An array can be single-dimensional, multidimensional , or jagged.
• The number of dimensions and the length of each dimension are established
when the array instance is created. These values can't be changed during the
lifetime of the instance.
• The default values of numeric array elements are set to zero, and reference
elements are set to null.
• Arrays are zero-indexed: an array with n elements is indexed from 0 to n-1.
Declaring Arrays
• Arrays are declared using the following syntax:
type[ ] array-name;
• Where type is the data type of the array elements.
• The following examples show how to declare different kinds of arrays:
• Single-dimensional arrays:
int[] numbers;
• Multidimensional arrays:
int[,] matrix;
• Array-of-arrays (jagged):
byte[][] scores;
Declaring Arrays cont…
• In C#, arrays are objects. That means that declaring an array
doesn't create an array. After declaring an array, you need to
instantiate an array by using the "new" operator.
int[] numbers = { 1, 2, 3, 4, 5 };
Console.WriteLine("Integer Array:");
Console.WriteLine("Length: " +
numbers.Length);
string[] names = { "John", "Jane",
"Alice" };
Console.WriteLine("String Array:");
Console.WriteLine("Length: " +
names.Length);
Array Methods
• C# provides methods like Sort()
and Reverse() for sorting and
reversing arrays.
• Sort() arranges the elements in
ascending order.
• Reverse() reverses the order of
elements.
Example
int[] numbers = { 5, 2, 8, 1, 4 };
// Sorting the array in ascending order
Array.Sort(numbers);
Console.WriteLine("Sorted Array (Ascending Order):");
foreach (int num in numbers)
{
Console.Write(num + " ");
}
Console.WriteLine();
// Reversing the array
Array.Reverse(numbers);
Console.WriteLine("Reversed Array:");
foreach (int num in numbers)
{
Console.Write(num + " ");
}
Console.WriteLine();
Summary
• Today, we covered important concepts related to the C# programming
language.
• We learned about declaring and assigning values to arrays, accessing
elements using subscripts, and using the Length property.
• We also explored the Sort() and Reverse() methods for array
manipulation.
• Additionally, we discussed other useful array methods.
Thank You