- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to define a single-dimensional array in C Sharp?
An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type stored at contiguous memory locations.
To define a single-dimensional array −
int[] runs = new int[10];
Let us now initialize the array in the same line −
int[] runs = new int[5] {125, 173, 190, 264, 188};
The following is an example displaying how to declare, initialize and display an array −
Example
using System; namespace Program { class Demo { static void Main(string[] args) { int[] runs = new int[5] {125, 173, 190, 264, 188}; int i,j; for (j = 0; j < 5; j++ ) { Console.WriteLine("Innings score of Cricketer[{0}] = {1}", j, runs[j]); } Console.ReadKey(); } } }
Output
Innings score of Cricketer[0] = 125 Innings score of Cricketer[1] = 173 Innings score of Cricketer[2] = 190 Innings score of Cricketer[3] = 264 Innings score of Cricketer[4] = 188
Advertisements