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 Sort an Array in C# | Array.Sort() Method Set - 1
Next article icon

C# Array Class

Last Updated : 31 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Array class in C# is part of the System namespace and provides methods for creating, searching, and sorting arrays. The Array class is not part of the System.Collections namespace, but it is still considered as a collection because it is based on the IList interface. The Array class is the base class for language implementations that support arrays.

  • The length of the array is the number of elements it holds.
  • The lower bound of an Array is the index of its first element and the default value of the lower bound is 0.
  • The default size of an Array is 2GB.
  • Arrays of the same type share the same type or Class object.

Example: This example demonstrates array traversal.

C#
// C# program to demonstrates the traversal on array using System;  class Geeks {     static void Main()     {         // Declare and initialize an array of integers         int[] arr = { 10, 20, 30, 40, 50 };          // Print each element of the array         Console.WriteLine("Array elements are:");         foreach(int i in arr) {            Console.WriteLine(i);          }     } } 

Output
Array elements are: 10 20 30 40 50 

Properties

The Array class provides several properties to access its state.

PropertiesDescription
IsFixedSizeGets a value indicating whether the Array has a fixed size.
IsReadOnlyGets a value indicating whether the Array is read-only.
IsSynchronizedGets a value indicating whether access to the Array is synchronized (thread safe).
LengthGets the total number of elements in all the dimensions of the Array.
LongLengthGets a 64-bit integer that represents the total number of elements in all the dimensions of the Array.
RankGets the rank (number of dimensions) of the Array. For example, a one-dimensional array returns 1, a two-dimensional array returns 2, and so on.
SyncRootGets an object that can be used to synchronize access to the Array.

Example 1: This example demonstrates how to use the length property of an array.

C#
// C# program to demonstrates the length of an array using System;  class Geeks {         public static void Main()     {         // Declares a 1D array of integers         int[] arr;          // Allocating memory for the array         arr = new int[] { 10, 20, 30, 40, 50, 60 };          // Displaying the length of the array         Console.WriteLine("Length of the array: {0}",                           arr.Length);     } } 

Output
Length of the array: 6 

Example 2: This example, demonstrates how to use the Rank property to find the number of dimensions of an array and prints each element of the array.

C#
// C# program to demonstrates the property  rank of an array using System;  class Geeks {      public static void Main()     {         // Declare and initialize a 1D array of integers         int[] arr = { 10, 20, 30, 40, 50, 60 };          // Display the rank (dimension) of the array         Console.WriteLine("Rank of the array: " + arr.Rank);          foreach(int i in arr) {            Console.WriteLine(i);          }     } } 

Output
Rank of the array: 1 10 20 30 40 50 60 

Methods

MethodDescription
AsReadOnly()Returns a read-only wrapper for the specified array.
BinarySearch()Searches a one-dimensional sorted Array for a value, using a binary search algorithm.
Clear()Sets a range of elements in an array to the default value of each element type.
Clone()Creates a shallow copy of the Array.
ConstrainedCopy()Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. Guarantees that all changes are undone if the copy does not succeed completely.
ConvertAll()Converts an array of one type to an array of another type.
Copy()Copies a range of elements in one Array to another Array and performs type casting and boxing as required.
CopyTo()Copies all the elements of the current one-dimensional array to the specified one-dimensional array.
CreateInstance()Initializes a new instance of the Array class.
Empty()Returns an empty array.
Equals()Determines whether the specified object is equal to the current object.
Exists()Determines whether the specified array contains elements that match the conditions defined by the specified predicate.
Find()Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire Array.
FindAll()Retrieves all the elements that match the conditions defined by the specified predicate.
FindIndex()Searches for an element that matches the conditions defined by a specified predicate, and returns the zero-based index of the first occurrence within an Array or a portion of it.
FindLast()Searches for an element that matches the conditions defined by the specified predicate, and returns the last occurrence within the entire Array.
FindLastIndex()Searches for an element that matches the conditions defined by a specified predicate, and returns the zero-based index of the last occurrence within an Array or a portion of it.
ForEach()Performs the specified action on each element of the specified array.
GetEnumerator()Returns an IEnumerator for the Array.
GetHashCode()Serves as the default hash function.
GetLength()Gets a 32-bit integer that represents the number of elements in the specified dimension of the Array.
GetLongLength()Gets a 64-bit integer that represents the number of elements in the specified dimension of the Array.
GetLowerBound()Gets the index of the first element of the specified dimension in the array.
GetType()Gets the Type of the current instance.
GetUpperBound()Gets the index of the last element of the specified dimension in the array.
GetValue()Gets the value of the specified element in the current Array.
IndexOf()Searches for the specified object and returns the index of its first occurrence in a one-dimensional array or in a range of elements in the array.
Initialize()Initializes every element of the value-type Array by calling the default constructor of the value type.
LastIndexOf()Returns the index of the last occurrence of a value in a one-dimensional Array or in a portion of the Array.
MemberwiseClone()Creates a shallow copy of the current Object.
Resize()Changes the number of elements of a one-dimensional array to the specified new size.
Reverse()Reverses the order of the elements in a one-dimensional Array or in a portion of the Array.
SetValue()Sets the specified element in the current Array to the specified value.
Sort()Sorts the elements in a one-dimensional array.
ToString()Returns a string that represents the current object. 
(Inherited from Object)
TrueForAll()Determines whether every element in the array matches the conditions defined by the specified predicate.

