Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
How to Count Elements in C# Array?
Next article icon

C# | Total number of elements present in an array

Last Updated : 04 Aug, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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
 



Next Article
How to Count Elements in C# Array?

K

Kirti_Mangal
Improve
Article Tags :
  • C#
  • CSharp-Arrays
  • CSharp-method

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<TSource>() 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 Range of Numbers in an Array in C?
    The range of numbers within an array is defined as the difference between the maximum and the minimum element present in the array. In this article, we will learn how we can find the range of numbers in an array in C. Example Input:int arr[] = { 23, 12, 45, 20, 90, 89, 95, 32, 65, 19 }Output: The ra
    2 min read
  • How to Add an Element to an Array of Structs in C?
    In C, a struct is a user-defined data type that allows the users to group related data in a single object. An array of structs allows to store multiple structs in contiguous memory locations. In this article, we will learn how to add an element to an array of structs in C. Example: Input: structArra
    3 min read
  • C Program to Remove All Occurrences of an Element in an Array
    To remove all the occurrences of an element in an array, we will use the following 2 approaches: Using MethodsWithout using methods or With Conditional Statement We will keep the same input in all the mentioned approaches and get an output accordingly. Input: array = {1, 2, 1, 3, 1} value = 1 Output
    3 min read
  • How to Find the Mode of Numbers in an Array in C?
    In C, the mode of array numbers is the element that appears most frequently in the array. To find the mode, we can count the occurrences of each element and identify the one with the highest count. In this article, we will find the mode of numbers in C. Example:Input: myArray = { 1, 2, 3, 4, 5, 2, 3
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences