SlideShare a Scribd company logo
1 of 14
C# Tutorial
Part 15:
Method
www.siri-kt.blogspot.com
• A method is a group of statements that together
perform a task. Every C# program has at least one class
with a method named Main.
• To use a method, you need to:
• Define the method
• Call the method
• Defining Methods in C#
• When you define a method, you basically declare the
elements of its structure.
• The syntax for defining a method in C# is as follows:
• <Access Specifier> <Return Type> <Method Name>(Parameter List) {
Method Body }
• Following are the various elements of a method:
• Access Specifier:
• This determines the visibility of a variable or a method from
another class.
• Return type:
• A method may return a value. The return type is the data type of
the value the method returns. If the method is not returning any
values, then the return type is void.
• Method name:
• Method name is a unique identifier and it is case sensitive. It
• Parameter list:
• Enclosed between parentheses, the parameters are used to pass
and receive data from a method. The parameter list refers to the
type, order, and number of the parameters of a method.
Parameters are optional; that is, a method may contain no
parameters.
• Method body:
• This contains the set of instructions needed to complete the
required activity.
• Example
• Following code snippet shows a function FindMax that takes two
integer values and returns the larger of the two.
• It has public access specifier, so it can be accessed from outside
• Example
• Following code snippet shows a function FindMax that takes
two integer values and returns the larger of the two. It has
public access specifier, so it can be accessed from outside the
class using an instance of the class.
• class NumberManipulator
• { public int FindMax(int num1, int num2)
• { /* local variable declaration */ int result;
• if (num1 > num2) result = num1;
• else result = num2;
• return result; } ... }
• Calling Methods in C#
• You can call a method using the name of the method. The following
example illustrates this:
• using System;
• namespace CalculatorApplication
• { class NumberManipulator
• { public int FindMax(int num1, int num2)
• { /* local variable declaration */
• int result;
• if (num1 > num2) {result = num1; }
• Else{ result = num2; return result; }
• }
• static void Main(string[] args)
• { /* local variable definition */
• int a = 100; int b = 200; int ret;
• NumberManipulator n = new NumberManipulator();
• //calling the FindMax method
• ret = n.FindMax(a, b);
• Console.WriteLine("Max value is : {0}", ret );
• Console.ReadLine();
• } } }
• When the above code is compiled and executed, it produces the
following result:
• Max value is : 200 You can also call public method from other
classes by using the instance of the class.
• For example, the method FindMax belongs to
the NumberManipulatorclass, you can call it from another
class Test.
• using System;
• namespace CalculatorApplication
• { class NumberManipulator
• { public int FindMax(int num1, int num2)
• { /* local variable declaration */
• int result;
• if(num1 > num2) {result = num1; }
• else {result = num2; return result; }
• } }
• class Test { static void Main(string[] args)
• { /* local variable definition */
• int a = 100; int b = 200;
• int ret;
• NumberManipulator n = new NumberManipulator();
• //calling the FindMax method ret = n.FindMax(a, b);
Console.WriteLine("Max value is : {0}", ret );
• Console.ReadLine();
• } } }
• When the above code is compiled and executed, it produces
the following result:
• Max value is : 200
• Recursive Method Call
• A method can call itself. This is known as recursion. Following is an example that
calculates factorial for a given number using a recursive function:
• using System;
• namespace CalculatorApplication
• { class NumberManipulator
• { public int factorial(int num)
• { /* local variable declaration */ int result;
• if (num == 1) { return 1; } e
• lse { result = factorial(num - 1) * num; return result; } }
• static void Main(string[] args)
• { NumberManipulator n = new NumberManipulator();
• //calling the factorial method
• Console.WriteLine("Factorial of 6 is : {0}", n.factorial(6));
Console.WriteLine("Factorial of 7 is : {0}", n.factorial(7));
Console.WriteLine("Factorial of 8 is : {0}", n.factorial(8)); Console.ReadLine(); }
• } }
• When the above code is compiled and executed, it produces
the following result:
• Factorial of 6 is: 720 Factorial of 7 is: 5040 Factorial of 8
is: 40320 Passing Parameters to a Method
• When method with parameters is called, you need to pass
the parameters to the method. There are three ways that
parameters can be passed to a method:
Mechanism Description
Value
parameters
This method copies the actual value of an argument into
the formal parameter of the function. In this case,
changes made to the parameter inside the function
have no effect on the argument.
Reference
parameters
This method copies the reference to the memory
location of an argument into the formal parameter. This
means that changes made to the parameter affect the
argument.
Output
parameters
This method helps in returning more than one value.
SIRIKT
Sharing Knowledge is Learning
For more visit our website
www.siri-kt.blogspot.com

More Related Content

What's hot

User Defined Functions in MATLAB part 2
User Defined Functions in MATLAB part 2User Defined Functions in MATLAB part 2
User Defined Functions in MATLAB part 2Shameer Ahmed Koya
 
Matlab Programming Tips Part 1
Matlab Programming Tips Part 1Matlab Programming Tips Part 1
Matlab Programming Tips Part 1Shameer Ahmed Koya
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++NUST Stuff
 
