C# | Total number of elements present in an array (original) (raw)
Last Updated : 04 Aug, 2021
Array.GetLength(Int32) Method is used to find the total number of elements present in the specified dimension of the Array.
Syntax:
public int GetLength (int dimension);
Here, dimension is a zero-based dimension of the Array whose length needs to be determined.
Return value: The return type of this method is System.Int32. This method return a 32-bit integer that represents the number of elements in the specified dimension.
**Exception:**This method will give IndexOutOfRangeException if the value of dimension is less than zero or if the value of dimension is equal to or greater than Rank.
Below given are some examples to understand the implementation in a better way:
Example 1:
CSharp `
// C# program to illustrate the // use of GetLength() method using System;
public class GFG {
// Main method
static public void Main()
{
// create and initialize array
int[] myarray = {445, 44, 66, 6666667, 78, 878, 1};
// Display the array
Console.WriteLine("The elements of myarray :");
foreach(int i in myarray)
{
Console.WriteLine(i);
}
// Find the number of element in myarray
int result = myarray.GetLength(0);
Console.WriteLine("Total Elements: {0}", result);
}
}
`
Output:
The elements of myarray : 445 44 66 6666667 78 878 1 Total Elements: 7
Example 2:
CSharp `
// C# program to check arrays contain // same number of elements or not using System;
public class GFG {
// Main method
static public void Main()
{
// create and initializing array
int[] myarray1 = {100, 0, 400, 660, 700, 809, 0};
int[] myarray2 = {100, 0, 400, 660, 700};
int[] myarray3 = {100, 0, 400, 660, 700, 809, 0};
// Find the number of element in myarray
// using GetLength() method
int result1 = myarray1.GetLength(0);
int result2 = myarray2.GetLength(0);
int result3 = myarray3.GetLength(0);
// Check if myarray1, myarray2, myarray3
// contain the same number of elements or not
Console.WriteLine("myarray1 and myarray2: {0}",
Equals(result1, result2));
Console.WriteLine("myarray1 and myarray3: {0}",
Equals(result1, result3));
}
}
`
Output:
myarray1 and myarray2: False myarray1 and myarray3: True
Reference: https://docs.microsoft.com/en-us/dotnet/api/system.array.getlength?view=netcore-2.1
Similar Reads
- C# | Count the total number of elements in the List List class represents the list of objects which can be accessed by index. It comes under the System.Collection.Generic namespace. List class can be used to create a collection of different types like integers, strings etc. List class also provides the methods to search, sort, and manipulate lists. L 2 min read
- C# | Number of elements contained in the BitArray The BitArray class manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on i.e, 1 and false indicates the bit is off i.e, 0. This class is contained in System.Collections namespace. BitArray.Count property is used to get the number of element 2 min read
- C# | Get the number of elements actually contained in the ArrayList ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Count property gets the number of elements actually contained in 3 min read
- C# | Get or set the number of elements that the ArrayList can contain ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Capacity property is used to get or set the number of elements th 2 min read
- How to Count Elements in C# Array? To count the number of elements in the C# array, we can use the count() method from the IEnumerable. It is included in the System.Linq.Enumerable class. The count method can be used with any type of collection such as an array, ArrayList, List, Dictionary, etc. Syntax: Count() This me 3 min read
- C# Arrays An array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array spe 8 min read
- How to Find the Size of an Array in C? The size of an array is generally considered to be the number of elements in the array (not the size of memory occupied in bytes). In this article, we will learn how to find the size of an array in C.The simplest method to find the size of an array in C is by using sizeof operator. First determine t 2 min read
- Length of Array in C The Length of an array in C refers to the maximum number of elements that an array can hold. It must be specified at the time of declaration. It is also known as the size of an array that is used to determine the memory required to store all of its elements.In C language, we don't have any pre-defin 3 min read
- Find the Length of Character Array in C In C, a character array is a collection of elements of the ‘char’ data type that are placed in contiguous memory locations and are often used to store strings. The length of a character array is defined as the total number of characters present in the array, excluding the null character ('\0') at th 2 min read
- How to Find Size of Dynamic Array in C? In C, dynamic memory allocation allows us to manage memory resources during the execution of a program. It’s particularly useful when dealing with arrays where the size isn’t known at compile time. In this article, we will learn how to find the size of a dynamically allocated array in C. Find Size o 2 min read