C#
What is .NET Framework? Explain Architecture & Components
What is .Net Framework? .Net Framework is a software development platform developed by Microsoft for...
An array is used to store a collection or series of elements. These elements will be of the same type.
So for example, if you had an array of Integer values, the array could be a collection of values such as [1, 2, 3, 4]. Here the number of elements in the array is 4.
Arrays are useful when you want to store a collection of values of the same type. So instead of declaring a variable for every element, you can just declare one variable.
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.
Code Explanation:-
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.
Code Explanation:-
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.
Note: - In Arrays, the index position starts from 0.
Step 3) Let's now display the individual elements of the array in the Console. Let's add the below code to achieve this.
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:
From the output, you can see all the values of the array being displayed in the Console.
What is .Net Framework? .Net Framework is a software development platform developed by Microsoft for...
In C# file operations, normally streams are used to read and write to files. A stream is an...
Flow Control and conditional statements Flow control and conditional statements are available in...
What is an Interface Class? Interfaces are used along with classes to define what is known as a...
In this tutorial, you will learn- Access Modifiers Constructor Access Modifiers Access Modifiers or...
C# Enumeration An enumeration is used in any programming language to define a constant set of...