C# - Methods
A method also known as function is a block of statements which executes only when it is called somewhere in the program. A method provides re-usability of same code for different inputs, hence saves time and resources. C# has many built-in methods and one of the common method is Console.WriteLine() which is used to print the output in a new line. Users can also create their own method which is termed as user-defined methods.
Create method
In C#, a method must be declared in a class. Creating a method starts with modifier followed by return type of method. After that, it is followed by method's name and parenthesis containing method's parameter(s), if it has any. At last, it contains block of statements which is also called body of the method.
Syntax
//Defining method modifier return_type method_name(parameters) { statements; }
modifier: It defines the access type of the method and it is optional to use. There are four access modifiers (public, protected, internal, private) which defines the six accessibility levels as follows:
- public
- protected
- internal
- protected internal
- private
- private protected
The following table shows the access to members permitted by each level:
Accessibility Level | public | protected | internal | protected internal | private | private protected |
---|---|---|---|---|---|---|
Entire Program | Y | N | N | N | N | N |
Containing Class | Y | Y | Y | Y | Y | Y |
Current Assembly | Y | N | Y | Y | N | N |
Derived Types | Y | Y | N | Y | N | N |
Derived Types within current Assembly | Y | Y | Y | Y | N | Y |
return_type: A method can return value(s). The return_type is the data type of the value the method returns. If a method does not return anything in that case void is used as return_type.
Example: A method with no parameter
In the example below, a method HelloWorld is created to print Hello World!. It requires no parameters to execute and has no return type, hence void keyword is used.
static tells the program that the HelloWorld belongs to the MyClass and not an object of the MyClass.
using System; class MyProgram { static void HelloWorld() { Console.WriteLine("Hello World!"); } static void Main(string[] args) { HelloWorld(); } }
The output of the above code will be:
Hello World!
Call a method
After defining the method, it can be called anywhere in the program with it's name followed by parenthesis containing method's parameter(s), if it has any and semicolon ;. In the above example, the method is called inside main() method using the following statement:
HelloWorld();
Parameter
A parameter (or also known as argument) is a variable which is used to pass information inside a method. In above example, the method does not has any parameter. But a user can create a method with single or multiple parameters. Value of a parameter can be further used by the method to achieve desired result.
Example: A method with parameters
In the example below, the method called Mymethod is created which requires two integer numbers as parameters and print sum of two numbers in desired style. Please note that, the method returns nothing hence void is used as return type.
using System; class MyProgram { static void MyMethod(int a, int b) { int sum; sum = a + b; Console.WriteLine("Sum of "+ a +" and "+ b +" is: "+ sum); } static void Main(string[] args) { MyMethod(15,10); } }
The output of the above code will be:
Sum of 15 and 10 is: 25
Return Values
A method can be used to return values. To achieve this, user must have to define return type in definition and declaration of the method. In the example below, the return type is int.
using System; class MyProgram { static int MyMethod(int a, int b) { return a + b; } static void Main(string[] args) { int x = 15, y = 10, sum; sum = MyMethod(x, y); Console.WriteLine("Sum of "+ x +" and "+ y +" is: "+ sum); } }
The output of the above code will be:
Sum of 15 and 10 is: 25
Default Parameter Value
Default value can be assigned to a parameter at the time of creating method. When the method is called without parameter then it uses the default value.
Example:
In the example below,the MyFunction function is used to add two, three and four integer numbers. The function uses the default value of a given parameter when the function is called without it. The default value of a parameter is set while defining the function.
using System; class MyProgram { static int MyMethod(int p, int q, int r=0, int s=0) { return p+q+r+s; } static void Main(string[] args) { int a = 15, b = 10, c = 5, d = 1; Console.WriteLine(MyMethod(a,b)); Console.WriteLine(MyMethod(a,b,c)); Console.WriteLine(MyMethod(a,b,c,d)); } }
The output of the above code will be:
25 30 31
Method with Named Argument
In C#, the arguments can be passed in key: value pairs.
Example:
In the example below, arguments can be passed in key: value pairs and hence order of arguments does not matter while using this method.
using System; class MyProgram { static int MyMethod(int p, int q) { return p/q; } static void Main(string[] args) { int a = 10, b = 2; Console.WriteLine(MyMethod(q:b, p:a)); } }
The output of the above code will be:
5