- Back to Home »
- C#.NET »
- C#.NET Arrays
Posted by :
Sudhir Chekuri
Thursday, 31 December 2015
Array is a type used to store multiple values of same data type under a single name.
Arrays allows to store multiple values but all the values should be same data type.
The elements can't exceed the number while creating an array.
Arrays are zero indexed that means index starts with zero.
Arrays are objects in C#.Memory objects are created in heap memory.
Index of an array will be used to retrieve values.
Array.Reverse()is used to reverse the elements of an array.
Array.Sort() is used to print values in ascending order.
Array.Length() is used to get the no.of elements in the array.
Syntax for creating a single dimensional array:
datatype[ ] arrayname=new datatype[no. of elements];
Example
int[ ] i=new int[3];
Example program for creating single dimensional array using for loop
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace sdarray
{
class Program
{
static void Main(string[] args)
{
//int[ ] i = new int[3];
// i[0] = 1;------Assign a value
// i[1] = 2;------Assign a value
// i[2] = 3;------Assign a value
int[] i = new int[3] { 1, 2, 3 };
for (int a = 0; a < 3; a++)
{
Console.WriteLine(i[a]);
}
Console.ReadKey();
}
}
}
Output
1
2
3
Example program for creating single dimensional array using foreach loop
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace sdarray
{
class Program
{
static void Main(string[] args)
{
int[] i = new int[3] { 3,4,5 };
foreach (int a in i)
{
Console.WriteLine(a);
}
Console.ReadKey();
}
}
}
Output
3
4
5
Syntax for creating a multi dimensional array
datatype[ , ] arrayname=new datatype[row,col];
Example
int[ , ] a=new int[2,3];
Example program for creating multi dimensional array using for loop
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace sdarray
{
class Program
{
static void Main(string[] args)
{
int[ , ] a = new int[2,3]{{5,6,3}, { 3,4,5} };
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(a[i, j]);
}
Console.WriteLine("");
}
Console.ReadKey();
}
}
}
Output
563
345
Syntax for creating Jagged Array
datatype[ ][ ] arrayname=new datatype[rows][cols];
Example
int[ ][ ] a=new int[2][ ];
Example program for creating Jagged Array using for loop
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace sdarray
{
class Program
{
static void Main(string[] args)
{
int[ ][ ] a = new int[2][];
a[0] = new int[3] { 3, 4, 5 };
a[1] = new int[4] { 1, 2, 3, 4 };
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < a[i].Length; j++)
{
Console.Write(a[i][j]);
}
Console.WriteLine("");
}
Console.ReadKey();
}
}
}
Output
345
1234
Arrays allows to store multiple values but all the values should be same data type.
The elements can't exceed the number while creating an array.
Arrays are zero indexed that means index starts with zero.
Arrays are objects in C#.Memory objects are created in heap memory.
Index of an array will be used to retrieve values.
Array.Reverse()is used to reverse the elements of an array.
Array.Sort() is used to print values in ascending order.
Array.Length() is used to get the no.of elements in the array.
Single Dimensional Array
Single Dimensional Array is used to store multiple values in a linear fashion.Syntax for creating a single dimensional array:
datatype[ ] arrayname=new datatype[no. of elements];
Example
int[ ] i=new int[3];
Example program for creating single dimensional array using for loop
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace sdarray
{
class Program
{
static void Main(string[] args)
{
//int[ ] i = new int[3];
// i[0] = 1;------Assign a value
// i[1] = 2;------Assign a value
// i[2] = 3;------Assign a value
int[] i = new int[3] { 1, 2, 3 };
for (int a = 0; a < 3; a++)
{
Console.WriteLine(i[a]);
}
Console.ReadKey();
}
}
}
Output
1
2
3
Example program for creating single dimensional array using foreach loop
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace sdarray
{
class Program
{
static void Main(string[] args)
{
int[] i = new int[3] { 3,4,5 };
foreach (int a in i)
{
Console.WriteLine(a);
}
Console.ReadKey();
}
}
}
Output
3
4
5
Multi Dimensional Array
Multi Dimensional Array is used to store elements in the form of a table.Syntax for creating a multi dimensional array
datatype[ , ] arrayname=new datatype[row,col];
Example
int[ , ] a=new int[2,3];
Example program for creating multi dimensional array using for loop
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace sdarray
{
class Program
{
static void Main(string[] args)
{
int[ , ] a = new int[2,3]{{5,6,3}, { 3,4,5} };
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(a[i, j]);
}
Console.WriteLine("");
}
Console.ReadKey();
}
}
}
Output
563
345
Jagged Array
Jagged array is a special type of array which is known as array in array. In this we have static rows and dynamic columns.Syntax for creating Jagged Array
datatype[ ][ ] arrayname=new datatype[rows][cols];
Example
int[ ][ ] a=new int[2][ ];
Example program for creating Jagged Array using for loop
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace sdarray
{
class Program
{
static void Main(string[] args)
{
int[ ][ ] a = new int[2][];
a[0] = new int[3] { 3, 4, 5 };
a[1] = new int[4] { 1, 2, 3, 4 };
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < a[i].Length; j++)
{
Console.Write(a[i][j]);
}
Console.WriteLine("");
}
Console.ReadKey();
}
}
}
Output
345
1234