Advertisement
type[] array-name;
where:
//C# Example of declaring an array
using System;
class A
{
public static void Main()
{
int[] array; //declaration of a 1-D array
}
}
type[] array-name = new type[size]
where:
//C# Example of constructing a 2D array
using System;
class A
{
public static void Main()
{
int[] array= new int[4]; //Array object is constructed on the heap with size to hold 4 int values.
}
}
Array type | Default value |
---|---|
char | '\u0000' |
byte | 0 |
short | 0 |
int | 0 |
long | 0 |
float | 0 |
double | 0 |
bool | false | String | null |
Object | null |
Advertisement
//C# Example of initializing an array
using System;
class A
{
public static void Main()
{
//Array object is constructed on the heap when its size is mentioned at its creation.
int[] oneDA = new int[4];
//storing 10 at index 0 in array
oneDA[0] = 10;
//storing 20 at index 1 in array
oneDA[1] = 20;
//storing 30 at index 2 in array
oneDA[2] = 30;
//storing 40 at index 3 in array
oneDA[3] = 40;
}
}
int[] oneDA = {10, 20, 30, 40};
Console.WriteLine(arr[2]); //Reading 2nd index of this array gives us a value, 30
//C# Example of using the Length property of an array
using System;
class A
{
public static void Main()
{
//Array object is constructed on the heap when its size is mentioned at its creation.
char[] array = new char[4];
array[0] = 'a';
array[1] = 'b';
array[2] = 'c';
array[3] = 'd';
//Accessing the array elements by using its Length property in the for-loop
for(int i=0; i<array.Length; i++)
{
Console.WriteLine(array[i]);
}
}
}
a
b
c
d
//C# Example of array holding object references of the same type
using System;
class A
{
public static void Main()
{
//Array holding 4 object references of type, A.
A[] array= new A[4];
//Creating 4 object of type A
A ob1 = new A();
A ob2 = new A();
A ob3 = new A();
A ob4 = new A();
//Assigning reference of the first object to the first array element
//with index position 0
array[0] = ob1;
//Assigning reference of the second object to the second array element
//with index position 1
array[1] = ob2;
//Assigning reference of the third object to the third array element
//with index position 2
array[2] = ob3;
//Assigning reference of the fourth object to the fourth array element
//with index position 3
array[3] = ob4;
//length of an array.
Console.WriteLine("Length of the array holding object references: "+ array.Length);
//Accessing the array elements by using its Length property in for-loop
for(int i=0; i<array.Length; i++)
{
Console.WriteLine(array[i]);
}
}
}
Length of the array holding object references: 4
A
A
A
A
//C# Example of using the Length property of an array
using System;
class A
{
public static void Main()
{
//An array object to hold 4 objects of type A is constructed and initialized on the heap
//The elements of this array will be automatically initialized to null
A[] array = new A[4];
//Accessing the array elements by using its Length property in the for-loop
for(int i=0; i<array.Length; i++)
{
if(array[i] == null)
Console.WriteLine(array[i] + "A null element");
}
}
}
A null element
A null element
A null element
A null element
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement