
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How do you loop through a C# array?
To loop through an array in C#, use any of the loops. These loops have starting and ending value set that allows you to set or check value through iterations.
C# has while, do…while, for and foreach loops to loop through an array.
Let us see an example of for loop in C# −
Example
using System; namespace ArrayApplication { class MyArray { static void Main(string[] args) { int [] n = new int[10]; int i,j; for ( i = 0; i < 10; i++ ) { n[ i ] = i + 10; } for (j = 0; j < 10; j++ ) { Console.WriteLine("Element[{0}] = {1}", j, n[j]); } Console.ReadKey(); } } }
Now let us see how the above works to loop through the array.
An array of 10 integers −
int [] n = new int[10];
Now, initialize elements of the above declared array −
for ( i = 0; i < 10; i++ ) { n[ i ] = i + 10; }
Above the loop iterates from i=0 to i = 10 and after every iteration the value of i increments −
i++;
On every iteration until i = 10, the value is added to the array starting with element 1 as 10 −
n[ i ] = i + 10;
Output
Element[0] = 10 Element[1] = 11 Element[2] = 12 Element[3] = 13 Element[4] = 14 Element[5] = 15 Element[6] = 16 Element[7] = 17 Element[8] = 18 Element[9] = 19
- Related Articles
- How do you Loop Through a Dictionary in Python?
- How do we loop through array of arrays containing objects in JavaScript?
- How do you use a ‘for loop’ for accessing array elements in C#?
- How to loop through an array in Java?
- How do you use ‘foreach’ loop for iterating over an array in C#?
- Loop through an array in Java
- How do we use foreach statement to loop through the elements of an array in C#?
- Loop through array and edit string JavaScript
- How to use for each loop through an array in Java?
- How do you throw an Exception without breaking a for loop in java?
- How do I loop through a JSON file with multiple keys/sub-keys in Python?
- How to loop through all the elements of an array in C#?
- How do you create a Tkinter GUI stop button to break an infinite loop?
- How to use for...in statement to loop through an Array in JavaScript?
- Loop through a Set using Javascript

Advertisements