C# Arrays

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

C#

Arrays & Strings


Arrays
C# provides many built-in classes to store data. Arrays are one
of them.

An array is a data structure that is used for storing a


collection of data.

An array may only store one data type be it integer, float,


string or else.

int[] myArray;
This statement declares an array of integers.

Since arrays are objects they must be instantiated with the new
operator
int[] myArray = new int[5];
This statement instantiates an array named myArray that holds 5 integers.

To assign values to individual elements, assigning using index


number is used like so:
myArray[0] = 23;

We can provide initial values to the array when it’s declared


by using curly brackets:

string[] names = new string[3]{”John”, ”Mary”, ”Jessica”};


double[] prices = new double[4]{3.6, 9.8, 6.4, 5.9};

We may omit the size of the array, if the assigned values amount
represents the size.

string[] names = new string[]{”John”, ”Mary”, ”Jessica”};


double[] prices = new double[]{3.6, 9.8, 6.4, 5.9};
The new operator may also be omitted.

string[] names = {”John”, ”Mary”, ”Jessica”};


double[] prices = {3.6, 9.8, 6.4, 5.9};

Arrays & Loops


To iterate through the elements of an array, loops are used.

int[] IndexArray = new int[10];


for (int i = 0; i < 10; i++){
IndexArray[i] = i*2;}

This assigns values into the array.

for (int i = 0; i < 10; i++){


Console.WriteLine(IndexArray[i])}
This iterates through and outputs the contents of the array.

The foreach Loop


The foreach loop provides a shorter and easier way of accessing
array elements.

foreach (int i in IndexArray){


Console.WriteLine(IndexArray[i])}

Do not try to make foreach loop assign values to an array’s


elements! Use for loop instead.

Multidimensional Arrays
An array can have multiple dimensions. A multidimensional array
is declared as follows:
type[, , … ,] arrayName = new type[size1, …, sizeN];

int[ , ] x = new int[3,4];

(In case of a 2d array, the first


number may be thought of as rows
and the second as columns)

A good way to visualize it is


a table with rows and columns.
Jagged Arrays
A jagged array is an array whose elements are arrays. Jagged
array is an array of arrays.

int[][] jaggedArray = new int[Array size][];

Each dimension is an array, so you can initialize the array upon


declaration like this:

int[][] jaggedArray = new int[][]


{
new int[] {1,8,2,7,9},
new int[] {2,4,6},
new int[] {33,42}
};

Jagged array’s elements may be accessed by following syntax:

int x = jaggedArray[2][3];
This code assigns the value of 6 to the newly declared variable x

Arrays Properties
Various properties and methods are provided to work with arrays,
Length and Rank are examples. Dot syntax is used to access them.

int[] Array = {2, 4, 7};


Console.WriteLine(Array.Length);
Console.WriteLine(Array.Rank);
This code will output the number of elements in an array and the amount of
dimensions

An example of usage the Length operator is in for loops, where


it is needed to specify the number of time the loop should run.

int[] Array = {2, 4, 7};


for(int i=0; i < Array.Length; i++){
Console.WriteLine(Array[i]);
}
There are also other methods available for operators:

Max() - returns the largest number

Min() - returns the smallest number

Sum() - returns the sum of all elements

For example:

int[] Array = {2, 4, 7, 1};


Console.WriteLine(Array.Max()); //Outputs 7
Console.WriteLine(Array.Min()); //Outputs 1
Console.WriteLine(Array.Sum()); //Outputs 14

C# also provides a static Array class with additional methods.

Strings
Working with Strings

It’s common to think of strings as arrays of characters. In


reality, strings is C# are objects.

When a string is declared, an object of type String is being


instantiated.

Some of methods accessible with dot operator, for working with


strings include:
Length – returns the length of the string
IndexOf(value) – returns the index of the first occurrence
of the value within the string
Insert(index,value) – inserts the value into the string
after the specified index
Remove(index, lengthOVERLOAD) – removes all characters in the
string from the specified index. The length argument is used to
remove additional characters after the index
Replace(oldValue,newValue) – replaces the specified value
Substring(index, lengthOVERLOAD) – returns a substring from
the parent string starting at index till the end of the string.
The length argument is used to set the range
StartsWith(value) – returns true if value is at the start
Contains(value) – returns true if value is present
Strings may be also converted to arrays, it is also important
to mention that usually this is done to assign a value to a char
type variable. It uses the following syntax:

string value = ”This is a random string”;


char someChar = value[value.Length-1];
This assigns the last character to someChar, hence someChar now has
contains ‘g’ (notice the single brackets)

You might also like