Page Stats
Visitor: 1084
C# User-Defined Method
A method is a group of statements that together perform a task.
In order to use a method in your program do the following:
- Define the Method
- Call the Method
Example 1: Display message "Hello, World" using method

Method Overloading
In Method Overloading, we can create multiple methods with the same name, but with different parameters. The method call will be resolved by the number, type and sequence of parameters passed. In method overloading return type of method doesn’t matter.
Example 2: Input 2 Integer, Decimal, String Value and Sum them using Method Overloading

Exercise on method overloading
- Find Area of Circle and Rectangle.
- Find Area of Circle, Rectangle and Triangle
Call by Value and Call by Reference
When we pass a value into a variable used by the function then any changes made to this variable in the function have no effect on the parameters specified in the function.
If we pass the parameter by reference we have to use ref keyword. The ref keyword on a method parameter causes a method to refer to the same variable that was passed into the method. Any changes made to the parameter in the method will be reflected in that variable when control passes back to the calling method.
Example : Call by Value
class Class1 { public static void MyMethod (int val) { val = val * 2; Console.WriteLine("val doubled={0}",val); } static void Main (string[] args) { int x=5; MyMethod(x); Console.WriteLine("x={0}",x); Console.ReadLine(); } }
Example : Call by Reference
class Class1 { public static void MyMethod (ref int val) { val = val * 2; Console.WriteLine("val doubled={0}",val); } static void Main (string[] args) { int x=5; MyMethod(ref x); Console.WriteLine("x={0}",x); Console.ReadLine(); } }
Out Parameter
We can also specify that a given parameter is an out parameter using the out keyword, which is used in same way as ref keyword. However, there are important differences. It is illegal to use an unassigned variable in a ref parameter however, we can use in out parameter. Declaring an out method is useful when you want a method to return multiple values. A method can have more than one out parameter.
class Class1 { public static intTestOut(out char i) { i = 'b'; return -1; } static void Main(string[] args) { char i; // variable need not be initialized Console.WriteLine(TestOut(out i)); Console.WriteLine(i); } }
Parameter Arrays
C# allows us to specify one special parameter for a method. This is known as parameter array. It is defined using params keyword. The params keyword lets you specify a method parameter that takes an argument where the number of arguments is flexible. No additional parameters are permitted after the params keyword in a method declaration, and only one params keyword is permitted in a method declaration. If two parameters are specified in the function then params should be in last.
Ad
Java tutorial on core java, servlet, jsp, struts, spring, jsf, hibernate, junit, javamail api, quartz schedular, jdbc, generics, sql, log4j, iText, ant, jsoup, jaxb, json, javascript, xml parser, json, jar, war, oracle plsql, angularjs, apache maven, typescript
Optional Parameters
While calling a method we can omit its argument value. That argument is assigned with a value known as default value or optional parameter. C# now supports using optional parameters with methods, constructors, and indexers.
Recursion/Recursive Method
A Method which call itself again and again until the condition is satisfied is known as Recursion Method. In Recursion Method, we cannot use loops only if conditions are applied.
Example: Find Factorial
class Program { static int fac(int n) { if (n == 1) return n; return n * fac(n - 1); } static void Main(string[] args) { int a = fac(6); Console.WriteLine(a); Console.ReadLine(); } }