Microsoft Visual C# 2010       Fourth Edition        Chapter 8 Advanced Method Concepts
Objectives• Learn about parameter types: ref parameters, out  parameters, and parameter arrays• Overload methods• Learn ho...
Understanding Parameter Types• Mandatory parameter    – Argument for it is required in every method call• Four types of ma...
Using Mandatory Value Parameters• Value parameter    – Method receives a copy of the value passed to it    – Copy is store...
Using Value Parameters (contd.)Microsoft Visual C# 2010, Fourth Edition   5
Using Reference and Output                    Parameters• Reference and output parameters    – Have memory addresses that ...
Using Reference and Output                 Parameters (contd.)Microsoft Visual C# 2010, Fourth Edition   7
Using Reference and Output                 Parameters (contd.)Microsoft Visual C# 2010, Fourth Edition   8
Using Reference and Output                 Parameters (contd.)• Advantage of using reference and output  parameters    – M...
Using Parameter Arrays• Parameter array    – A local array declared within the method header by      using the keyword par...
Using Parameter Arrays (contd.)Microsoft Visual C# 2010, Fourth Edition   11
Overloading Methods• Overloading    – Involves using one term to indicate diverse meanings• When you overload a C# method:...
Overloading Methods (contd.)Microsoft Visual C# 2010, Fourth Edition   13
Overloading Methods (contd.)Microsoft Visual C# 2010, Fourth Edition   14
Microsoft Visual C# 2010, Fourth Edition   15
Understanding Overload Resolution• Overload resolution    – Used by C# to determine which method to execute      when a me...
Understanding Overload Resolution                (contd.)Microsoft Visual C# 2010, Fourth Edition   17
Understanding Built-In Overloaded                 Methods• When you use the IDE to create programs:    – Visual Studio’s I...
Avoiding Ambiguous Methods• Ambiguous method    – Compiler cannot determine which method to use    – Occurs when you overl...
Avoiding Ambiguous Methods (contd.)Microsoft Visual C# 2010, Fourth Edition   20
Using Optional Parameters• Optional parameter    – One for which a default value is automatically      supplied• Make a pa...
Using Optional Parameters (contd.)Microsoft Visual C# 2010, Fourth Edition   22
Using Optional Parameters (contd.)Microsoft Visual C# 2010, Fourth Edition   23
Leaving Out Unnamed Arguments• When calling a method with optional parameters  (and you are using unnamed arguments)    – ...
Leaving Out Unnamed Arguments                  (contd.)Microsoft Visual C# 2010, Fourth Edition   25
Using Named Arguments• In C# 4.0, leave out optional arguments in a  method call    – If you pass the remaining arguments ...
Using Named Arguments (contd.)Microsoft Visual C# 2010, Fourth Edition   27
Overload Resolution with Named          and Optional Arguments• Named and optional arguments affect overload  resolution  ...
You Do It• Activities to explore    – Using Reference Parameters    – Overloading MethodsMicrosoft Visual C# 2010, Fourth ...
Summary• Method parameters can be mandatory or optional• Types of formal parameters    –   Value parameters    –   Referen...
Summary (contd.)• When you overload a method, you run the risk of  creating an ambiguous situation• An optional parameter ...
Upcoming SlideShare
Loading in...5
×

Csc153 chapter 08

169

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
169
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Csc153 chapter 08

  1. 1. Microsoft Visual C# 2010 Fourth Edition Chapter 8 Advanced Method Concepts
  2. 2. Objectives• Learn about parameter types: ref parameters, out parameters, and parameter arrays• Overload methods• Learn how to avoid ambiguous methods• Use optional parametersMicrosoft Visual C# 2010, Fourth Edition 2
  3. 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 modifierMicrosoft Visual C# 2010, Fourth Edition 3
  4. 4. 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 methodMicrosoft Visual C# 2010, Fourth Edition 4
  5. 5. Using Value Parameters (contd.)Microsoft Visual C# 2010, Fourth Edition 5
  6. 6. 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 variableMicrosoft Visual C# 2010, Fourth Edition 6
  7. 7. Using Reference and Output Parameters (contd.)Microsoft Visual C# 2010, Fourth Edition 7
  8. 8. Using Reference and Output Parameters (contd.)Microsoft Visual C# 2010, Fourth Edition 8
  9. 9. Using Reference and Output Parameters (contd.)• 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” paradigmMicrosoft Visual C# 2010, Fourth Edition 9
  10. 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 declarationMicrosoft Visual C# 2010, Fourth Edition 10
  11. 11. Using Parameter Arrays (contd.)Microsoft Visual C# 2010, Fourth Edition 11
  12. 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 differentMicrosoft Visual C# 2010, Fourth Edition 12
  13. 13. Overloading Methods (contd.)Microsoft Visual C# 2010, Fourth Edition 13
  14. 14. Overloading Methods (contd.)Microsoft Visual C# 2010, Fourth Edition 14
  15. 15. Microsoft Visual C# 2010, Fourth Edition 15
  16. 16. 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 rulesMicrosoft Visual C# 2010, Fourth Edition 16
  17. 17. Understanding Overload Resolution (contd.)Microsoft Visual C# 2010, Fourth Edition 17
  18. 18. Understanding Built-In Overloaded Methods• When you use the IDE to create programs: – Visual Studio’s IntelliSense features provide information about methods you are usingMicrosoft Visual C# 2010, Fourth Edition 18
  19. 19. 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 overloadedMicrosoft Visual C# 2010, Fourth Edition 19
  20. 20. Avoiding Ambiguous Methods (contd.)Microsoft Visual C# 2010, Fourth Edition 20
  21. 21. 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 parametersMicrosoft Visual C# 2010, Fourth Edition 21
  22. 22. Using Optional Parameters (contd.)Microsoft Visual C# 2010, Fourth Edition 22
  23. 23. Using Optional Parameters (contd.)Microsoft Visual C# 2010, Fourth Edition 23
  24. 24. 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 useMicrosoft Visual C# 2010, Fourth Edition 24
  25. 25. Leaving Out Unnamed Arguments (contd.)Microsoft Visual C# 2010, Fourth Edition 25
  26. 26. 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 valueMicrosoft Visual C# 2010, Fourth Edition 26
  27. 27. Using Named Arguments (contd.)Microsoft Visual C# 2010, Fourth Edition 27
  28. 28. Overload Resolution with Named and 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 betterMicrosoft Visual C# 2010, Fourth Edition 28
  29. 29. You Do It• Activities to explore – Using Reference Parameters – Overloading MethodsMicrosoft Visual C# 2010, Fourth Edition 29
  30. 30. 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 listsMicrosoft Visual C# 2010, Fourth Edition 30
  31. 31. Summary (contd.)• 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 argumentMicrosoft Visual C# 2010, Fourth Edition 31
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×