
- 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 to declare an array in C#?
An array is used to store a collection of data. It stores a fixed-size sequential collection of elements of the same type.
To declare an array, follow the below given syntax −
datatype[] arrayName;
Here,
- datatype is used to specify the type of elements in the array.
- [ ] sets the rank of the array. The rank specifies the size of the array.
- arrayName specifies the name of the array.
Let us now see an example −
int[] goals;
The following is an example showing how to declare and initialize arrays in C#.
Example
using System; namespace Demo { class MyArray { static void Main(string[] args) { int [] goals = new int[5] {3,2,1,5,4}; int i,j; for (j = 0; j < 5; j++ ) { Console.WriteLine("Goals in FIFA - Match[{0}] = {1}", j, goals[j]); } Console.ReadKey(); } } }
Output
Goals in FIFA - Match[0] = 3 Goals in FIFA - Match[1] = 2 Goals in FIFA - Match[2] = 1 Goals in FIFA - Match[3] = 5 Goals in FIFA - Match[4] = 4
- Related Articles
- How to declare an Array Variables in Java?
- How to declare an empty string array in C#?
- How to declare, create, initialize and access an array in Java?
- how can I declare an Object Array in Java?
- How do I declare and initialize an array in Java?
- How to declare an event in C#?
- How to declare Java array with array size dynamically?
- How to declare a static String array in Java
- How to declare a two-dimensional array in C#
- How to declare an attribute in Python without a value?
- Declare a const array in C#
- How do you declare an interface in C++?
- How to write/declare an interface inside a class in Java?
- How to Declare an Object with Computed Property Name in JavaScript?
- How do I declare a 2d array in C++ using new

Advertisements