Skip to main content

C# Methods

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
Methods
Defining Methods in C#
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:
MechanismDescription
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

Popular posts from this blog

C# Nullables

C# provides a special data types, the  nullable  types, to which you can assign normal range of values as well as null values.  C# Methods For example, you can store any value from -2,147,483,648 to 2,147,483,647 or null in a Nullable<Int32> variable. Similarly, you can assign true, false, or null in a Nullable<bool> variable. Syntax for declaring a  nullable  type is as follows: < data_type> ? <variable_name> = null; The following example demonstrates use of nullable data types: using System ; namespace CalculatorApplication { class NullablesAtShow { static void Main ( string [] args ) { int ? num1 = null ; int ? num2 = 45 ; double ? num3 = new double ?(); double ? num4 = 3.14157 ; bool ? boolval = new bool ?(); // display the values Console . WriteLine ( "Nullables at Show: {0}, {1}, {2}, {3}" , ...

C# - Constants and Literals

The constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are also enumeration constants as well. The constants are treated just like regular variables except that their values cannot be modified after their definition.  More Info:  C Sharp (programming language) Integer Literals An integer literal can be a decimal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, and there is no prefix id for decimal. An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in any order. Here are some examples of integer literals: 212 /* Legal */ 215u /* Legal */ 0xFeeL /* Legal */ Following a...