C# (C Sharp) - Methods
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
}
Calling Methods in C#
You can call a method using the name of the method. The following example illustrates this − Live Demo
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();
}
}
}
"c# method"
"c# method parameter"
"c# methods list"
"methods in c# examples"
"c# method return types"
"how to create a method in c#"