0% found this document useful (0 votes)
51 views2 pages

Arrays and Strings: I. What Are Arrays?

Arrays allow storing and manipulating a collection of data of the same type. They can be declared with a data type and square brackets, and must be instantiated with the new keyword. Individual elements can be accessed by their index number, with the first element being at index 0. Multidimensional arrays have multiple dimensions, interpreted as nested arrays. Strings in C# are objects that support useful properties like Length and methods like IndexOf and Replace.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views2 pages

Arrays and Strings: I. What Are Arrays?

Arrays allow storing and manipulating a collection of data of the same type. They can be declared with a data type and square brackets, and must be instantiated with the new keyword. Individual elements can be accessed by their index number, with the first element being at index 0. Multidimensional arrays have multiple dimensions, interpreted as nested arrays. Strings in C# are objects that support useful properties like Length and methods like IndexOf and Replace.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

SH1803

Arrays and Strings


I. What are Arrays?
 One of the built-in classes in C# that allow storing and manipulation of data
 It is a data structure used to store a collection of data of the same type.

A. Declaring and Instantiating Arrays


 Arrays can be declared simply by writing the desired data type, a pair of square
brackets ([ ]), and the identifier that will be used to refer/call the array.
 Additionally, since arrays are objects, they need to be instantiated with the new keyword
and another pair of square brackets; the square brackets are used to hold the number of
elements the array should hold.

B. Assigning Array Values


 This can be done by using the array’s identifier and a specific element’s index number.
 Arrays follow zero-indexing; the first element starts on the index number of zero (0).

C. Multiple Array Value Assignment


 Initial values can be assigned to arrays using curly braces ({ }) during array declaration.
 Each value provided within the curly braces should be separated using commas (,).
 Array size and even the new keyword statement can be omitted to simplify the
declaration.

D. Accessing Array Values


 Individual array elements can be accessed by using the array identifier, followed by a
pair of square brackets ([ ]) containing the index number of the element to be accessed.
 Index numbers can be used for a number of things, such as displaying the array’s
contents, or even evaluation of expressions and/or conditions using the array values.

II. Using Arrays in Loops


 It's occasionally necessary to iterate through the elements of an array, making element
assignments based on certain calculations.
 This can be easily done using loops.
 Loops can also be used to read (and display) the values of an array.

A. For foreach Loop


 Provides a shorter and easier way of accessing array elements.
 The data type of the variable in the foreach loop should match the data type of the
array elements.

III. Multidimensional Arrays


 These are arrays that have multiple dimensions; each dimension is usually interpreted as
another array.
 Also called matrices.
 The simplest version of a multidimensional array is the table, which is a two-dimensional
(2D) array.
 They are usually declared as whitespaces within the square brackets ([ ]) of the array data
type declaration; each dimension is separated by commas (,).

05 Handout 1 *Property of STI


[email protected] Page 1 of 2
SH1803

 To access elements, index values for each dimension must be provided on the array element
access statement.

IV. Array Properties and Methods


 The Array class in C# provides various properties and methods to work with arrays most
of the properties can be accessed using the dot syntax, just like any class member:
o The Length property returns the number of elements of the array; this property is useful
in for loops where there may be a need to specify the number of times the loop should
run.
o The Rank property returns the number of dimensions of the array; this property is

A. Array Methods
 These methods are used in arrays and are mathematical in nature.
o The Min method returns the smallest value in the array.
o The Max method returns the largest value in the array.
o The Sum method returns the sum of all elements.

V. Working with Strings


 In reality, strings in C# are objects.
 When you declare a string variable, you basically instantiate an object of type String.
 String objects support a number of useful properties and methods.

A. String Properties and Methods


 The Length property returns the length of the string.
 The IndexOf(value) property-method returns the index of the first occurrence of the
value within the string; as string is read as an actual array of characters, the index value
refers to the index value of a character in a string in zero-index form.
 The Insert(index, value) property-method inserts the value into the string starting from
the specified index value.
 The Remove(index) property-method removes all characters in the string after the
specified index value.
 The Replace(oldValue, newValue) property-method replaces the specified index value
in the string.
 The Substring(index, length) property-method returns a substring of the specified
length, starting from the specified index value; if length is not specified, the operation
continues to the end of the string.
 The Contains(value) property-method returns a Boolean value of true if the string
contains the specified index value.

References
 Microsoft. (n.d.). Introducing Visual Studio.NET. Retrieved May 8, 2018 from
http://msdn.microsoft.com/en-uslibrary/fc6bk1f4(v=vs.71).aspx.
 SoloLearn. (n.d.). C# tutorial. Retrieved on July 17, 2018, from
https://www.sololearn.com/Course/CSharp/

05 Handout 1 *Property of STI


[email protected] Page 2 of 2

You might also like