Anonymous and Inline Functions in MATLAB
Anonymous and Inline Functions in MATLABAnonymous and Inline Functions in MATLAB
Anonymous and Inline Functions in MATLABShameer Ahmed Koya
 
Csphtp1 06
Csphtp1 06Csphtp1 06
Csphtp1 06HUST
 
Python programming –part 7
Python programming –part 7Python programming –part 7
Python programming –part 7Megha V
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++NUST Stuff
 
Java8: what's new and what's hot
Java8: what's new and what's hotJava8: what's new and what's hot
Java8: what's new and what's hotSergii Maliarov
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringSri Harsha Pamu
 
Python lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce functionPython lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce functionARVIND PANDE
 
CSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and StringsCSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and StringsDhiviya Rose
 

What's hot (20)

User Defined Functions in MATLAB part 2
User Defined Functions in MATLAB part 2User Defined Functions in MATLAB part 2
User Defined Functions in MATLAB part 2
 
C++ lecture 03
C++   lecture 03C++   lecture 03
C++ lecture 03
 
Matlab Programming Tips Part 1
Matlab Programming Tips Part 1Matlab Programming Tips Part 1
Matlab Programming Tips Part 1
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
 
Functions
FunctionsFunctions
Functions
 
Anonymous and Inline Functions in MATLAB
Anonymous and Inline Functions in MATLABAnonymous and Inline Functions in MATLAB
Anonymous and Inline Functions in MATLAB
 
Java method
Java methodJava method
Java method
 
Csphtp1 06
Csphtp1 06Csphtp1 06
Csphtp1 06
 
Functions
FunctionsFunctions
Functions
 
Python programming –part 7
Python programming –part 7Python programming –part 7
Python programming –part 7
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
C++ Template
C++ TemplateC++ Template
C++ Template
 
Java8: what's new and what's hot
Java8: what's new and what's hotJava8: what's new and what's hot
Java8: what's new and what's hot
 
Class 10
Class 10Class 10
Class 10
 
Templates
TemplatesTemplates
Templates
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
 
Python lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce functionPython lambda functions with filter, map & reduce function
Python lambda functions with filter, map & reduce function
 
CSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and StringsCSEG1001Unit 3 Arrays and Strings
CSEG1001Unit 3 Arrays and Strings
 

Similar to 14method in c#

Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Abou Bakr Ashraf
 
ch03-parameters-objects.ppt
ch03-parameters-objects.pptch03-parameters-objects.ppt
ch03-parameters-objects.pptMahyuddin8
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: MethodsSvetlin Nakov
 
Intake 38 data access 3
Intake 38 data access 3Intake 38 data access 3
Intake 38 data access 3Mahmoud Ouf
 
Method parameters in C# - All types of parameter passing in C #
Method parameters in C# - All types of parameter passing in C #Method parameters in C# - All types of parameter passing in C #
Method parameters in C# - All types of parameter passing in C #JayanthiM19
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxsangeeta borde
 
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
 
Class & Object - User Defined Method
Class & Object - User Defined MethodClass & Object - User Defined Method
Class & Object - User Defined MethodPRN USM
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxGebruGetachew2
 
Ifi7184 lesson3
Ifi7184 lesson3Ifi7184 lesson3
Ifi7184 lesson3Sónia
 
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 JavaHamad Odhabi
 

Similar to 14method in c# (20)

Functions
FunctionsFunctions
Functions
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
 
09. Methods
09. Methods09. Methods
09. Methods
 
ch03-parameters-objects.ppt
ch03-parameters-objects.pptch03-parameters-objects.ppt
ch03-parameters-objects.ppt
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: Methods
 
Intake 38 data access 3
Intake 38 data access 3Intake 38 data access 3
Intake 38 data access 3
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Parameters
ParametersParameters
Parameters
 
Python_Functions_Unit1.pptx
Python_Functions_Unit1.pptxPython_Functions_Unit1.pptx
Python_Functions_Unit1.pptx
 
Chapter 4 - Classes in Java
Chapter 4 - Classes in JavaChapter 4 - Classes in Java
Chapter 4 - Classes in Java
 
Intake 37 linq2
Intake 37 linq2Intake 37 linq2
Intake 37 linq2
 
Method parameters in C# - All types of parameter passing in C #
Method parameters in C# - All types of parameter passing in C #Method parameters in C# - All types of parameter passing in C #
Method parameters in C# - All types of parameter passing in C #
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 
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...
 
Class & Object - User Defined Method
Class & Object - User Defined MethodClass & Object - User Defined Method
Class & Object - User Defined Method
 
Methods intro-1.0
Methods intro-1.0Methods intro-1.0
Methods intro-1.0
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
 
Ifi7184 lesson3
Ifi7184 lesson3Ifi7184 lesson3
Ifi7184 lesson3
 
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
 

More from Sireesh K (20)

Cn10
Cn10Cn10
Cn10
 
chanakya neeti
chanakya neetichanakya neeti
chanakya neeti
 
