Hello friends, I hope you all are having fun. In today's tutorial, we are going to discuss How to use Arrays in C#. It's our 6th tutorial in C# series and currently we are focusing on basic concepts in C#.
Without understanding these simple concepts, we can't move forward. So, let's get started with Arrays in C#:
How to use Arrays in C# ???
An array (base type System.Array) is a collection of similar (like-typed) data types, called by a common name. Each data item is referred as an element of array and is assigned an index starting from 0.
As C# array is an object so we can find its length using member length, which is actually a count of total number of data items in that array.
Arrays are reference types and implement IEnumerable that's why, we have to use new keyword for new instance of array.
Here's an example of an array in action:
You can see in above code that I have first initialized an array named TEPArray and I have fixed its size to 3.
After that, I have added values in the array at each index 0, 1 and 2. If we count we have 3 positions which is the size of our array.
After that, I am simply printing those values in the console.
Why we need to use Arrays ???
Let's say you are working on some project where you need to show marks of some students, let's say 50 students.
In such cases, it would be too difficult to create separate variable for each student. Instead, you can simply create an array of each subject and save all students' marks in single variable.
In simple words, we use arrays to handle big data of similar data types.
Arrays are strongly typed i.e. they take data of single data type, we can't add multiple data types in single array.
We can't increase Array size after initialization, as in above code I have initialized the array with fixed size of 3 so now if I add a 4th element, then compiler will generate an error. Although, we can use foreach iteration on all arrays in C#, we will discuss that later.
Different ways to initialize Arrays in C#
There are three different ways to initialize arrays in C#, although they are all same at the end but slightly different in syntax.
I have shown all these three ways in below figure:
As you can see in above code, the first way is the same one, we first initialized the array and then added values in it.
In the second way, we have initialized the array and added values in it, in a single line.
In the third way, I have just declared the arrays and haven't added the size or the data in it.
After that I have initialized them and then added values, arrays don't occupy any memory unless initialized.
Here's the complete code, which we have designed so far:
using System;
namespace TEPProject
{
class Program
{
static void Main(string[] args)
{
// First Way: Initialization first & then added values
int[] TEPArray = new int[3];
TEPArray[0] = 1;
TEPArray[1] = 2;
TEPArray[2] = 3;
// Second Way: Initialization & values added in a single line
int[] TEPArray2 = new int[5] { 1, 2, 3, 4, 5 };
Console.WriteLine("Code Designed by www.TheEngineeringProjects.com \n");
Console.WriteLine("Value at Index 0 is : {0} \n", TEPArray[0]);
Console.WriteLine("Value at index 2 is : {0} \n", TEPArray[2]);
Console.WriteLine("Length of Second Array : {0} \n", TEPArray2.Length);
Console.WriteLine("First element of Second Array : {0} \n", TEPArray2[0]);
// Third Way: Declared First & then initialized & values added
string[] TEPArray3, TEPArray4;
TEPArray3 = new string[5] {"Hello","welcome","to","C#","Tutorial"};
TEPArray4 = new string[] { "Designed", "by", "The", "Engineering", "Projects" };
Console.WriteLine("\n\n");
Console.Write("For loop : ");
for (int i = 0; i < TEPArray3.Length; i++)
Console.Write(TEPArray3[i] + " ");
Console.WriteLine("\n\n");
Console.Write("For-each loop : ");
// using for-each loop
foreach (string i in TEPArray4)
Console.Write(" " + i);
Console.WriteLine("\n\n");
}
}
}
Till now, we have discussed only One Dimensional Arrays, now let's have a look at Multi-Dimensional Arrays:
Multi-Dimensional Arrays in C#
We have seen single row of data in arrays so far, but we can also add multiple rows in arrays and such arrays are called Multi-Dimensional Arrays or Rectangular Arrays.
They are called rectangular because length of each row has to be same in a single array.
Let's have a look at Multi-Dimensional Array in below figure:
In the above code, you can see i have created two arrays, the first one TEPArray1 is a 2-Dimensional Array, while TEPArray2 is a 3-Dimensional Array.
I have also used nested for loop to display data of 2D array, if you can't understand it yet, don't worry we will discuss it in detail in coming lectures.
But make sure you understand, how I am accessing elements of multi-dimensional arrays.
Here's the complete code for Multi-Dimensional Arrays in C#:
So, that was all about Arrays in C#. If you got into any errors, then ask in comments. In next lecture, we will have a look at How to use Comments in C#. Till then take care & have fun !!! :)
syedzainnasir
I am Syed Zain Nasir, the founder of The Engineering Projects (TEP). I am a
programmer since 2009 before that I just search things, make small projects and now I am sharing my
knowledge through this platform. I also work as a freelancer and did many projects related to
programming and electrical circuitry. My Google Profile+Follow
Get Connected
Comments on ‘’ How to use Arrays in C# ??? ‘’ ( 0 )