SlideShare ist ein Scribd-Unternehmen logo
1 von 15
Downloaden Sie, um offline zu lesen
C# FOR BEGINNERS
LESSON 8
MICHEAL OGUNDERO
CONTACT :
EMAIL – OGUNDEROAYODEJI@GMAIL.COM
Methods
INTRODUCTION
• A method is a block of code with a name which can be executed
anywhere in the program by using its name.
• Data can be passed and received in a method.
• Methods are Subset of Classes or Structs
STRUCTURE OF A METHOD…
• The Method header - This contains the characteristics of the method such as the Name,
parameters and ReturnType
• The Method Body - this contains the execution processes from start to finish.
• The Method Signature - is a unique identification.The signature consists of a method name and
the type and kind of each of its formal parameters. Method signature does not include the return
type.
class class_name
{
...
...
<Access_Specifier> <Return_Type> Method_Name(<Parameters>)
{
// Statements to Execute
}
...
...
}
STRUCTURE OF A METHOD
Access_Specifier - It is used to define an access level either public or private, etc.
to allow other classes to access the method. If we didn’t mention any access modifier,
then by default it is private.
Return_Type - It is used to specify the type of value the method can return. In case, if
method is not returning any value, then we need to mention void as return type.
Method_Name - It must be a unique name to identify the method in a class.
Parameters - The method parameters are used to send or receive a data from
method and these method parameters are enclosed within parentheses and are
separated by commas.
In case, if no parameters are required for a method then, we need to define a method
with empty parentheses.
METHOD EXAMPLE I
• Public – This allows the method
to be accessible in another
class.
• int - This is the return of the
method and it simply means
your final result will be an Int
datatype.
• Sum - This is the name of the
method.
static void Main(string[] args)
{
}
public int Sum(int x, int y)
{
return x + y;
}
A method that adds two numbers of int datatype and returns and
integer
STATIC METHODS
In c#, if we create a methods with static, then we can directly invoke those methods
from class level without creating an instance of object.
public static string GetUserDetails(string name, int age)
{
string info = string.Format("Name: {0},Age: {1}", name, age);
return info;
}
IMPLEMENTATION
The ”GetUserDetailsMethod” is implemented in the ”Main” method
static void Main(string[] args)
{
string result = GetUserDetails(”Avero Tosh", 22);
Console.ReadLine();
}
METHODSWITH NO RETURNTYPE AND PARAMETERS
void ShowSum(int x, int y)
{
Console.WriteLine( x + y);
}
This method has no return type because void was used
METHODSWITH NO RETURNTYPE AND PARAMETERS
public static void DisplayUserDetails()
{
string name = ”Dapo Olumide";
int age = 10;
Console.WriteLine("Name: {0},Age: {1}", name, age);
}
IMPLEMENTATION
static void Main(string[] args)
{
DisplayUserDetails(”Avero Tosh", 22);
Console.ReadLine();
}
METHOD OVERLOADING…
static void Main(string[] args)
{
int myNum1 = PlusMethodInt(8, 5);
double myNum2 = PlusMethodDouble(4.3, 6.26);
Console.WriteLine("Int: " + myNum1);
Console.WriteLine("Double: " + myNum2);
}
static int PlusMethodInt(int x, int y)
{ return x + y; }
static double PlusMethodDouble(double x, double y)
{ return x + y; }
With method overloading, multiple methods can have the same name with different parameters:
METHOD OVERLOADING
static void Main(string[] args)
{
int result = PlusMethod(8, 5);
double result2 = PlusMethod(3, 6, 5);
Console.WriteLine(result);
Console.WriteLine(result2);
}
static int PlusMethod(int x, int y)
{ return x + y; }
static int PlusMethod(int x, int y, int z)
{ return x + y+z; }
Instead of defining two methods that should do the same thing, it is better to overload one:
CLASS EXERCISES
• Write a program in C# Sharp to create a function to calculate the result of raising an integer
number to another
• Write a program in C# Sharp to create a function to calculate the sum of the individual digits of a
given two-digit number
ASSIGNMENT
REFERENCES
• Visual C# How to Program (6th Edition) (Deitel Series)
• https://www.tutlane.com/tutorial/csharp/csharp-methods-functions-with-examples
• https://www.w3schools.com/cs/cs_method_overloading.asp

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
البرمجة الهدفية بلغة جافا - مفاهيم أساسية البرمجة الهدفية بلغة جافا - مفاهيم أساسية
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
 
Basic c#
Basic c#Basic c#
Basic c#
 
Class or Object
Class or ObjectClass or Object
Class or Object
 
البرمجة الهدفية بلغة جافا - تعدد الأشكال
البرمجة الهدفية بلغة جافا - تعدد الأشكالالبرمجة الهدفية بلغة جافا - تعدد الأشكال
البرمجة الهدفية بلغة جافا - تعدد الأشكال
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - Abstraction
 
Interface
InterfaceInterface
Interface
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritance
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
 
Classes,object and methods java
Classes,object and methods javaClasses,object and methods java
Classes,object and methods java
 
object oriented programming OOP
object oriented programming OOPobject oriented programming OOP
object oriented programming OOP
 
Abstract method
Abstract methodAbstract method
Abstract method
 
Lecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptLecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops concept
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packages
 
Lec4
Lec4Lec4
Lec4
 
Object and class
Object and classObject and class
Object and class
 
البرمجة الهدفية بلغة جافا - مصفوفة الكائنات
البرمجة الهدفية  بلغة جافا - مصفوفة الكائناتالبرمجة الهدفية  بلغة جافا - مصفوفة الكائنات
البرمجة الهدفية بلغة جافا - مصفوفة الكائنات
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2
 
‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3‫Object Oriented Programming_Lecture 3
‫Object Oriented Programming_Lecture 3
 

Ähnlich wie static methods

Class & Object - User Defined Method
Class & Object - User Defined MethodClass & Object - User Defined Method
Class & Object - User Defined Method
PRN USM
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6
sotlsoc
 
Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4
Vince Vo
 
Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]
Palak Sanghani
 

Ähnlich wie static methods (20)

Working with Methods in Java.pptx
Working with Methods in Java.pptxWorking with Methods in Java.pptx
Working with Methods in Java.pptx
 
Class & Object - User Defined Method
Class & Object - User Defined MethodClass & Object - User Defined Method
Class & Object - User Defined Method
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6
 
Computer programming 2 Lesson 15
Computer programming 2  Lesson 15Computer programming 2  Lesson 15
Computer programming 2 Lesson 15
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: Methods
 
Methods In C-Sharp (C#)
Methods In C-Sharp (C#)Methods In C-Sharp (C#)
Methods In C-Sharp (C#)
 
Methods.ppt
Methods.pptMethods.ppt
Methods.ppt
 
Xamarin: C# Methods
Xamarin: C# MethodsXamarin: C# Methods
Xamarin: C# Methods
 
Java methods or Subroutines or Functions
Java methods or Subroutines or FunctionsJava methods or Subroutines or Functions
Java methods or Subroutines or Functions
 
Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part I
 
09. Methods
09. Methods09. Methods
09. Methods
 
Methods intro-1.0
Methods intro-1.0Methods intro-1.0
Methods intro-1.0
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Polymorphism.pptx
Polymorphism.pptxPolymorphism.pptx
Polymorphism.pptx
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]
 
09. Java Methods
09. Java Methods09. Java Methods
09. Java Methods
 
CJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxCJP Unit-1 contd.pptx
CJP Unit-1 contd.pptx
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 

Mehr von Micheal Ogundero

Mehr von Micheal Ogundero (15)

csharp repitition structures
csharp repitition structurescsharp repitition structures
csharp repitition structures
 
selection structures
selection structuresselection structures
selection structures
 
escape sequences and substitution markers
escape sequences and substitution markersescape sequences and substitution markers
escape sequences and substitution markers
 
A simple program C# program
A simple program C# programA simple program C# program
A simple program C# program
 
c# operators
c# operatorsc# operators
c# operators
 
datatypes_variables_constants
datatypes_variables_constantsdatatypes_variables_constants
datatypes_variables_constants
 
2 robot types_classifications
2 robot types_classifications2 robot types_classifications
2 robot types_classifications
 
History of robots
History of robotsHistory of robots
History of robots
 
c# keywords, identifiers and Naming Conventions
c# keywords, identifiers and Naming Conventionsc# keywords, identifiers and Naming Conventions
c# keywords, identifiers and Naming Conventions
 
Dictionary and sets-converted
Dictionary and sets-convertedDictionary and sets-converted
Dictionary and sets-converted
 
Basic Sorting algorithms csharp
Basic Sorting algorithms csharpBasic Sorting algorithms csharp
Basic Sorting algorithms csharp
 
Csharp_List
Csharp_ListCsharp_List
Csharp_List
 
c# Enumerations
c# Enumerationsc# Enumerations
c# Enumerations
 
csharp_Passing_parameters_by_value_and_reference
csharp_Passing_parameters_by_value_and_referencecsharp_Passing_parameters_by_value_and_reference
csharp_Passing_parameters_by_value_and_reference
 
C# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data TypesC# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data Types
 

Kürzlich hochgeladen

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
 

Kürzlich hochgeladen (20)

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
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
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
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
 
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
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
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.
 
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
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 

static methods

  • 1. C# FOR BEGINNERS LESSON 8 MICHEAL OGUNDERO CONTACT : EMAIL – OGUNDEROAYODEJI@GMAIL.COM Methods
  • 2. INTRODUCTION • A method is a block of code with a name which can be executed anywhere in the program by using its name. • Data can be passed and received in a method. • Methods are Subset of Classes or Structs
  • 3. STRUCTURE OF A METHOD… • The Method header - This contains the characteristics of the method such as the Name, parameters and ReturnType • The Method Body - this contains the execution processes from start to finish. • The Method Signature - is a unique identification.The signature consists of a method name and the type and kind of each of its formal parameters. Method signature does not include the return type. class class_name { ... ... <Access_Specifier> <Return_Type> Method_Name(<Parameters>) { // Statements to Execute } ... ... }
  • 4. STRUCTURE OF A METHOD Access_Specifier - It is used to define an access level either public or private, etc. to allow other classes to access the method. If we didn’t mention any access modifier, then by default it is private. Return_Type - It is used to specify the type of value the method can return. In case, if method is not returning any value, then we need to mention void as return type. Method_Name - It must be a unique name to identify the method in a class. Parameters - The method parameters are used to send or receive a data from method and these method parameters are enclosed within parentheses and are separated by commas. In case, if no parameters are required for a method then, we need to define a method with empty parentheses.
  • 5. METHOD EXAMPLE I • Public – This allows the method to be accessible in another class. • int - This is the return of the method and it simply means your final result will be an Int datatype. • Sum - This is the name of the method. static void Main(string[] args) { } public int Sum(int x, int y) { return x + y; } A method that adds two numbers of int datatype and returns and integer
  • 6. STATIC METHODS In c#, if we create a methods with static, then we can directly invoke those methods from class level without creating an instance of object. public static string GetUserDetails(string name, int age) { string info = string.Format("Name: {0},Age: {1}", name, age); return info; }
  • 7. IMPLEMENTATION The ”GetUserDetailsMethod” is implemented in the ”Main” method static void Main(string[] args) { string result = GetUserDetails(”Avero Tosh", 22); Console.ReadLine(); }
  • 8. METHODSWITH NO RETURNTYPE AND PARAMETERS void ShowSum(int x, int y) { Console.WriteLine( x + y); } This method has no return type because void was used
  • 9. METHODSWITH NO RETURNTYPE AND PARAMETERS public static void DisplayUserDetails() { string name = ”Dapo Olumide"; int age = 10; Console.WriteLine("Name: {0},Age: {1}", name, age); }
  • 10. IMPLEMENTATION static void Main(string[] args) { DisplayUserDetails(”Avero Tosh", 22); Console.ReadLine(); }
  • 11. METHOD OVERLOADING… static void Main(string[] args) { int myNum1 = PlusMethodInt(8, 5); double myNum2 = PlusMethodDouble(4.3, 6.26); Console.WriteLine("Int: " + myNum1); Console.WriteLine("Double: " + myNum2); } static int PlusMethodInt(int x, int y) { return x + y; } static double PlusMethodDouble(double x, double y) { return x + y; } With method overloading, multiple methods can have the same name with different parameters:
  • 12. METHOD OVERLOADING static void Main(string[] args) { int result = PlusMethod(8, 5); double result2 = PlusMethod(3, 6, 5); Console.WriteLine(result); Console.WriteLine(result2); } static int PlusMethod(int x, int y) { return x + y; } static int PlusMethod(int x, int y, int z) { return x + y+z; } Instead of defining two methods that should do the same thing, it is better to overload one:
  • 13. CLASS EXERCISES • Write a program in C# Sharp to create a function to calculate the result of raising an integer number to another • Write a program in C# Sharp to create a function to calculate the sum of the individual digits of a given two-digit number
  • 15. REFERENCES • Visual C# How to Program (6th Edition) (Deitel Series) • https://www.tutlane.com/tutorial/csharp/csharp-methods-functions-with-examples • https://www.w3schools.com/cs/cs_method_overloading.asp