Example 1: This example demonstrates how to use the Array.Reverse() to reverse the elements of a one-dimensional integer array.

C#
// C# program to demonstrates the working of Reverse() using System;  class Geeks {     public static void Main()     {         // Declare and initialize a 1D array of integers         int[] arr = { 10, 20, 30, 40, 50, 60 };          // Display the array before reversing         Console.WriteLine("Array before reverse:");         foreach(int i in arr) {            Console.Write(i + " ");          }         Console.WriteLine();          // Using Reverse() method to reverse the array         Array.Reverse(arr);          // Display the array after reversing         Console.WriteLine("Array after reverse:");         foreach(int i in arr) {            Console.Write(i + " ");          }         Console.WriteLine();     } } 

Output
Array before reverse: 10 20 30 40 50 60  Array after reverse: 60 50 40 30 20 10  

Example 2: This example, demonstrates hoe to use Array.sort() Method to sort a one-dimensional integer array in ascending order.

C#
// C# program to demonstrates the working of Sort() using System;  class Geeks {     public static void Main()     {         // Declare and initialize a 1D array of integers         int[] arr = { 50, 20, 40, 10, 60, 30 };          // Display the array before sorting         Console.WriteLine("Array before sorting:");         foreach(int i in arr) {            Console.Write(i + " ");          }         Console.WriteLine();          // Using Sort() method to sort the array         Array.Sort(arr);          // Display the array after sorting         Console.WriteLine("Array after sorting:");         foreach(int i in arr) {            Console.Write(i + " ");          }         Console.WriteLine();     } } 

Output
Array before sorting: 50 20 40 10 60 30  Array after sorting: 10 20 30 40 50 60  


Next Article
How to Sort an Array in C# | Array.Sort() Method Set - 1
author
ankita_saini
Improve
Article Tags :
  • C#
  • CSharp-Arrays

