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");
}
}
}
using System;
namespace TEPProject
{
class Program
{
static void Main(string[] args)
{
string[,] TEPArray1 = new string[4, 2] {
{ "one", "two" },
{ "three", "four" },
{ "five", "six" },
{ "seven", "eight" }
};
int[,,] TEPArray2 = new int[2, 2, 3] {
{
{ 1, 2, 3 },
{ 4, 5, 6 }
},
{
{ 7, 8, 9 },
{ 0, 1, 2 }
}
};
// Accessing array elements.
Console.WriteLine("Code Designed by www.TheEngineeringProjects.com \n");
Console.WriteLine("2DArray[0][0] : " + TEPArray1[0, 0]);
Console.WriteLine("2DArray[0][1] : " + TEPArray1[0, 1]);
Console.WriteLine("2DArray[1][1] : " + TEPArray1[1, 1]);
Console.WriteLine("2DArray[2][0] " + TEPArray1[2, 0]);
Console.WriteLine("\n\n");
Console.WriteLine("3DArray[1][0][1] : " + TEPArray2[1, 0, 1]);
Console.WriteLine("3DArray[1][1][2] : " + TEPArray2[1, 1, 2]);
Console.WriteLine("3DArray[0][1][1] : " + TEPArray2[0, 1, 1]);
Console.WriteLine("3DArray[1][0][2] : " + TEPArray2[1, 0, 2]);
Console.WriteLine("\n\n");
Console.WriteLine("For Loop:");
for (int i = 0; i < 4; i++)
for (int j = 0; j < 2; j++)
Console.Write(TEPArray1[i, j] + " ");
Console.WriteLine("\n\n");
}
}
}
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 !!! :)
JLCPCB – Prototype 10 PCBs for $2 (For Any Color)
China’s Largest PCB Prototype Enterprise, 600,000+ Customers & 10,000+ Online Orders Daily
How to Get PCB Cash Coupon from JLCPCB: https://bit.ly/2GMCH9w