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

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#

Class & Object - User Defined Method
Class & Object - User Defined MethodClass & Object - User Defined Method
Class & Object - User Defined Method
PRN USM
 

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

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
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
PECB
 

Recently uploaded (20)

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
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"
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
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 ...
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
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
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 

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