SlideShare a Scribd company logo
@ 2010 Tata McGraw-Hill Education
1
Education
Methods in C#Methods in C#
@ 2010 Tata McGraw-Hill Education
2
Education
Introduction
In object-oriented programming, objects are used as building blocks in
developing a program. They are the runtime entities. They may represent a
person, a place, a bank account, a table of data or any item that the program
handles. Objects encapsulate data, and code to manipulate that data. The
code designed to work on the data is known as methods in C#. Methods give
objects their behavioral characteristics. They are used not only to access and
process data contained in the object but also to provide responses to any
messages received from other objects. C# is a pure object-oriented language
and therefore, every method must be contained within a class Remember, in
other languages like VB, C and C++, we can defi ne global functions that are
not associated with a particular class. In this chapter, we shall see how
methods are defined and used in different situations. More about their
applications in the development of object-oriented system will be discussed in
Chapter 12.
@ 2010 Tata McGraw-Hill Education
3
Education
DECLARING METHODS
Methods are declared inside the body of a class, normally after the declaration of data
fields. The general form of a method declaration is
modifiers type methodname (formal-parameter-list)
{
method body
}
Method declaration has five parts:
Name of the method (methodname)
Type of value the method returns ( type )
List of parameters ( formal-parameter-list )
Body of the method
Method modifiers ( modifier )
@ 2010 Tata McGraw-Hill Education
4
Education
THE MAIN METHOD
As mentioned earlier, C# programs start execution at a method named Main( ). This
method must be the static method of a class and must have either int or void as
return type
public static int Main( )
Or
public static void Main( )
The modifier public is used as this method must be called from outside the program.
The Main can also have parameters which may receive values from the command line
at the time of execution.
public static int Main (string [ ] args)
public static void Main (string [ ] args)
@ 2010 Tata McGraw-Hill Education
5
Education
INVOKING METHODS
Once methods have been defined, they must be activated for operations. The process
of activating a method is known as invoking or calling. The invoking is done using the
dot operator as shown below: objectname.methodname( actual-
parameter-list );
NESTING OF METHODS
We mentioned earlier that a method of a class can be invoked only by an object of that
class if it is invoked outside the class. We have also seen an exception where a method
is invoked without using any object and dot operator. That is, a method can be called
using only its name by another method of the same class. This is known as nesting of
methods
@ 2010 Tata McGraw-Hill Education
6
Education
METHOD PARAMETERS
A method invocation creates a copy, specific to that invocation, of the formal
parameters and local variables of that method. The actual argument list of the
invocation assigns values or variable references to the newly created formal
parameters. Within the body of a method, formal parameters can be used like any other
variables.
PASS BY VALUE
By default, method parameters are passed by value. That is, a parameter declared with
no modifier is passed by value and is called a value parameter. When a method is
invoked, the values of actual parameters are assigned to the corresponding formal
parameters
@ 2010 Tata McGraw-Hill Education
7
Education
PASS BY REFERENCE
We have just seen that pass by value is the default behaviour of methods in C#. We can,
however, force the value parameters to be passed by reference. To do this, we use the
ref keyword. A parameter declared with the ref modifi er is a reference parameter.
Example:
void Modify ( ref int x )
THE OUTPUT PARAMETERS
As pointed out earlier, output parameters are used to pass results back to the calling
method. This is achieved by declaring the parameters with an out keyword. Similar to
a reference parameter, an output parameter does not create a new storage location.
@ 2010 Tata McGraw-Hill Education
8
Education
VARIABLE ARGUMENT LISTS
In C#, we can defi ne methods that can handle variable number of arguments using what
are known as parameter arrays. Parameter arrays are declared using the keyword
params.
METHODS OVERLOADING
C# allows us to create more than one method with the same name, but with the
different parameter lists and different definitions. This is called method overloading.
Method overloading is used when methods are required to perform similar tasks but
using different input parameters.

More Related Content

What's hot

Introduction to c#
Introduction to c#Introduction to c#
Java Data Types
Java Data TypesJava Data Types
Java Data Types
Spotle.ai
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
Prem Kumar Badri
 
Friend function
Friend functionFriend function
Friend function
zindadili
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cyclemyrajendra
 
Files in java
Files in javaFiles in java
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#Doncho Minkov
 
Functions in c
Functions in cFunctions in c
Functions in c
sunila tharagaturi
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Vineeta Garg
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
programming9
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
Muhammad Waqas
 
