220 likes | 421 Views
This slide on C# Arrays tutorial will acquaint you with a clear understanding of the fundamentals of C# Arrays. In this C# Tutorial for beginners, you will get better understanding on how to program arrays. we will start with an introduction to C# Arrays, then we will look into types of C# Arrays like single dimensional, multi dimensional and jagged C# arrays. Finally, we will cover the advantages and disadvantages of C# arrays.<br>
E N D
Agenda • What is C# Arrays? • Types of C# Arrays • Advantages of C# Arrays • Disadvantages of C# Arrays • Conclusion
What is C# Arrays? In C#, an array is a collection of elements of the same type that are stored in the same memory location
What is C# Arrays? In C#, array is an object of base type "System.Array". The array index in C# starts at 0. In a C# array, we can only store a fixed number of elements.
Single Dimensional C# Array To create single dimensional array, you need to use square brackets [] after the type. int[] arr = newint[5];//creating array arr[0] = 10;//initializing array
Multi-Dimensional Array To create multidimensional array, we need to use comma inside the square brackets. int[,] arr=newint[3,3]; //declaration of 2D array int[,,] arr=newint[3,3,3]; //declaration of 3D array
Jagged Array Jagged array is also known as a "array of arrays" in C# since each of its elements is an array. A jagged array's element size might vary. int[][] arr = newint[2][];
Advantages It is used to represent numerous data objects of similar kind with a single name. Data structures, like linked lists, trees, stacks, and queues, are implemented using arrays. The two-dimensional arrays in C# are used to represent matrices. Since arrays are strongly typed, the program's performance is improved because of no boxing and unboxing.
Disadvantages The array size is fixed. So, we must declare the size of array beforehand. If we allocate more memory than the requirement then the extra memory will be wasted. An element can never be inserted in the middle of an array. It is also not possible to delete or remove elements from the middle of an array.
Conclusion Code Optimization is very easy in C# Arrays. It is easy to traverse data using C# Arrays. It is easy to manipulate data using C# Arrays. It is easy to sort data using C# Arrays. Since arrays have a fixed size, declaring it may result in memory waste or shortage.