C# Array Tutorial - Create, Declare, Initialize
C# Array Tutorial - Create, Declare, Initialize
C# Array Tutorial - Create, Declare, Initialize
This variable will point to an array or list of elements, which will be responsible for storing the
elements of the array.
Let's look at how we can work with arrays in C#. In our example, we will declare an array of
Integers and work with them accordingly.
Note that all of the below code is being made to the Program.cs file.
Step 1) Declaring an array – The first step is to declare an array. Let's see how we can achieve
this by the below code example.
/
(/images/c-sharp-net/052616_1018_CFundamenta29.png)
Code Explanation:-
1. The first part is the datatype. It specifies the type of elements used in the array. So in our
case, we are creating an array of Integers.
2. The second part [ ], which specifies the rank of the array. (The rank is a placeholder which
specifies the number of elements the array will contain)
3. Next is the Name of the array which in our case is 'values'. Note you see a green squiggly
underline, don't worry about that. That is just .Net saying that you have declared an array,
but not using it anywhere.
Step 2) The next step is to initialize the array. Here we are going to specify the number of values
the array will hold. We are also going to assign values to each element of the array.
(/images/c-sharp-net/052616_1018_CFundamenta30.png) /
Code Explanation:-
1. First, we are setting the number of elements the array will hold to 3. So in the square
brackets, we are saying that the array will hold 3 elements.
2. Then we are assigning values to each element of the array. We can do this by specifying the
variable name + the index position in the array.
So values[0] means that we are storing a value in the first position of the array. Similarly to
access the second position, we use the notation of values[1] and so on and so forth.
Step 3) Let's now display the individual elements of the array in the Console. Let's add the
below code to achieve this.
(/images/c-sharp-net/052616_1018_CFundamenta31.png)
/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
Int32[] value;
value=new Int32[3];
value[0]=1;
value[1]=2;
value[2]=3;
Console.WriteLine(value[0]);
Console.WriteLine(value[1]);
Console.WriteLine(value[2]);
Console.ReadKey();
}
}
}
Code Explanation:-
This is the simple part wherein we just use the Console.WriteLine method to send each value of
the element to the console.
Note that again, we are accessing each element with the help of the array variable name along
with the index position.
If the above code is entered properly and the program is executed, the following output will be
displayed.
Output:
/
(/images/c-sharp-
net/052616_1018_CFundamenta32.png)
From the output, you can see all the values of the array being displayed in the Console.
Report a Bug
Prev (/c-sharp-conditional-statements.html)
Next (/c-sharp-class-object.html)
C# C# C#
C# C# C#
C# Tutorial
4) C# Hello World (/c-sharp-fundamentals-arrays-enumeration.html)
6) C# Enum(Enumeration) (/c-sharp-enum.html)
(https://www.facebook.com/guru99com/)
(https://twitter.com/guru99com)
(https://www.linkedin.com/company/guru99/)
(https://www.youtube.com/channel/UC19i1XD6k88KqHlET8atqFQ)
(https://forms.aweber.com/form/46/724807646.htm)
About
About Us (/about-us.html)
Advertise with Us (/advertise-us.html)
Write For Us (/become-an-instructor.html)
Contact Us (/contact-us.html)
/
Career Suggestion
SAP Career Suggestion Tool (/best-sap-module.html)
Software Testing as a Career (/software-testing-career-
complete-guide.html)
Interesting
eBook (/ebook-pdf.html)
Blog (/blog/)
Quiz (/tests.html)
SAP eBook (/sap-ebook-pdf.html)
Execute online
Execute Java Online (/try-java-editor.html)
Execute Javascript (/execute-javascript-online.html)
Execute HTML (/execute-html-online.html)
Execute Python (/execute-python-online.html)