This document discusses methods in C#. It defines a method as a block of code with a name that can be executed anywhere in a program. It describes the structure of a method including the method header, body, and signature. It provides examples of different types of methods like static methods, methods with no return type or parameters, and method overloading. It also lists some class exercises and references for further reading.
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);
}
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