
- 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 use a ‘for loop’ for accessing array elements in C#?
The ‘for loop’ executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
The following is our for loop.
Example
using System; namespace ArrayApplication { class MyArray { static void Main(string[] args) { int [] n = new int[10]; /* n is an array of 10 integers */ int i,j; /* initialize elements of array n */ for ( i = 0; i < 10; i++ ) { n[ i ] = i + 100; } /* output each array element's value */ for (j = 0; j < 10; j++ ) { Console.WriteLine("Element[{0}] = {1}", j, n[j]); } int a = n[2]; Console.WriteLine(a); Console.ReadKey(); } } }
Output
Element[0] = 100 Element[1] = 101 Element[2] = 102 Element[3] = 103 Element[4] = 104 Element[5] = 105 Element[6] = 106 Element[7] = 107 Element[8] = 108 Element[9] = 109 102
Now let’s say you need the third element. For that, use.
int a = n[1];
The above will give the third element i.e. 102.
- Related Articles
- How do you use ‘foreach’ statement for accessing array elements in C#
- How do you use ‘foreach’ loop for iterating over an array in C#?
- How do you loop through a C# array?
- How do you throw an Exception without breaking a for loop in java?
- How to use for each loop through an array in Java?
- How to use ‘for loop’ in Java?
- How to use for loop in Python?
- How to use for loop to print all the elements of a list in R?
- How to use for...in statement to loop through an Array in JavaScript?
- How to use nested for loop in JavaScript?
- How do we use foreach statement to loop through the elements of an array in C#?
- Accessing Array Elements in Perl
- How to use FOR LOOP in MySQL Stored Procedure?
- How do you Loop Through a Dictionary in Python?
- How to write and use for loop in Vue js?

Advertisements