-
Key Features and
Create a new project in Visual Studio 2022 - Naming Standards & Comments
- Data Types
- Variables and Constant Variable
- Conditional Statement
- Switch Conditional Statement
- Arithmetic, Comparison, Assignment and Logical Operators
- For, While and Do While Loops
- Importance of Break and Continue Keywords
- Strings and Important String Methods
- Single Dimensional Array
- Multi Dimensional Array
- Methods and Method Overloading
- Math Class and Functions
- Casting and Type Conversions
- Class and Objects
- Constructor, Parameter less and Parameterized Constructor
- Single, Multi level and Hierarchical Inheritance
- Compile Time Polymorphism, Method and Operator Overloading
- Run-time Polymorphism and Importance of base keyword
- Abstract Class and Method
- Interface and Multiple Inheritance through Interfaces
Agenda
- C# Array
- Single Dimensional Array
- Update Array Element Value
- Display Array Elements in Console using for/foreach loop
- Array Sorting
- Array Extension Methods
Array in C#
- Array is a collection of similar type of elements that have a contiguous memory location.
- Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
- An array is a very common type of data structure wherein all elements must be of the same data type. Once defined, the size of an array is fixed and cannot increase to accommodate more elements, index starts from zero to n-1.
- We can store only the fixed size of elements in the array. It doesn't grow its size at runtime.)
- There are two types of array. Single Dimensional Array – Example: int [] array1 = {1, 2, 3, 4}; Multidimensional Array – Example: int [,] array2 = {{1, 3, 5}, {2, 4, 6}}; //Two dimensional
- Using an array in any program is a 3 step process:
1) Declaring the Array – Example: int [] intArray;
2) Constructing the Array – Example: intArray = new int[4];
3) Initialize the Array – Example: intArray[0] = 1; intArray[1] = 4; intArray[2] = 3; intArray[3] = 2;
- Accessing any element in the array: Console.WriteLine(intArray[0]);
- User can update any element by updating the value: intArray[0] = 5;
- Length of the array can be found using: Console.WriteLine(intArray.Length);
Array in C# - Contd.
- C# Arrays class provides method Arrays.Sort(intArray) which helps user to sort an Array of objects.
- For loop can be used to access the array. for (int i = 0; i < intArray.Length; i++) Console.WriteLine(intArray[i]);
- Foreach loop can be used exclusively to loop through elements in an array. foreach (int i in intArray) { Console.WriteLine(i); }
- The Array class implements the IEnumerable interface, so user can use extension methods such as Max(), Min(), Sum(), Average(), Reverse() etc. intArray.Max(); intArray.Min(); intArray.Sum(); intArray.Average(); Array.Reverse(intArray);
- Example of string Array: string[] cars = {"Maruti", "BMW", "Honda", "Hyundai"};
[] intArray;
intArray = new int[4];
intArray[0] = 1; intArray[1] = 4; intArray[2] = 3; intArray[3] = 2;
intArray[0] = 5;
Console.WriteLine(intArray[3]);
Console.WriteLine(intArray.Length);
for (int i = 0; i < intArray.Length; i++)
Console.WriteLine(intArray[i]);
//Array.Sort(intArray);
foreach (int i in intArray)
{
Console.WriteLine(i);
}
Console.WriteLine(intArray.Max());
Console.WriteLine(intArray.Min());
Console.WriteLine(intArray.Sum());
Console.WriteLine(intArray.Average());Array.Reverse(intArray);
string[] cars = { "Maruti", "BMW", "Honda", "Hyundai" }; Array.Sort(cars);
foreach (string s in cars)
{
Console.WriteLine(s);
}