How to find the length of an Array in C#
Array.Length Property is used to get the total number of elements in all the dimensions of the Array. Basically, the length of an array is the total number of the elements which is contained by all the dimensions of that array.
Syntax:
public int Length { get; }
Property Value: This property returns the total number of elements in all the dimensions of the Array. It can also return zero if there are no elements in the array. The return type is System.Int32.
Exception: This property throws the OverflowException if the array is multidimensional and contains more than MaxValue elements. Here MaxValue is 2147483647.
Below programs illustrate the use of above-discussed property:
Example 1:
// C# program to find the // the total number of // elements in 1-D Array using System; namespace geeksforgeeks { class GFG { // Main Method public static void Main() { // declares a 1D Array of string. string[] weekDays; // allocating memory for days. weekDays = new string[] {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; // Displaying Elements of the array foreach(string day in weekDays) Console.Write(day + " "); Console.Write("\nTotal Number of Elements: "); // using Length property Console.Write(weekDays.Length); } } } |
Sun Mon Tue Wed Thu Fri Sat Total Number of Elements: 7
Example 2:
// C# program to find the total // number of elements in the // multidimensional Arrays using System; namespace geeksforgeeks { class GFG { // Main Method public static void Main() { // Two-dimensional array int[, ] intarray = new int[, ] {{1, 2}, {3, 4}, {5, 6}, {7, 8}}; // The same array with dimensions // specified 4 row and 2 column. int[, ] intarray_d = new int[4, 2] {{ 1, 2}, {3, 4}, {5, 6}, {7, 8}}; // Three-dimensional array. int[,, ] intarray3D = new int[,, ] {{{ 1, 2, 3}, { 4, 5, 6}}, {{ 7, 8, 9}, {10, 11, 12}}}; // The same array with dimensions // specified 2, 2 and 3. int[,, ] intarray3Dd = new int[2, 2, 3] {{{1, 2, 3}, {4, 5, 6}}, {{ 7, 8, 9}, {10, 11, 12}}}; Console.Write("Total Number of Elements in intarray: "); // using Length property Console.Write(intarray.Length); Console.Write("\nTotal Number of Elements in intarray_d: "); // using Length property Console.Write(intarray_d.Length); Console.Write("\nTotal Number of Elements in intarray3D: "); // using Length property Console.Write(intarray3D.Length); Console.Write("\nTotal Number of Elements in intarray3Dd: "); // using Length property Console.Write(intarray3Dd.Length); } } } |
Total Number of Elements in intarray: 8 Total Number of Elements in intarray_d: 8 Total Number of Elements in intarray3D: 12 Total Number of Elements in intarray3Dd: 12
Reference:
Recommended Posts:
- How to find the length of the StringBuilder in C#
- C# | Array.Find() Method
- How to find the rank of an array in C#
- How to set the Length of the Characters in the ComboBox in C#?
- How to set the Length of the Characters in TextBox in C#?
- Array.BinarySearch(Array, Object) Method with examples in C#
- C# | Array.BinarySearch(Array, Object, IComparer) Method
- Array.BinarySearch(Array, Int32, Int32, Object) Method with examples in C#
- C# | Array.BinarySearch(Array, Int32, Int32, Object, IComparer) Method
- C# | Check if an array object is equal to another array object
- C# | Buffer.BlockCopy(Array, Int32, Array, Int32, Int32) Method
- How to sort an Array in C# | Array.Sort() Method Set – 3
- How to sort an Array in C# | Array.Sort() Method Set – 2
- C# | Converting an array of one type to an array of another type
- How to sort an Array in C# | Array.Sort() Method Set - 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai



