A strategy is a gathering of articulations that together play out an errand. Each C# program has no less than one class with a strategy named Main.
To utilize a strategy, you have to:
- Characterize the strategy
- Call the strategy
Methods |
When you characterize a strategy, you essentially proclaim the components of its structure. The linguistic structure for characterizing a strategy in C# is as per the following:
<Access Specifier> <Return Type> <Method Name>(Parameter List) { Method Body }
Following are the different components of a technique:
Access Specifier: This decides the perceivability of a variable or a technique from another class.
Return type: A technique may restore an esteem. The arrival sort is the information kind of the esteem the technique returns. On the off chance that the strategy is not restoring any esteems, at that point the arrival sort is void.
Method name: Method name is a one of a kind identifier and it is case delicate. It can't be same as whatever other identifier pronounced in the class.
Parameter list: Enclosed between brackets, the parameters are utilized to pass and get information from a strategy. The parameter list alludes to the sort, request, and number of the parameters of a technique. Parameters are discretionary; that is, a technique may contain no parameters.
Method body: This contains the arrangement of directions expected to finish the required movement.
Example
Following code bit demonstrates a capacity FindMax that takes two whole number esteems and returns the bigger of the two. It has community specifier, so it can be gotten to from outside the class utilizing an occurrence of the class.
class NumberManipulator { public int FindMax(int num1, int num2) { /* local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; return result; } ... }
Calling Methods in C#
You can call a strategy utilizing the name of the technique. The accompanying case shows this:
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(); } } }
When the above code is compiled and executed, it produces the following result:
Max value is : 200
You can likewise call open technique from different classes by utilizing the example of the class. For instance, the technique FindMax has a place with the NumberManipulatorclass, you can call it from another class Test.
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; } } class Test { 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(); } } }
When the above code is compiled and executed, it produces the following result:
Max value is : 200
Recursive Method Call
A technique can call itself. This is known as recursion. Following is a case that figures factorial for a given number utilizing a recursive capacity:
using System; namespace CalculatorApplication { class NumberManipulator { public int factorial(int num) { /* local variable declaration */ int result; if (num == 1) { return 1; } else { result = factorial(num - 1) * num; return result; } } static void Main(string[] args) { NumberManipulator n = new NumberManipulator(); //calling the factorial method Console.WriteLine("Factorial of 6 is : {0}", n.factorial(6)); Console.WriteLine("Factorial of 7 is : {0}", n.factorial(7)); Console.WriteLine("Factorial of 8 is : {0}", n.factorial(8)); Console.ReadLine(); } } }
When the above code is compiled and executed, it produces the following result:
Factorial of 6 is: 720 Factorial of 7 is: 5040 Factorial of 8 is: 40320
Passing Parameters to a Method
When method with parameters is called, you need to pass the parameters to the method. There are three ways that parameters can be passed to a method:
Mechanism | Description |
---|---|
Value parameters
| This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. |
Reference parameters
| This method copies the reference to the memory location of an argument into the formal parameter. This means that changes made to the parameter affect the argument. |
Output parameters
| This method helps in returning more than one value. |
Comments
Post a Comment