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:
C# | Copying the Collection<T> elements to an array
Next article icon

How to find the length of an Array in C#

Last Updated : 11 Oct, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report

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);
    }
}
}
 
 
Output:
  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);
    }
}
}
 
 
Output:
  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:

  • https://docs.microsoft.com/en-us/dotnet/api/system.array.length?view=netframework-4.7.2


Next Article
C# | Copying the Collection<T> elements to an array

K

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

Similar Reads

  • How to find the rank of an array in C#
    Array.Rank Property is used to get the rank of the Array. Rank is the number of dimensions of an array. For example, 1-D array returns 1, a 2-D array returns 2, and so on. Syntax: public int Rank { get; } Property Value: It returns the rank (number of dimensions) of the Array of type System.Int32. B
    2 min read
  • C# | LongLength property of an Array
    Array.LongLength Property is used to get a 64-bit integer that represents the total number of elements in all the dimensions of the Array. Syntax: public long LongLength { get; } Property Value: This property returns a 64-bit integer that represents the total number of elements in all the dimensions
    2 min read
  • How to find the length of the StringBuilder in C#
    StringBuilder.Length Property is used to get or set the length of the current StringBuilder object. Syntax: public int Length { get; set; } It returns the length of the current instance. Exception: This property will give ArgumentOutOfRangeException if the value specified for a set operation is less
    2 min read
  • C# | Copying the Collection<T> elements to an array
    Collection<T>.CopyTo(T[], Int32) method is used to copy the entire Collection<T> to a compatible one-dimensional Array, starting at the specified index of the target array. Syntax: public void CopyTo (T[] array, int index); Parameters: array : The one-dimensional Array that is the destin
    3 min read
  • C# | Add an object to the end of 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.Add(Object) method adds an object to the end of the ArrayList. Pr
    2 min read
  • C# | How to insert an element in an Array?
    An array is a collection of items stored at contiguous memory locations. In this article, we will see how to insert an element in an array in C#. Let's say we have an array and we want to insert an element at a specific position in this array. Here's how to do it. First get the element to be inserte
    2 min read
  • How to get Synchronize access to the Array in C#
    Array.SyncRoot Property is used to get an object that can be used to synchronize access to the Array. An array is a group of like-typed variables that are referred to by a common name. Array class comes under the System namespace. Important Points: Synchronization of an object is done so that only o
    3 min read
  • C# | Finding the index of last element in the array
    GetUpperBound() Method is used to find the index of the last element of the specified dimension in the array. Syntax: public int GetUpperBound (int dimension); Here, dimension is a zero-based dimension of the array whose upper bound needs to be determined. Return Value:The return type of this method
    2 min read
  • How to sort an Array in C# | Array.Sort() Method Set – 2
    Array.Sort Method is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method. Here we will discuss the following methods: Sort(Array, Int32, Int32, IComparer) Method Sort(Array, Array, Int32, Int32, IComparer) Method Sort(Array, Int32, Int32) Method
    9 min read
  • How to sort an Array in C# | Array.Sort() Method | Set – 4
    Array.Sort Method is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method. Here we will discuss the following methods: Sort(Array) Method This method sorts the elements in an entire one-dimensional array using the IComparable implementation of ea
    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