You are on page 1of 29

Arrays & Strings

An array is a collection of variable of the same type that are referred to by


a common name.
An advantage of an array is that it organizes data in such a way that it can
be easily manipulated.
Arrays have one special attribute: they are implemented as objects.
Two types of arrays we have:
One-Dimensional Arrays
Multi-Dimensional Arrays
Initializing an Array
Syntax:
type[] array-name={val1, val2, val3,, valN};
Different ways to declare an Array:

int[] nums = new int[] {99, 10, 100, 18, 78,


23,63, 9, 87, 49 };

int[] nums;
nums = new int[] {99, 10, 100, 18, 78, 23,
63, 9, 87, 49 };

int[] nums= new int[10]{99, 10, 100, 18, 78,


23,63, 9, 87, 49 };
Jagged Arrays
A jagged array is an array of arrays in which the length of each array can
differ.
Jagged arrays can be used to create a table in which the lengths of the
rows are not the same.
Syntax:
type[][] array-name=new type[size][];
Example:
int[][] aiNum = new int[3][];
aiNum[0] = new int[4];
aiNum[1] = new int[5];
aiNum[2] = new int[3];
Using the Length Property
For each loop is used to cycle through the elements of a collection.
A collection is a group of objects.
C# defines several type of collections
Strings
string defines and supports character strings.
In C# strings are objects. Thus, string is a reference type.
Constructing Strings
The easiest way to construct a string is to use a string literal.
string str = C# strings are good;
You can also create a string from a char array.
char[] charray = {t,e,s,t};
string str = new string(charrray);
Method Description
static string Copy (string str) Returns a copy of str.
int Compare To (string str) Returns less than zero if the invoking
string is less than str, gretate than zero
if the invoking string is greater than str,
and zero if the strings are equal.

int Index Of (string str) Searches the invoking string for the
substring specified by str.

int last Index Of (string str) Searches the invoking string for the
substring specified by str.

string To Lower() Returns a lowercase version of the


invoking string.
string To Upper() Returns an uppercase version of the
invoking string.
Thank You!

You might also like