SlideShare ist ein Scribd-Unternehmen logo
1 von 31
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 how to avoid ambiguous methods
• Use optional parameters




Microsoft Visual C# 2010, Fourth Edition             2
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
Microsoft Visual C# 2010, Fourth Edition                 3
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
Using Value Parameters (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   5
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
Using Reference and Output
                 Parameters (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   7
Using Reference and Output
                 Parameters (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   8
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
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


Microsoft Visual C# 2010, Fourth Edition               10
Using Parameter Arrays (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   11
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


Microsoft Visual C# 2010, Fourth Edition                  12
Overloading Methods (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   13
Overloading Methods (cont'd.)




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 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
Understanding Overload Resolution
                (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   17
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
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
Avoiding Ambiguous Methods (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   20
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
Using Optional Parameters (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   22
Using Optional Parameters (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   23
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
Leaving Out Unnamed Arguments
                  (cont'd.)




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 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
Using Named Arguments (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   27
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 better




Microsoft Visual C# 2010, Fourth Edition                     28
You Do It
• Activities to explore
    – Using Reference Parameters
    – Overloading Methods




Microsoft Visual C# 2010, Fourth Edition         29
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
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

Weitere ähnliche Inhalte

Was ist angesagt?

9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02Terry Yoast
 
Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Warren0989
 
Functions ppt ch06
Functions ppt ch06Functions ppt ch06
Functions ppt ch06Terry Yoast
 
Computer Programming
Computer ProgrammingComputer Programming
Computer ProgrammingBurhan Fakhar
 
9781439035665 ppt ch04
9781439035665 ppt ch049781439035665 ppt ch04
9781439035665 ppt ch04Terry Yoast
 
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12Terry Yoast
 
9781439035665 ppt ch02
9781439035665 ppt ch029781439035665 ppt ch02
9781439035665 ppt ch02Terry Yoast
 
Operators and Expressions in C#
Operators and Expressions in C#Operators and Expressions in C#
Operators and Expressions in C#Prasanna Kumar SM
 
Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaAdan Hubahib
 
Generating UML Models with Inferred Types from Pharo Code
Generating UML Models with Inferred Types from Pharo CodeGenerating UML Models with Inferred Types from Pharo Code
Generating UML Models with Inferred Types from Pharo CodeESUG
 
9781111530532 ppt ch02
9781111530532 ppt ch029781111530532 ppt ch02
9781111530532 ppt ch02Terry Yoast
 
9781111530532 ppt ch03
9781111530532 ppt ch039781111530532 ppt ch03
9781111530532 ppt ch03Terry Yoast
 

Was ist angesagt? (20)

Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Chapter 4 5
Chapter 4 5Chapter 4 5
Chapter 4 5
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02
 
Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...
 
Chap02
Chap02Chap02
Chap02
 
Chapter 12
Chapter 12Chapter 12
Chapter 12
 
Functions ppt ch06
Functions ppt ch06Functions ppt ch06
Functions ppt ch06
 
Computer Programming
Computer ProgrammingComputer Programming
Computer Programming
 
Chapter2
Chapter2Chapter2
Chapter2
 
9781439035665 ppt ch04
9781439035665 ppt ch049781439035665 ppt ch04
9781439035665 ppt ch04
 
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12
 
9781439035665 ppt ch02
9781439035665 ppt ch029781439035665 ppt ch02
9781439035665 ppt ch02
 
JavaScript functions
JavaScript functionsJavaScript functions
JavaScript functions
 
Operators and Expressions in C#
Operators and Expressions in C#Operators and Expressions in C#
Operators and Expressions in C#
 
Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of Java
 
Generating UML Models with Inferred Types from Pharo Code
Generating UML Models with Inferred Types from Pharo CodeGenerating UML Models with Inferred Types from Pharo Code
Generating UML Models with Inferred Types from Pharo Code
 
7494605
74946057494605
7494605
 
9781111530532 ppt ch02
9781111530532 ppt ch029781111530532 ppt ch02
9781111530532 ppt ch02
 
Intake 38_1
Intake 38_1Intake 38_1
Intake 38_1
 
9781111530532 ppt ch03
9781111530532 ppt ch039781111530532 ppt ch03
9781111530532 ppt ch03
 

Ähnlich wie Csc153 chapter 08

CIS110 Computer Programming Design Chapter (9)
CIS110 Computer Programming Design Chapter  (9)CIS110 Computer Programming Design Chapter  (9)
CIS110 Computer Programming Design Chapter (9)Dr. Ahmed Al Zaidy
 
Intro to Programming: Modularity
Intro to Programming: ModularityIntro to Programming: Modularity
Intro to Programming: ModularityNicole Ryan
 
METHODS OR FUNCTIONS IN C for dotnet.pptx
METHODS OR FUNCTIONS IN C for dotnet.pptxMETHODS OR FUNCTIONS IN C for dotnet.pptx
METHODS OR FUNCTIONS IN C for dotnet.pptxArjunKhanal8
 
9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.pptLokeshK66
 
Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...
Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...
Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...Simplilearn
 
First draft programming c++
First draft programming c++First draft programming c++
First draft programming c++藝輝 王
 
UoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdfUoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdfmadihamaqbool6
 
Software coding & testing, software engineering
Software coding & testing, software engineeringSoftware coding & testing, software engineering
Software coding & testing, software engineeringRupesh Vaishnav
 
Whats Newi in C# 8.0
Whats Newi in C# 8.0Whats Newi in C# 8.0
Whats Newi in C# 8.0Manoj Sharma
 
New c sharp4_features_part_i
New c sharp4_features_part_iNew c sharp4_features_part_i
New c sharp4_features_part_iNico Ludwig
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10Terry Yoast
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10Terry Yoast
 
Presentation Verification & Validation
Presentation Verification & ValidationPresentation Verification & Validation
Presentation Verification & ValidationElmar Selbach
 
chapter11 - Programming.pdf
chapter11 - Programming.pdfchapter11 - Programming.pdf
chapter11 - Programming.pdfsatonaka3
 
Object oriented programming 11 preprocessor directives and program structure
Object oriented programming 11 preprocessor directives and program structureObject oriented programming 11 preprocessor directives and program structure
Object oriented programming 11 preprocessor directives and program structureVaibhav Khanna
 

Ähnlich wie Csc153 chapter 08 (20)

CIS110 Computer Programming Design Chapter (9)
CIS110 Computer Programming Design Chapter  (9)CIS110 Computer Programming Design Chapter  (9)
CIS110 Computer Programming Design Chapter (9)
 
Intro to Programming: Modularity
Intro to Programming: ModularityIntro to Programming: Modularity
Intro to Programming: Modularity
 
METHODS OR FUNCTIONS IN C for dotnet.pptx
METHODS OR FUNCTIONS IN C for dotnet.pptxMETHODS OR FUNCTIONS IN C for dotnet.pptx
METHODS OR FUNCTIONS IN C for dotnet.pptx
 
C++.ppt
C++.pptC++.ppt
C++.ppt
 
9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt
 
lecture_3.ppt
lecture_3.pptlecture_3.ppt
lecture_3.ppt
 
Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...
Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...
Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...
 
Lesson 4.2 5th and 6th step
Lesson 4.2 5th and 6th stepLesson 4.2 5th and 6th step
Lesson 4.2 5th and 6th step
 
First draft programming c++
First draft programming c++First draft programming c++
First draft programming c++
 
Cs111 ch01 v4
Cs111 ch01 v4Cs111 ch01 v4
Cs111 ch01 v4
 
UoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdfUoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdf
 
Software coding & testing, software engineering
Software coding & testing, software engineeringSoftware coding & testing, software engineering
Software coding & testing, software engineering
 
Chapter 04
Chapter 04Chapter 04
Chapter 04
 
Whats Newi in C# 8.0
Whats Newi in C# 8.0Whats Newi in C# 8.0
Whats Newi in C# 8.0
 
New c sharp4_features_part_i
New c sharp4_features_part_iNew c sharp4_features_part_i
New c sharp4_features_part_i
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
 
Presentation Verification & Validation
Presentation Verification & ValidationPresentation Verification & Validation
Presentation Verification & Validation
 
chapter11 - Programming.pdf
chapter11 - Programming.pdfchapter11 - Programming.pdf
chapter11 - Programming.pdf
 
Object oriented programming 11 preprocessor directives and program structure
Object oriented programming 11 preprocessor directives and program structureObject oriented programming 11 preprocessor directives and program structure
Object oriented programming 11 preprocessor directives and program structure
 

Kürzlich hochgeladen

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 

Kürzlich hochgeladen (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

Csc153 chapter 08

  • 1. Microsoft Visual C# 2010 Fourth Edition Chapter 8 Advanced Method Concepts
  • 2. 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
  • 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 Microsoft Visual C# 2010, Fourth Edition 3
  • 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 method Microsoft Visual C# 2010, Fourth Edition 4
  • 5. Using Value Parameters (cont'd.) Microsoft Visual C# 2010, Fourth Edition 5
  • 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 variable Microsoft Visual C# 2010, Fourth Edition 6
  • 7. Using Reference and Output Parameters (cont'd.) Microsoft Visual C# 2010, Fourth Edition 7
  • 8. Using Reference and Output Parameters (cont'd.) Microsoft Visual C# 2010, Fourth Edition 8
  • 9. 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
  • 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 Microsoft Visual C# 2010, Fourth Edition 10
  • 11. Using Parameter Arrays (cont'd.) Microsoft Visual C# 2010, Fourth Edition 11
  • 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 Microsoft Visual C# 2010, Fourth Edition 12
  • 13. Overloading Methods (cont'd.) Microsoft Visual C# 2010, Fourth Edition 13
  • 14. Overloading Methods (cont'd.) Microsoft Visual C# 2010, Fourth Edition 14
  • 15. Microsoft Visual C# 2010, Fourth Edition 15
  • 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 rules Microsoft Visual C# 2010, Fourth Edition 16
  • 17. Understanding Overload Resolution (cont'd.) Microsoft Visual C# 2010, Fourth Edition 17
  • 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 using Microsoft Visual C# 2010, Fourth Edition 18
  • 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 overloaded Microsoft Visual C# 2010, Fourth Edition 19
  • 20. Avoiding Ambiguous Methods (cont'd.) Microsoft Visual C# 2010, Fourth Edition 20
  • 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 parameters Microsoft Visual C# 2010, Fourth Edition 21
  • 22. Using Optional Parameters (cont'd.) Microsoft Visual C# 2010, Fourth Edition 22
  • 23. Using Optional Parameters (cont'd.) Microsoft Visual C# 2010, Fourth Edition 23
  • 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 use Microsoft Visual C# 2010, Fourth Edition 24
  • 25. Leaving Out Unnamed Arguments (cont'd.) Microsoft Visual C# 2010, Fourth Edition 25
  • 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 value Microsoft Visual C# 2010, Fourth Edition 26
  • 27. Using Named Arguments (cont'd.) Microsoft Visual C# 2010, Fourth Edition 27
  • 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 better Microsoft Visual C# 2010, Fourth Edition 28
  • 29. You Do It • Activities to explore – Using Reference Parameters – Overloading Methods Microsoft Visual C# 2010, Fourth Edition 29
  • 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 lists Microsoft Visual C# 2010, Fourth Edition 30
  • 31. 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