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# - Variables

A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C# has a specific type, which determines the size and layout of the variable's memory the range of values that can be stored within that memory and the set of operations that can be applied to the variable. The basic value types provided in C# can be categorized as: Type Example Integral types sbyte, byte, short, ushort, int, uint, long, ulong, and char Floating point types float and double Decimal types decimal Boolean types true or false values, as assigned Nullable types Nullable data types C# also allows defining other value types of variable such as  enum  and reference types of variables such as  class , which we will cover in subsequent chapters. Defining Variables Syntax for variable definition in C# is: <data_type> <variable_list> ; Here, data_type must be a valid C# data type including char, int, float, double, or any user-defined da

C# - Environment

In this chapter, we will discuss the tools required for creating C# programming. We have already mentioned that C# is part of .Net framework and is used for writing .Net applications. Therefore, before discussing the available tools for running a C# program, let us understand how C# relates to the .Net framework. The .Net Framework The .Net framework is a revolutionary platform that helps you to write the following types of applications: Windows applications Web applications Web services The .Net framework applications are multi-platform applications. The framework has been designed in such a way that it can be used from any of the following languages: C#, C++, Visual Basic, Jscript, COBOL, etc. All these languages can access the framework as well as communicate with each other. The .Net framework consists of an enormous library of codes used by the client languages such as C#. Following are some of the components of the .Net framework:  More Info:  C Sharp (programming l