1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no...
-
Upload
francine-king -
Category
Documents
-
view
220 -
download
0
Transcript of 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no...
![Page 1: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/1.jpg)
1
Chapter Three
Using Methods
![Page 2: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/2.jpg)
2
Objectives
• Learn how to write methods with no arguments and no return value
• Learn about implementation hiding and how to use multiple files
• Learn how to write methods that require a single argument
• Learn how to write methods that require multiple arguments
• Learn how to write methods that return a value
![Page 3: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/3.jpg)
3
Objective
• Learn how to use reference and output parameters with methods
• Learn how to overload methods• Learn how to avoid ambiguous methods
![Page 4: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/4.jpg)
4
Writing Methods with No Arguments and No Return Value
• A method is a series of statements that carry out a task• A method can be invoked or called by other methods • Method names are always followed by a set of
parentheses• Programmers can create custom methods• Example:
Console.WriteLine(“HELLO WORLD”);
![Page 5: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/5.jpg)
5
Writing Methods with No Arguments and No Return Value
• In C#, a method must include:– A method declaration– An opening curly brace– A method body– A closing brace
![Page 6: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/6.jpg)
6
Writing Methods with No Arguments and No Return Value
• The optional access modifiers for a method include: public, protected internal, protected, internal, and private
• If an access modifier for a method is not declared, the method becomes private by default
• The static keyword indicates that a method can be called without referring to an object
• Every method has a return type
![Page 7: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/7.jpg)
7
Writing Methods with No Arguments and No Return Value
• This is an example of a method calling another method
![Page 8: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/8.jpg)
8
Hiding Implementation by Using Multiple Files
• An important principle of object-oriented programming is the notion of implementation hiding
• The users of methods should only be aware of the interface
• The following implementation, with two newline escape sequences, could have been used instead of the previous code—neither implementation effects the user
![Page 9: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/9.jpg)
9
Hiding Implementation by Using Multiple Files
• Two different implementations of the WelcomeMessage() method
![Page 10: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/10.jpg)
10
Hiding Implementation by Using Multiple Files
• A multifile assembly is a program that is composed of many different files and classes
• A netmodule file is a file that contains modules to be used as part of another program
• Compiling multifile assemblies and netmodules requires a slightly different command
csc /t:module LogoNamespace.cscsc DemoLogo2.cs /addmodule:LogoNameSpace.netmodule
![Page 11: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/11.jpg)
11
Writing Methods That Require a Single Argument
• Arguments or parameters are used to communicate and send additional information to methods
• The following items are included in the method declaration parentheses when declaring a method that can receive an argument:– The type of the argument– A local name for the argument
![Page 12: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/12.jpg)
12
Writing Methods That Require a Single Argument
• The identifier saleAmount is simply the name the value “goes by” while it is used in the method, regardless of the name the value “goes by” in the calling program
![Page 13: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/13.jpg)
13
Writing Methods That Require a Single Argument
• A formal parameter is a parameter in the method declaration
• An actual parameter refers to an argument within a method call
![Page 14: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/14.jpg)
14
Writing Methods That Require a Single Argument
• Complete program using the ComputeSevenPercentSalesTax() method
![Page 15: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/15.jpg)
15
Writing Methods That Require a Single Argument
• Output of UseSevenPercentSalesTax program
![Page 16: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/16.jpg)
16
Writing Methods That Require Multiple Arguments
• You can pass multiple arguments to a method by listing the arguments within the call to the method and separating them with commas
• You can write a method to take any number of arguments in any order
![Page 17: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/17.jpg)
17
Writing Methods That Return Values
• The return type for a method can be any type used in the C# programming language
• A method can also return nothing, in which case the return type is void
• A method’s return type is also known as a method’s type
![Page 18: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/18.jpg)
18
Writing Methods That Return Values
• The return type in the above example is double• The return statement causes the value stored in gross
to be sent back to any method that calls the CalcPay() method
![Page 19: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/19.jpg)
19
Writing Methods That Return Values
• Program using the CalcPay() method
![Page 20: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/20.jpg)
20
Using ref and out Parameters Within Methods
• In C#, you can write methods with four kinds of formal parameters listed within the parentheses in the method header– Value parameters– Reference parameters– Output parameters– Parameter arrays
![Page 21: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/21.jpg)
21
Using ref and out Parameters Within Methods
• When you use a value parameter in a method header, you indicate the parameter’s type and name, and the method receives a copy of the value passed to it
![Page 22: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/22.jpg)
22
Using ref and out Parameters Within Methods
• Both the reference and output parameters have memory addresses that are passed to a method, allowing it to alter the original variables
• When you use a reference parameter to a method, the parameter must be assigned a value before you use it in the method call
• When you use an output parameter, it does not contain an original value
![Page 23: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/23.jpg)
23
Using ref and out Parameters Within Methods
• Both reference and output parameters act as aliases, or other names, for the same memory location
• The keyword ref is used to indicate a reference parameter
• The keyword out is used to indicate an output parameter
![Page 24: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/24.jpg)
24
Using ref and out Parameters Within Methods
• Program calling method with a reference parameter
![Page 25: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/25.jpg)
25
Using ref and out Parameters Within Methods
• In the preceding code:– The modifier ref precedes the variable in both the
method call and the method header– The passed and received variables occupy the same
memory location– The passed variable was assigned a value
![Page 26: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/26.jpg)
26
Using ref and out Parameters Within Methods
• Unlike the reference parameter, the output parameter does not need a value assigned to it (before it is used in a method call)
• The output parameter is convenient when the passed variable doesn’t have a value yet
• The output parameter gets its value from the method, and these values persist back to the calling program
![Page 27: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/27.jpg)
27
Overloading Methods
• Overloading involves using one term to indicate diverse meanings
• When you overload a C# method, you write multiple methods with a shared name
• The compiler understands the meaning based on the arguments you use with the method
![Page 28: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/28.jpg)
28
Avoiding Ambiguous Methods
• By overloading methods, you run the risk of creating an ambiguous situation
• An ambiguous situation is one in which the compiler cannot determine which method to use
• The compiler usually distinguishes between overloaded methods by the argument lists, but this is NOT always possible
![Page 29: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/29.jpg)
29
Avoiding Ambiguous Methods
• If only one method exists, there is no chance of an ambiguous situation• This code would work fine even if your arguments were both of type int
![Page 30: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/30.jpg)
30
Avoiding Ambiguous Methods
• What happens when another version of the same method is added?• An ambiguous situation does not exist because the compiler can
determine the appropriate method
![Page 31: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/31.jpg)
31
Avoiding Ambiguous Methods
• A more complicated and potentially ambiguous situation arises when the compiler cannot determine which of several versions of a method to use
![Page 32: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/32.jpg)
32
Avoiding Ambiguous Methods
• An ambiguous situation arises because there is no exact match for the method call
• An overloaded method is not ambiguous on its own—it become ambiguous only if you create an ambiguous situation
![Page 33: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/33.jpg)
33
Chapter Summary
• A method is a series of reusable statements that carry out a task
• Invoking programs must know the interface to methods but need not understand the hidden implementation
• Methods receive data in the form of parameters or arguments
• You can pass multiple arguments to a method by listing the arguments within the call to the method and separating them with commas
![Page 34: 1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.](https://reader036.fdocuments.in/reader036/viewer/2022062515/56649ce35503460f949af9e3/html5/thumbnails/34.jpg)
34
Chapter Summary
• The return type for a method can be any type in the C# programming language
• In C#, you can write methods with four kinds of formal parameters listed within the parentheses in the method header
• Overloading a method involves writing multiple methods with the same name, but with different arguments
• The compiler determines which of several versions of a method to call based on argument lists