Getsuyodev, Programming is mainly used when constructing an application. Programming requires knowledge of application domains, algorithms, and programming language expertise. Programming can be developed in different logic based on developer knowledge.

C# Array Examples



Arrays are using for store similar data types grouping as a single unit. We can access Array elements by its numeric index. The array indexes start at zero. The default value of numeric array elements are set to zero, and reference elements are set to null.

Integer Array

Adding values to a C# array

Declaring and Initializing an Integer Array

int[] array = new int[4];
array[0] = 10;<br>
array[1] = 20;<br>
array[2] = 30;<br>
array[3] = 40;

In the above code, we declare an Integer Array of four elements and assign the value to an array index. That means we assign values to array index 0 – 4.

The following code shows how to add items in an Array using for loop.

 
int[] items = new int[100];
for (int cnt = 0; cnt <= 100; cnt++)
{
    items[cnt] = yourValueHere;
}

We can retrieve these values from the Array by using a for a loop.

for (int i = 0; i < array.Length; i++)
{
  MessageBox.Show (array[i]);
}

Initialize Array

Also, we can declare and initialize an array in one statement.

int[] array = new int[] {10, 20, 30, 40};

Note that in the above code we did not specify the length of the Array so the compiler will do it for us.

How to print contents of Array horizontally?

int[] array = new int[] {10, 20, 30, 40};
foreach(var item in array)
{
    Console.Write(item.ToString());
}

String Array

Declaring and Initializing a String Array

string[] week = new string[7];
week[0] = "Sunday";
week[1] = "Monday";

The above C# code declares a string array of 7 strings and assigns some values to it.

string[] week = new string[] {"Sunday","Monday","Tuesday"};

The above code declares and initializes a string array with values.

string str = week[1];

We can access the Arrays elements by providing its numerical index, the above statement we access the second value from the week Array.

In the following program, we declare an Array “week” capable of seven String values and assigns the seven values as days in a week. Next step is to retrieve the elements of the Array using a for a loop. For finding the end of an Array, we used the Length function of Array Object.

string[] week = new string[7];
week[0] = "Sunday";
week[1] = "Monday";
week[2] = "Tuesday";
week[3] = "Wednsday";
week[4] = "Thursday";
week[5] = "friday";
week[6] = "Saturday";
for (int i = 0; i < = week.Length-1; i++)
{
    MessageBox.Show(week[i]);
}

How to find the length(size) of an Array?

Array. Length Property returned the total number of elements in all the dimensions of the Array.

 
String[] flag = new string[] {"TRUE","FALSE"};
int sizeOfArray = flag.Length;

How to resize an Array

An array can be resized with Array.Resize < T > Method that means We make an array bigger or smaller. Array.Resize < T > Method Changes the number of elements of a one-dimensional array to the specified new size.

Array.Resize < T > – T is the type of the elements of the Array.

This method should be used with only one dimensional Array. This method allocates a new array with the specified size, copies elements from the old array to the new one, and then replace the old Array with the new one.

Resize Array

// Initialize array for example
char[] array = new char[5];
array[0] = 'A';
array[1] = 'B';
array[2] = 'C';
array[3] = 'D';
array[4] = 'E';
for (int i = 0; i < array.Length; i++)
{
    MessageBox.Show (array[i].ToString ());
}
Array.Resize(ref array, 3);
for (int i = 0; i < array.Length; i++)
{
    MessageBox.Show(array[i].ToString ());
}

Array.Resize(ref array, 3);

In the above code, we resize the Array to 3 elements.

for..each loop and Array

int[] array = { 10, 30, 50 }; //array declaration
foreach (int element in array)
{
  Console.WriteLine(element);
}

Converting String array to List

string[] week = new string[7];
week[0] = "Sunday";
week[1] = "Monday";
week[2] = "Tuesday";
week[3] = "Wednsday";
week[4] = "Thursday";
week[5] = "friday";
week[6] = "Saturday";
List<string> lst = new List<string>(week);
foreach (string day in lst)
{
    MessageBox.Show(day);
}

How can I test if an array contains a specific value?

string[] week = new string[7];
week[0] = "Sunday";
week[1] = "Monday";
week[2] = "Tuesday";
week[3] = "Wednsday";
week[4] = "Thursday";
week[5] = "friday";
week[6] = "Saturday";
string value = "Wednsday";
int pos = Array.IndexOf(week, value);
if (pos > -1)
    MessageBox.Show(value + " exist !");
else
    MessageBox.Show(value + " not exist !");

How to check if a value exists in an array?

The following program shows how to find an element from an Array.

string stringToCheck = "GHI";
string[] stringArray = { "ABC", "DEF", "GHI", "JKL" };
foreach (string x in stringArray)
{
    if (x.Equals (stringToCheck))
    {
        MessageBox.Show("Find the string ..." + x);
    }
}
PropertyDescription
IsFixedSizeReturns a value indicating whether the Array has a fixed size.
IsReadOnlyReturns a value indicating whether the Array is read-only.
IsSynchronizedReturns a value indicating whether access to the Array is synchronized.
LengthReturns the total number of elements in all the dimensions of the Array.
LongLengthReturns a 64-bit integer that represents the total number of elements in all the dimensions of the Array.

MethodsDescription
Clear(Array, Int32, Int32)Sets a range of elements in the Array to zero, to false, or to Nothing, depending on the element type.
Clone()Creates a shallow copy of the Array.
Copy(Array, Array, Int32)Copies elements from one array to another. The length is specified as a 32-bit integer.
CreateInstance(Type, Int32)Creates a one-dimensional Array of the specified Type and length, with zero-based indexing.
Equals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object)
GetEnumerator()Returns an IEnumerator for the Array.
GetType()Gets the Type of the current instance. (Inherited from Object)
IndexOf(Array, Object)Searches for the specified object and returns the index of its first occurrence in a one-dimensional array.
Reverse(Array)Reverses the sequence of the elements in the entire one-dimensional Array.
SetValue(Object, Int32)Sets a value to the element at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer.
Sort(Array)Sorts the elements in an entire one-dimensional Array using the IComparable implementation of each element of the Array.
ToString()Returns a string representation.

Comments are closed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More