In C#, a method can be a function or a procedure (subroutine). A method has a name, and a body contains the sequence of statements to be executed when the method is called. In this tutorial, you will learn how to declare, create, and call a method in C# with examples.
Important Notes
- C# does not support global methods; methods should be written in a class; otherwise, you will not be able to compile the code.
- You must specify the types of parameters and the type of return value.
- A method that returns a value is a function, and a method that does not return any value is a procedure (subroutine).
C# - Declare a Method
In C#, the syntax of declaring a method is as follows:
[returnType | void] methodName (parameterList separated with comma)
{
// sequence of statements
[return expression;]
}
Here are the details of the elements that make up a declaration:
- The returnType is the name of the data type that the method will return. This can be any type such as string, or int. If a Method does not return a value, then void keyword should be used in the place of the return type.
- The methodName is the name of the method. Rules should be followed for method name same as variable names in C#. It is better to follow the camelCase convention for method names, for example, processData.
- The parameterList, parameters are optional but if a method requires parameters then describe its type and name. Parameters should be written in the brackets () and should be separated with a comma if more than one.
- The Curly Braces, method code, should be inside the curly braces {}.
- The return expression will be used if you are creating a method that returns a value. If a method that does not returns a value, means created as void then no need to use the return statement.
C# - Create a Method
The following is an example of a method that returns a value (function). It accepts two parameters and will sum up the value and return.
int sumValues(int aNumber, int bNumber)
{
int nResult;
nResult = aNumber + bNumber;
return nResult;
}
Below is an example of a method that does not return a value (procedure).
void printText (string sName)
{
Console.WriteLine($"Hello {sName}");
}
C# - Calling a Method
The following are the examples of calling a method in C#:
int a = 5;
int b = 9;
int t;
t = sumValues(a, b);
printText("World");
Here is the complete example for methods. In the following C# program, two methods are created in the class Program; one is sumValues a function type method which returns the total of the two numbers given in the parameters and the second is the printText, a procedure type method which prints the Hello followed by the name given in the parameter.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CreatingMethods
{
class Program
{
int sumValues(int aNumber, int bNumber)
{
int nResult;
nResult = aNumber + bNumber;
return nResult;
}
void printText (string sName)
{
Console.WriteLine($"Hello {sName}!");
}
static void Main(string[] args)
{
// calling method sumValues
Program myMethods = new Program();
int nTotal = myMethods.sumValues(5, 9);
Console.WriteLine("The total of 5 + 9 is " + nTotal);
// calling method printText
myMethods.printText("World");
}
}
}
Output
The total of 5 + 9 is 14
Hello World!
Press any key to continue . . .