Operators in java
Operators in javaOperators in java
Operators in java
Then Murugeshwari
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
Madishetty Prathibha
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
OOP in C++
OOP in C++OOP in C++
OOP in C++
ppd1961
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
Amit Soni (CTFL)
 

What's hot (20)

Introduction to c#
Introduction to c#Introduction to c#
Introduction to c#
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Interface in java
Interface in javaInterface in java
Interface in java
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
 
Friend function
Friend functionFriend function
Friend function
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cycle
 
interface in c#
interface in c#interface in c#
interface in c#
 
Files in java
Files in javaFiles in java
Files in java
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
OOP in C++
OOP in C++OOP in C++
OOP in C++
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 

Similar to Methods in C#

Object Oriented Programming (Advanced )
Object Oriented Programming   (Advanced )Object Oriented Programming   (Advanced )
Object Oriented Programming (Advanced )
ayesha420248
 
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Akhil Mittal
 
The Java Learning Kit Chapter 5 – Methods and Modular.docx
The Java Learning Kit Chapter 5 – Methods and Modular.docxThe Java Learning Kit Chapter 5 – Methods and Modular.docx
The Java Learning Kit Chapter 5 – Methods and Modular.docx
arnoldmeredith47041
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
vekariyakashyap
 
C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01Zafor Iqbal
 
OOPJ.pptx
OOPJ.pptxOOPJ.pptx
OOPJ.pptx
ssuser99ca78
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in Java
Hamad Odhabi
 
Ap Power Point Chpt4
Ap Power Point Chpt4Ap Power Point Chpt4
Ap Power Point Chpt4dplunkett
 
c#.pptx
c#.pptxc#.pptx
Application package
Application packageApplication package
Application packageJAYAARC
 
Learn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & MethodsLearn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & Methods
Eng Teong Cheah
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
Igor Crvenov
 
Bca1020 programming in c
Bca1020  programming in cBca1020  programming in c
Bca1020 programming in c
smumbahelp
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
Ajay Chimmani
 
UNIT1-JAVA.pptx
UNIT1-JAVA.pptxUNIT1-JAVA.pptx
UNIT1-JAVA.pptx
ssuser99ca78
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective cSunny Shaikh
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
Pranali Chaudhari
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rdConnex
 

Similar to Methods in C# (20)

Object Oriented Programming (Advanced )
Object Oriented Programming   (Advanced )Object Oriented Programming   (Advanced )
Object Oriented Programming (Advanced )
 
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
 
7494605
74946057494605
7494605
 
The Java Learning Kit Chapter 5 – Methods and Modular.docx
The Java Learning Kit Chapter 5 – Methods and Modular.docxThe Java Learning Kit Chapter 5 – Methods and Modular.docx
The Java Learning Kit Chapter 5 – Methods and Modular.docx
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
 
C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01
 
OOPJ.pptx
OOPJ.pptxOOPJ.pptx
OOPJ.pptx
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in Java
 
Ap Power Point Chpt4
Ap Power Point Chpt4Ap Power Point Chpt4
Ap Power Point Chpt4
 
c#.pptx
c#.pptxc#.pptx
c#.pptx
 
Application package
Application packageApplication package
Application package
 
Learn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & MethodsLearn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & Methods
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
 
Bca1020 programming in c
Bca1020  programming in cBca1020  programming in c
Bca1020 programming in c
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
 
Essential language features
Essential language featuresEssential language features
Essential language features
 
UNIT1-JAVA.pptx
UNIT1-JAVA.pptxUNIT1-JAVA.pptx
UNIT1-JAVA.pptx
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective c
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rd
 

More from Prasanna Kumar SM

C# Introduction brief
C# Introduction briefC# Introduction brief
C# Introduction brief
Prasanna Kumar SM
 
Structure and Enum in c#
Structure and Enum in c#Structure and Enum in c#
Structure and Enum in c#
Prasanna Kumar SM
 
Overview of c#
Overview of c#Overview of c#
Overview of c#
Prasanna Kumar SM
 
Operators and Expressions in C#
Operators and Expressions in C#Operators and Expressions in C#
Operators and Expressions in C#
Prasanna Kumar SM
 
Decision making and loop in C#
Decision making and loop in C#Decision making and loop in C#
Decision making and loop in C#
Prasanna Kumar SM
 
Characteristics of c#
Characteristics of c#Characteristics of c#
Characteristics of c#
Prasanna Kumar SM
 
C# intro
C# introC# intro

More from Prasanna Kumar SM (7)