chanakya neeti
chanakya neetichanakya neeti
chanakya neeti
 
What is mvc
What is mvcWhat is mvc
What is mvc
 
31c
31c31c
31c
 
31cs
31cs31cs
31cs
 
45c
45c45c
45c
 
44c
44c44c
44c
 
43c
43c43c
43c
 
42c
42c42c
42c
 
41c
41c41c
41c
 
40c
40c40c
40c
 
39c
39c39c
39c
 
38c
38c38c
38c
 
37c
37c37c
37c
 
35c
35c35c
35c
 
34c
34c34c
34c
 
33c
33c33c
33c
 
30c
30c30c
30c
 
29c
29c29c
29c
 

Recently uploaded

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 

Recently uploaded (20)

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 

14method in c#

  • 2. • A method is a group of statements that together perform a task. Every C# program has at least one class with a method named Main. • To use a method, you need to: • Define the method • Call the method • Defining Methods in C# • When you define a method, you basically declare the elements of its structure.
  • 3. • The syntax for defining a method in C# is as follows: • <Access Specifier> <Return Type> <Method Name>(Parameter List) { Method Body } • Following are the various elements of a method: • Access Specifier: • This determines the visibility of a variable or a method from another class. • Return type: • A method may return a value. The return type is the data type of the value the method returns. If the method is not returning any values, then the return type is void. • Method name: • Method name is a unique identifier and it is case sensitive. It
  • 4. • Parameter list: • Enclosed between parentheses, the parameters are used to pass and receive data from a method. The parameter list refers to the type, order, and number of the parameters of a method. Parameters are optional; that is, a method may contain no parameters. • Method body: • This contains the set of instructions needed to complete the required activity. • Example • Following code snippet shows a function FindMax that takes two integer values and returns the larger of the two. • It has public access specifier, so it can be accessed from outside
  • 5. • Example • Following code snippet shows a function FindMax that takes two integer values and returns the larger of the two. It has public access specifier, so it can be accessed from outside the class using an instance of the class. • class NumberManipulator • { public int FindMax(int num1, int num2) • { /* local variable declaration */ int result; • if (num1 > num2) result = num1; • else result = num2; • return result; } ... }
  • 6. • Calling Methods in C# • You can call a method using the name of the method. The following example illustrates this: • using System; • namespace CalculatorApplication • { class NumberManipulator • { public int FindMax(int num1, int num2) • { /* local variable declaration */ • int result; • if (num1 > num2) {result = num1; } • Else{ result = num2; return result; } • }
  • 7. • static void Main(string[] args) • { /* local variable definition */ • int a = 100; int b = 200; int ret; • NumberManipulator n = new NumberManipulator(); • //calling the FindMax method • ret = n.FindMax(a, b); • Console.WriteLine("Max value is : {0}", ret ); • Console.ReadLine(); • } } }
  • 8. • When the above code is compiled and executed, it produces the following result: • Max value is : 200 You can also call public method from other classes by using the instance of the class. • For example, the method FindMax belongs to the NumberManipulatorclass, you can call it from another class Test. • using System; • namespace CalculatorApplication • { class NumberManipulator • { public int FindMax(int num1, int num2) • { /* local variable declaration */ • int result; • if(num1 > num2) {result = num1; } • else {result = num2; return result; } • } }
  • 9. • class Test { static void Main(string[] args) • { /* local variable definition */ • int a = 100; int b = 200; • int ret; • NumberManipulator n = new NumberManipulator(); • //calling the FindMax method ret = n.FindMax(a, b); Console.WriteLine("Max value is : {0}", ret ); • Console.ReadLine(); • } } } • When the above code is compiled and executed, it produces the following result: • Max value is : 200
  • 10. • Recursive Method Call • A method can call itself. This is known as recursion. Following is an example that calculates factorial for a given number using a recursive function: • using System; • namespace CalculatorApplication • { class NumberManipulator • { public int factorial(int num) • { /* local variable declaration */ int result; • if (num == 1) { return 1; } e • lse { result = factorial(num - 1) * num; return result; } } • static void Main(string[] args) • { NumberManipulator n = new NumberManipulator(); • //calling the factorial method • Console.WriteLine("Factorial of 6 is : {0}", n.factorial(6)); Console.WriteLine("Factorial of 7 is : {0}", n.factorial(7)); Console.WriteLine("Factorial of 8 is : {0}", n.factorial(8)); Console.ReadLine(); } • } }
  • 11. • When the above code is compiled and executed, it produces the following result: • Factorial of 6 is: 720 Factorial of 7 is: 5040 Factorial of 8 is: 40320 Passing Parameters to a Method • When method with parameters is called, you need to pass the parameters to the method. There are three ways that parameters can be passed to a method:
  • 12. Mechanism Description Value parameters This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. Reference parameters This method copies the reference to the memory location of an argument into the formal parameter. This means that changes made to the parameter affect the argument. Output parameters This method helps in returning more than one value.
  • 14. For more visit our website www.siri-kt.blogspot.com