0% found this document useful (0 votes)
20 views27 pages

Chapter 6

This document discusses arrays in C#, including: - Arrays store multiple values in a single variable instead of separate variables. They are a collection of variables of the same type stored in contiguous memory locations. - Arrays can solve the problem of needing to store information for multiple students in a student information system, rather than creating separate variables for each additional student. - There are different kinds of arrays like single-dimensional, multidimensional, and jagged arrays. Single-dimensional arrays have a sequence of values and their syntax and accessing values is explained.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views27 pages

Chapter 6

This document discusses arrays in C#, including: - Arrays store multiple values in a single variable instead of separate variables. They are a collection of variables of the same type stored in contiguous memory locations. - Arrays can solve the problem of needing to store information for multiple students in a student information system, rather than creating separate variables for each additional student. - There are different kinds of arrays like single-dimensional, multidimensional, and jagged arrays. Single-dimensional arrays have a sequence of values and their syntax and accessing values is explained.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

COMPUTER PROGRAMMING 2

CHAPTER 6
ARRAYS IN C# (SPECIAL VARIABLE)
OBJECTIVES

 Describe the nature and purpose of an array


 Learn array declaration
 Identify array element
 Define methods with an array as a parameter
 Define methods that return an array
WHAT IS ARRAY?
WHAT IS ARRAY?

 Arraysare used to store multiple values in a


single variable, instead of declaring separate
variables for each value.
 Itis often more useful to think of an array as
a collection of variables of the same type
stored at contiguous memory locations.
PROBLEM

 The school looks for a programmer to create their


student information system. Initially, they have three
students.
PROBLEM

 Therefore you create a


program like this...
 You create 3 different sets
of variables that will hold
the information of 3
students.
PROBLEM

• However, another student


enroll.
• Thus, you edit your code
again and add another set.
PROBLEM

 But another student enroll,


for a total of five. So you
edit your code again.
PROBLEM

 Whatwill happen if there are more than one


hundred or even a thousand students enrolled?
PROBLEM
PROBLEM

 UsingArray we could minimize and optimize our


code like this...
PROBLEM
ARRAY - FACTS

 Variables in the array are ordered and each has an index beginning from
zero (0).
 An array is an object of base type System.Array.
 Default values of numeric array and reference type elements are set to be
respectively zero and null.
 Array elements can be of any type, including an array type.
 Array variables can also be declared like other variables with open and
closed brackets [ ] after the data type.
 The default maximum number of items is 2,147,483,591.
DIFFERENT KINDS OF ARRAY

 Single-Dimensional Arrays
 An array with sequence of value.
 Multidimensional Arrays
 An array that could have multiple column and row.
 Jagged Arrays
 An array whose elements are arrays.
SINGLE-DIMENSIONAL ARRAYS
SINGLE-DIMENSIONAL ARRAY – SYNTAX

 To declare an array.

 Wherein datatype could be any datatype (int,


string, boolean) while size is the maximum
number of items the array could hold.
SINGLE-DIMENSIONAL ARRAY – SYNTAX

To declare a dynamic.

Omitting the size will make a dynamic


number of values
SINGLE-DIMENSIONAL ARRAY – SYNTAX

 To assign a value

 Whereinthe index is the location of the value


in the array. The first value should be placed
in index zero (0).
SINGLE-DIMENSIONAL ARRAY – SYNTAX

 To assign a value a value in dynamic array.

 We need to resize first the array before we can add more


value. Wherein the additionalsize could be one (1) or
more.
SINGLE-DIMENSIONAL ARRAY – ACCESS VALUE

 To access a value
 arrayname[index]
 Wherein the index is the location of the value in the
array.
SINGLE-DIMENSIONAL ARRAY – ACCESS VALUE

 To access all value we simply use a loop (for, while or


do while)
SINGLE-DIMENSIONAL ARRAY – ACCESS VALUE

 Foreach loop
 A loop that executes a block of statements for each
element in an instance of an array.

foreach(datatype variableholder in arrayname)


{
}
SINGLE-DIMENSIONAL ARRAY – ACCESS VALUE

 We could use foreach to access all


values of an array without an
error.
 However, this still shows null
values.
 To prevent displaying blank we
could simply use a dynamic array
or use a conditional statement.
PRACTICE

 Dynamic Arrays
 Utilize dynamic arrays to store a growing number of
elements without predefined size.
 Implement a program that dynamically resizes an array
based on user input.
FURTHER READING

 https://www.tutorialsteacher.com/csharp/array-csharp
 https://www.w3schools.com/cs/cs_arrays.php
 https://
learn.microsoft.com/en-us/dotnet/csharp/language-referen
ce/builtin-types/arrays
TO BE CONTINUED

You might also like