Similar Reads

    Introduction

    • C# Tutorial
      C# (pronounced "C-sharp") is a modern, versatile, object-oriented programming language developed by Microsoft in 2000 that runs on the .NET Framework. Whether you're creating Windows applications, diving into Unity game development, or working on enterprise solutions, C# is one of the top choices fo
      4 min read

    • Introduction to .NET Framework
      The .NET Framework is a software development framework developed by Microsoft that provides a runtime environment and a set of libraries and tools for building and running applications on Windows operating systems. The .NET framework is primarily used on Windows, while .NET Core (which evolved into
      6 min read

    • C# .NET Framework (Basic Architecture and Component Stack)
      C# (C-Sharp) is a modern, object-oriented programming language developed by Microsoft in 2000. It is a part of the .NET ecosystem and is widely used for building desktop, web, mobile, cloud, and enterprise applications. This is originally tied to the .NET Framework, C# has evolved to be the primary
      6 min read

    • C# Hello World
      The Hello World Program is the most basic program when we dive into a new programming language. This simply prints "Hello World!" on the console. In C#, a basic program consists of the following: A Namespace DeclarationClass Declaration & DefinitionClass Members(like variables, methods, etc.)Mai
      4 min read

    • Common Language Runtime (CLR) in C#
      The Common Language Runtime (CLR) is a component of the Microsoft .NET Framework that manages the execution of .NET applications. It is responsible for loading and executing the code written in various .NET programming languages, including C#, VB.NET, F#, and others. When a C# program is compiled, t
      4 min read

    Fundamentals

    • C# Identifiers
      In programming languages, identifiers are used for identification purposes. Or in other words, identifiers are the user-defined name of the program components. In C#, an identifier can be a class name, method name, variable name, or label. Example: public class GFG { static public void Main () { int
      2 min read

    • C# Data Types
      Data types specify the type of data that a valid C# variable can hold. C# is a strongly typed programming language because in C# each type of data (such as integer, character, float, and so forth) is predefined as part of the programming language and all constants or variables defined for a given pr
      7 min read

    • C# Variables
      In C#, variables are containers used to store data values during program execution. So basically, a Variable is a placeholder of the information which can be changed at runtime. And variables allows to Retrieve and Manipulate the stored information. In Brief Defination: When a user enters a new valu
      4 min read

    • C# Literals
      In C#, a literal is a fixed value used in a program. These values are directly written into the code and can be used by variables. A literal can be an integer, floating-point number, string, character, boolean, or even null. Example: // Here 100 is a constant/literal.int x = 100; Types of Literals i
      5 min read

    • C# Operators
      In C#, Operators are special types of symbols which perform operations on variables or values. It is a fundamental part of language which plays an important role in performing different mathematical operations. It takes one or more operands and performs operations to produce a result. Types of Opera
      8 min read

    • C# Keywords
      Keywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to be used as variable names or objects. Doing this will result in a compile-time error. Example: [GFGTABS] C# // C# Program to i
      6 min read

    Control Statements

    • C# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch)
      Decision Making in programming is similar to decision making in real life. In programming too, a certain block of code needs to be executed when some condition is fulfilled. A programming language uses control statements to control the flow of execution of program based on certain conditions. These
      5 min read

    • C# Switch Statement
      In C#, Switch statement is a multiway branch statement. It provides an efficient way to transfer the execution to different parts of a code based on the value of the expression. The switch expression is of integer type such as int, char, byte, or short, or of an enumeration type, or of string type.
      4 min read

    • C# Loops
      Looping in a programming language is a way to execute a statement or a set of statements multiple times depending on the result of the condition to be evaluated to execute statements. The result condition should be true to execute statements within loops. Loops are mainly divided into two categories
      5 min read

    • C# Jump Statements (Break, Continue, Goto, Return and Throw)
      In C#, Jump statements are used to transfer control from one point to another point in the program due to some specified code while executing the program. In, this article, we will learn to different jump statements available to work in C#. Types of Jump StatementsThere are mainly five keywords in t
      4 min read

    OOP Concepts

    • C# Class and Objects
      Class and Object are the basic concepts of Object-Oriented Programming which revolve around real-life entities. A class is a user-defined blueprint or prototype from which objects are created. Basically, a class combines the fields and methods(member functions which define actions) into a single uni
      5 min read

    • C# Constructors
      Constructor is a special method of the class which gets automatically invoked whenever an instance of the class is created. Constructors in C# are fundamental components of object-oriented programming. Like methods, It contains the collection of instructions that are executed at the time of Object c
      5 min read

    • C# Inheritance
      Inheritance is a fundamental concept in object-oriented programming that allows a child class to inherit the properties from the superclass. The new class inherits the properties and methods of the existing class and can also add new properties and methods of its own. Inheritance promotes code reuse
      6 min read

    • C# Encapsulation
      Encapsulation is defined as the wrapping up of data and information under a single unit. It is the mechanism that binds together the data and the functions that manipulate them. In contrast, encapsulation is a protective shield that prevents the data from being accessed by the code outside this shie
      4 min read

    • C# Abstraction
      Data Abstraction is the property by which only the essential details are shown to the user and non-essential details or implementations are hidden from the user. In other words, Data Abstraction may also be defined as the process of identifying only the required characteristics of an object ignoring
      4 min read

    Methods

    • C# Methods
      A method is a block of code that performs a specific task. It can be executed when called, and it may take inputs, process them, and return a result. Methods can be defined within classes and are used to break down complex programs into simpler, modular pieces. Methods improve code organization, rea
      4 min read

    • C# Method Overloading
      Method overloading is an important feature of Object-Oriented programming and refers to the ability to redefine a method in more than one form. A user can implement method overloading by defining two or more functions in a class sharing the same name. C# can distinguish the methods with different me
      5 min read

    • C# | Method Parameters
      Methods in C# are generally the block of codes or statements in a program which gives the user the ability to reuse the same code which ultimately saves the excessive use of memory, acts as a time saver and more importantly, it provides better readability of the code. So you can say a method is a co
      7 min read

    • C# Method Overriding
      In C#, method overriding occurs when a subclass provides a specific implementation for a method that is already defined in the superclass or base class. The method in the subclass must have the same signature as the method in the base class. By overriding a method, the subclass can modify the behavi
      9 min read

    • Anonymous Method in C#
      An anonymous method is a method which doesn’t contain any name which is introduced in C# 2.0. It is useful when the user wants to create an inline method and also wants to pass parameter in the anonymous method like other methods. An Anonymous method is defined using the delegate keyword and the use
      3 min read

    Arrays

    • 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

    • C# Jagged Arrays
      A jagged array is an array of arrays, where each element in the main array can have a different length. In simpler terms, a jagged array is an array whose elements are themselves arrays. These inner arrays can have different lengths. Can also be mixed with multidimensional arrays. The number of rows
      5 min read

    • C# Array Class
      Array class in C# is part of the System namespace and provides methods for creating, searching, and sorting arrays. The Array class is not part of the System.Collections namespace, but it is still considered as a collection because it is based on the IList interface. The Array class is the base clas
      7 min read

    • How to Sort an Array in C# | Array.Sort() Method Set - 1
      Array.Sort Method in C# is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method as follows: Sort<T>(T[]) MethodSort<T>(T[], IComparer<T>) MethodSort<T>(T[], Int32, Int32) MethodSort<T>(T[], Comparison<T>) Metho
      8 min read

    • 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

    ArrayList

    • ArrayList in C#
      ArrayList is a powerful feature of C# language. It is the non-generic type of collection which is defined in System.Collections namespace. It is used to create a dynamic array means the size of the array is increase or decrease automatically according to the requirement of your program, there is no
      6 min read

    • C# ArrayList Class
      ArrayList class in C# is a part of the System.Collections namespace that 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. Elements ca
      8 min read

    • C# | Array vs ArrayList
      Arrays: An array is a group of like-typed variables that are referred to by a common name. Example: C/C++ Code // C# program to demonstrate the Arrays using System; class GFG { // Main Method public static void Main(string[] args) { // creating array int[] arr = new int[4]; // initializing array arr
      2 min read

    String

    • C# Strings
      In C#, a string is a sequence of Unicode characters or an array of characters. The range of Unicode characters will be U+0000 to U+FFFF. The array of characters is also termed as the text. So the string is the representation of the text. A string is an important concept, and sometimes people get con
      8 min read

    • C# Verbatim String Literal - @
      In C#, a verbatim string is created using a special symbol @. The symbol(@) is known as a verbatim identifier. If a string contains @ as a prefix followed by double quotes, then compiler identifies that string as a verbatim string and compile that string. The main advantage of @ symbol is to tell th
      5 min read

    • C# String Class
      In C#, a string is a sequence of Unicode characters or an array of characters. The range of Unicode characters will be U+0000 to U+FFFF. The array of characters is also termed as the text. So the string is the representation of the text. A string is represented by a class System.String. The String c
      9 min read

    • C# StringBuilder
      StringBuilder is a Dynamic Object. It doesn’t create a new object in the memory but dynamically expands the needed memory to accommodate the modified or new string.A String object is immutable, i.e. a String cannot be changed once created. To avoid string replacing, appending, removing or inserting
      4 min read

    • C# String vs StringBuilder
      StringBuilder is used to represent a mutable string of characters. Mutable means the string which can be changed. So String objects are immutable but StringBuilder is the mutable string type. It will not create a new modified instance of the current string object but do the modifications in the exis
      4 min read

    Tuple

    • C# Tuple
      A tuple is a data structure which consists of multiple parts. It is the easiest way to represent a data set which has multiple values of different types. It was introduced in .NET Framework 4.0. In a tuple, we can add elements from 1 to 8. If try to add elements greater than eight, then the compiler
      7 min read

    • C# Tuple Class
      In C#, the Tuple class is used to provide static methods for creating tuples and this class is defined under the System namespace. This class itself does not represent a tuple, but it provides static methods that are used to create an instance of the tuple type. In other words, the Tuple class provi
      3 min read

    • C# ValueTuple
      ValueTuple is a structure introduced in C# 7.0 which represents the value type. Already included in .NET Framework 4.7 or higher version. It allows us to store a data set that contains multiple values that may or may not be related to each other. It can store elements starting from 0 to 8 and can st
      7 min read

    • C# ValueTuple Struct
      ValueTuple Struct in C# is a structure that provides static methods that are used in creating value tuples. It is defined under the System namespace and was introduced in .NET Framework 4.7. This struct enables runtime implementation tuples in C#. The ValueTuple structure represents a tuple that can
      4 min read

    Indexers

    • C# Indexers
      In C#, an indexer allows an instance of a class or struct to be indexed as an array. When an indexer is defined for a class, then that class will behave like a virtual array. Array access operator i.e. ([ ]) is used to access the instance of the class which uses an indexer. A user can retrieve or se
      4 min read

    • C# Multidimensional Indexers
      In C#, indexers are special members that allow objects to be indexed similarly to arrays. Multi-dimensional indexers in C# are the same as multidimensional arrays. We can efficiently retrieve data with a multi-dimensional indexer by providing at least two parameters in its declaration. The first ind
      5 min read

    • C# - Overloading of Indexers
      In C#, Indexer allows an instance of a class or struct to be indexed as an array. When an indexer is defined for a class, then that class will behave like a virtual array. It can be overloaded. C# has multiple indexers in a single class. To overload an indexer, declare it with multiple parameters an
      3 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