C# Introduction brief
C# Introduction briefC# Introduction brief
C# Introduction brief
 
Structure and Enum in c#
Structure and Enum in c#Structure and Enum in c#
Structure and Enum in c#
 
Overview of c#
Overview of c#Overview of c#
Overview of c#
 
Operators and Expressions in C#
Operators and Expressions in C#Operators and Expressions in C#
Operators and Expressions in C#
 
Decision making and loop in C#
Decision making and loop in C#Decision making and loop in C#
Decision making and loop in C#
 
Characteristics of c#
Characteristics of c#Characteristics of c#
Characteristics of c#
 
C# intro
C# introC# intro
C# intro
 

Recently uploaded

Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 

Recently uploaded (20)

Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 

Methods in C#

  • 1. @ 2010 Tata McGraw-Hill Education 1 Education Methods in C#Methods in C#
  • 2. @ 2010 Tata McGraw-Hill Education 2 Education Introduction In object-oriented programming, objects are used as building blocks in developing a program. They are the runtime entities. They may represent a person, a place, a bank account, a table of data or any item that the program handles. Objects encapsulate data, and code to manipulate that data. The code designed to work on the data is known as methods in C#. Methods give objects their behavioral characteristics. They are used not only to access and process data contained in the object but also to provide responses to any messages received from other objects. C# is a pure object-oriented language and therefore, every method must be contained within a class Remember, in other languages like VB, C and C++, we can defi ne global functions that are not associated with a particular class. In this chapter, we shall see how methods are defined and used in different situations. More about their applications in the development of object-oriented system will be discussed in Chapter 12.
  • 3. @ 2010 Tata McGraw-Hill Education 3 Education DECLARING METHODS Methods are declared inside the body of a class, normally after the declaration of data fields. The general form of a method declaration is modifiers type methodname (formal-parameter-list) { method body } Method declaration has five parts: Name of the method (methodname) Type of value the method returns ( type ) List of parameters ( formal-parameter-list ) Body of the method Method modifiers ( modifier )
  • 4. @ 2010 Tata McGraw-Hill Education 4 Education THE MAIN METHOD As mentioned earlier, C# programs start execution at a method named Main( ). This method must be the static method of a class and must have either int or void as return type public static int Main( ) Or public static void Main( ) The modifier public is used as this method must be called from outside the program. The Main can also have parameters which may receive values from the command line at the time of execution. public static int Main (string [ ] args) public static void Main (string [ ] args)
  • 5. @ 2010 Tata McGraw-Hill Education 5 Education INVOKING METHODS Once methods have been defined, they must be activated for operations. The process of activating a method is known as invoking or calling. The invoking is done using the dot operator as shown below: objectname.methodname( actual- parameter-list ); NESTING OF METHODS We mentioned earlier that a method of a class can be invoked only by an object of that class if it is invoked outside the class. We have also seen an exception where a method is invoked without using any object and dot operator. That is, a method can be called using only its name by another method of the same class. This is known as nesting of methods
  • 6. @ 2010 Tata McGraw-Hill Education 6 Education METHOD PARAMETERS A method invocation creates a copy, specific to that invocation, of the formal parameters and local variables of that method. The actual argument list of the invocation assigns values or variable references to the newly created formal parameters. Within the body of a method, formal parameters can be used like any other variables. PASS BY VALUE By default, method parameters are passed by value. That is, a parameter declared with no modifier is passed by value and is called a value parameter. When a method is invoked, the values of actual parameters are assigned to the corresponding formal parameters
  • 7. @ 2010 Tata McGraw-Hill Education 7 Education PASS BY REFERENCE We have just seen that pass by value is the default behaviour of methods in C#. We can, however, force the value parameters to be passed by reference. To do this, we use the ref keyword. A parameter declared with the ref modifi er is a reference parameter. Example: void Modify ( ref int x ) THE OUTPUT PARAMETERS As pointed out earlier, output parameters are used to pass results back to the calling method. This is achieved by declaring the parameters with an out keyword. Similar to a reference parameter, an output parameter does not create a new storage location.
  • 8. @ 2010 Tata McGraw-Hill Education 8 Education VARIABLE ARGUMENT LISTS In C#, we can defi ne methods that can handle variable number of arguments using what are known as parameter arrays. Parameter arrays are declared using the keyword params. METHODS OVERLOADING C# allows us to create more than one method with the same name, but with the different parameter lists and different definitions. This is called method overloading. Method overloading is used when methods are required to perform similar tasks but using different input parameters.