An array is a variable that stores a collection of values of the same type. Array variables are sorted and each variable has an index starting at 0. Memory allocation for arrays in C# is done dynamically. Since arrays are objects, their size can be easily determined using built-in functions. The maximum number of elements that an array may hold is referred to as its length or size. C# array variables can be declared exactly like any other variable by adding [] after the data type.
C# Array Types:
In C# programming, there are three types of arrays:
- Single Dimensional Array
- Multidimensional Array
- Jagged Array
Single Dimensional Array
A single-dimensional array is the simplest type of array in C# that contains only one row to store the values of the same data type. It is also known as a one-dimensional array.
Multidimensional Array
A multidimensional array in C# is an array containing many rows to store data. It is also known as a two-dimensional array.
Jagged Array
A Jagged Array is an array whose elements are arrays. The elements of the jagged array vary in size and dimension.
Advantages of C# Arrays:
- It’s used to give a single name to a collection of data items that are all of the same kind.
- Since C# arrays are strongly typed, the efficiency of the program is improved because there is no boxing and unboxing.
- Easy to traverse, manipulate or sort the data.
- Other data structures, such as linked lists, graphs, trees, stacks, and queues, can be implemented using arrays.
Disadvantages of C# Arrays:
- Fixed array size.
- An element can never be inserted or deleted in the middle of an array.
The following section contains C# programs on arrays, array types, array operations, and array indexing. Every example program includes the problem description, problem solution, C# code, program explanation, and run-time test cases. All C# examples have been compiled and tested on Visual Studio.
Here is the listing of C# programming examples on Arrays:
- C# Programs on Array Types
- C# Programs on Array Operations
- C# Programs on Array Indexing
- C# Programs on Negation and Average Operations
1. C# Programs on Array Types
Program | Description |
---|---|
Convert 2D Array to 1D Array in C# | C# Program to Convert a 2D Array into 1D Array |
Jagged Arrays in C# | C# Program to Demonstrate Jagged Arrays |
Lower and Upper Bound of an Array in C# | C# Program to Print the Lower Bound and Upper Bound of an Array |
Find Length of an Array in C# | C# Program to Find the Length of an Array |
Rank of all Elements in an Array in C# | C# Program to Find the Rank of an Array |
2. C# Programs on Array Operations
Program | Description |
---|---|
Reverse an Array in C# | C# Program to Reverse an Array |
Search an Element in an Array in C# | C# Program to Search an Element in an Array |
Array Copy in C# | C# Program to Copy a Section of One Array to Another |
Sort Names in Alphabetical Order in C# | C# Program to Sort a List of Names in Alphabetical Order |
3. C# Programs on Array Indexing
Program | Description |
---|---|
Indexers in C# | C# Program to Implement use of Indexers |
Search an Element with Array Indices in C# | C# Program to Search an Element with Array Indices |
Merge Two Arrays into Third Array in C# | C# Program to Merge Two Arrays into Third Array |
Find Min and Max in Array in C# | C# Program to Find Minimum and Maximum of Numbers in an Array |
4. C# Programs on Negation and Average Operations
Program | Description |
---|---|
Negate the Positive Elements of Array in C# | C# Program to Convert a Positive Number into Negative |
Average of n Numbers using Array in C# | C# Program to Find the Average of an Array |
Average of Array Elements in C# | C# Program to Find the Average of All the Array Elements |
Length of Jagged Array using Predefined Functions in C# | C# Program to Find the Length of Jagged Array using Predefined Functions |