csc153 chapter 08
TRANSCRIPT
![Page 1: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/1.jpg)
Microsoft Visual C# 2010Fourth Edition
Chapter 8Advanced Method Concepts
![Page 2: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/2.jpg)
Objectives
• Learn about parameter types: ref parameters, out parameters, and parameter arrays
• Overload methods
• Learn how to avoid ambiguous methods
• Use optional parameters
Microsoft Visual C# 2010, Fourth Edition 2
![Page 3: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/3.jpg)
Microsoft Visual C# 2010, Fourth Edition 3
Understanding Parameter Types
• Mandatory parameter– Argument for it is required in every method call
• Four types of mandatory parameters– Value parameters
• Declared without any modifiers
– Reference parameters• Declared with the ref modifier
– Output parameters• Declared with the out modifier
– Parameter arrays• Declared with the params modifier
![Page 4: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/4.jpg)
Using Mandatory Value Parameters
• Value parameter– Method receives a copy of the value passed to it– Copy is stored at a different memory address than
actual parameter
• Changes to value parameters never affect the original argument in the calling method
Microsoft Visual C# 2010, Fourth Edition 4
![Page 5: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/5.jpg)
Using Value Parameters (cont'd.)
Microsoft Visual C# 2010, Fourth Edition 5
![Page 6: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/6.jpg)
Using Reference and Output Parameters
• Reference and output parameters– Have memory addresses that are passed to a
method, allowing it to alter the original variables
• Differences– Reference parameters need to contain a value
before calling the method– Output parameters do not need to contain a value
• Reference and output parameters act as aliases– For the same memory location occupied by the
original passed variable
Microsoft Visual C# 2010, Fourth Edition 6
![Page 7: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/7.jpg)
Using Reference and Output Parameters (cont'd.)
Microsoft Visual C# 2010, Fourth Edition 7
![Page 8: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/8.jpg)
Using Reference and Output Parameters (cont'd.)
Microsoft Visual C# 2010, Fourth Edition 8
![Page 9: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/9.jpg)
Using Reference and Output Parameters (cont'd.)
• Advantage of using reference and output parameters– Method can change multiple variables
• Disadvantage to using reference and output parameters– Allow multiple methods to have access to the same
data, weakening the “black box” paradigm
Microsoft Visual C# 2010, Fourth Edition 9
![Page 10: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/10.jpg)
Microsoft Visual C# 2010, Fourth Edition 10
Using Parameter Arrays
• Parameter array – A local array declared within the method header by
using the keyword params– Used when you don’t know how many arguments of
the same type you might eventually send to a method
• No additional parameters are permitted after the params keyword
• Only one params keyword is permitted in a method declaration
![Page 11: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/11.jpg)
Microsoft Visual C# 2010, Fourth Edition 11
Using Parameter Arrays (cont'd.)
![Page 12: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/12.jpg)
Microsoft Visual C# 2010, Fourth Edition 12
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– Compiler understands your meaning based on the
arguments you use with the method
• Methods are overloaded correctly when:– They have the same identifier but parameter lists are
different
![Page 13: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/13.jpg)
Microsoft Visual C# 2010, Fourth Edition 13
Overloading Methods (cont'd.)
![Page 14: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/14.jpg)
Microsoft Visual C# 2010, Fourth Edition 14
Overloading Methods (cont'd.)
![Page 15: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/15.jpg)
Microsoft Visual C# 2010, Fourth Edition 15
![Page 16: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/16.jpg)
Understanding Overload Resolution
• Overload resolution– Used by C# to determine which method to execute
when a method call could execute multiple overloaded method
• Applicable methods– Set of methods that can accept a call with a specific
list of arguments
• Betterness rules– Rules that determine which method version to call– Similar to the implicit data type conversion rules
Microsoft Visual C# 2010, Fourth Edition 16
![Page 17: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/17.jpg)
Microsoft Visual C# 2010, Fourth Edition 17
Understanding Overload Resolution (cont'd.)
![Page 18: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/18.jpg)
Understanding Built-In Overloaded Methods
• When you use the IDE to create programs:– Visual Studio’s IntelliSense features provide
information about methods you are using
Microsoft Visual C# 2010, Fourth Edition 18
![Page 19: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/19.jpg)
Avoiding Ambiguous Methods
• Ambiguous method– Compiler cannot determine which method to use– Occurs when you overload methods
• Methods with identical names that have identical parameter lists but different return types– Are not overloaded
Microsoft Visual C# 2010, Fourth Edition 19
![Page 20: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/20.jpg)
Microsoft Visual C# 2010, Fourth Edition 20
Avoiding Ambiguous Methods (cont'd.)
![Page 21: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/21.jpg)
Using Optional Parameters
• Optional parameter– One for which a default value is automatically
supplied
• Make a parameter optional by providing a value for it in the method declaration– Only value parameters can be given default values
• Any optional parameters in a parameter list must follow all mandatory parameters
Microsoft Visual C# 2010, Fourth Edition 21
![Page 22: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/22.jpg)
Microsoft Visual C# 2010, Fourth Edition 22
Using Optional Parameters (cont'd.)
![Page 23: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/23.jpg)
Microsoft Visual C# 2010, Fourth Edition 23
Using Optional Parameters (cont'd.)
![Page 24: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/24.jpg)
Leaving Out Unnamed Arguments
• When calling a method with optional parameters (and you are using unnamed arguments)– Leave out any arguments to the right of the last one
you use
Microsoft Visual C# 2010, Fourth Edition 24
![Page 25: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/25.jpg)
Microsoft Visual C# 2010, Fourth Edition 25
Leaving Out Unnamed Arguments (cont'd.)
![Page 26: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/26.jpg)
Using Named Arguments
• In C# 4.0, leave out optional arguments in a method call– If you pass the remaining arguments by name
• Named arguments appear in any order– But must appear after all the unnamed arguments
have been listed
• Name an argument using its parameter name and a colon before the value
Microsoft Visual C# 2010, Fourth Edition 26
![Page 27: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/27.jpg)
Microsoft Visual C# 2010, Fourth Edition 27
Using Named Arguments (cont'd.)
![Page 28: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/28.jpg)
Overload Resolution with Namedand Optional Arguments
• Named and optional arguments affect overload resolution– Rules for betterness on argument conversions are
applied only for arguments that are given explicitly
• If two signatures are equally good– Signature that does not omit optional parameters is
considered better
Microsoft Visual C# 2010, Fourth Edition 28
![Page 29: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/29.jpg)
You Do It
• Activities to explore– Using Reference Parameters– Overloading Methods
Microsoft Visual C# 2010, Fourth Edition 29
![Page 30: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/30.jpg)
Summary
• Method parameters can be mandatory or optional
• Types of formal parameters– Value parameters– Reference parameters– Output parameters– Parameter arrays
• When you overload a C# method, you write multiple methods with a shared name– But different argument lists
Microsoft Visual C# 2010, Fourth Edition 30
![Page 31: Csc153 chapter 08](https://reader036.vdocument.in/reader036/viewer/2022062705/556d12fbd8b42ad34f8b52a8/html5/thumbnails/31.jpg)
Summary (cont'd.)
• When you overload a method, you run the risk of creating an ambiguous situation
• An optional parameter to a method is one for which a default value is automatically supplied– If you do not explicitly send one as an argument
Microsoft Visual C# 2010, Fourth Edition 31