0% found this document useful (0 votes)
18 views9 pages

08 06 Lecture1 Arrays PassingToMethods

Uploaded by

sjurie21
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)
18 views9 pages

08 06 Lecture1 Arrays PassingToMethods

Uploaded by

sjurie21
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/ 9

Arrays and Methods

Objectives
• Passing Array to Method
• Passing Array Element to Method
Passing an Array to a Method (continued)
static void Main(string[] args)
{
int i;
int[] originalArray = { 1, 2, 3, 4, 5 };
IncreaseAllByTen(originalArray);

for (i = 0; i < originalArray.Length; i++)


Console.WriteLine(originalArray[i]);
Console.ReadLine();
} // end of void Main

static void IncreaseAllByTen(int[] originalArray)


{
int x;
for (x = 0; x < originalArray.Length; x++)
originalArray[x] += 10;
}
Passing an Array to a Method (continued)
static void Main(string[] args)
{
int i;
int[] originalArray = { 1, 2, 3, 4, 5 };
IncreaseAllByTen(originalArray);

for (i = 0; i < originalArray.Length; i++)


Console.WriteLine(originalArray[i]);
Console.ReadLine();
} // end of void Main

static void IncreaseAllByTen(int[] originalArray)


{
int x;
for (x = 0; x < originalArray.Length; x++)
originalArray[x] += 10; // increase each value by 10
}
 The next slide is the same example.

 Array name has changed when it’s passed into the


method.

 The result is the same. Permanent changes have been


made to the array.
Passing an Array to a Method (continued)
 With regards to the previous slides,
any changes made to the array (even if these
changes take place within a method) are permanent
changes, and are reflected throughout the program.
Passing a single array element to a method
static void Main(string[] args)
{
string[] colours = { "blue", "purple", "yellow" };

DisplayColours(colours[2]);

Console.ReadLine();
} // end of void Main

static void DisplayColours(string someColour)


{
Console.Write("The colour is {0}.", someColour);
}
Passing a single array element to a method
static void Main(string[] args)
{
string[] colours = { "blue", "purple", "yellow" };

DisplayColours(colours[2]);

Console.ReadLine();
} // end of void Main

static void DisplayColours(string someColour)


{
Console.Write("The colour is {0}.", someColour);
}

• The colour “yellow” is passed through to the method (not the entire
array).
• It comes through as a normal string variable into the DisplayColor
method.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy