Article

Method Parameter


Method Parameter

Parameters are like variables that is pass into method. as your requirement as many parameter you can add into method separated by comma. They are identify after method name and in the bracket.The information can be given to method as a parameter.

Example


using System;

namespace MyCode
{
  class Program
  {
    static void MethodName(string name) 
    {
      Console.WriteLine(name + " lastname ");
    }

    static void Main(string[] args)
    {
      MethodName("max");
     MethodName("roy");
     MethodName("Alexa");
    }  
  }
}

OUTPUT

max lastname 
roy lastname 
Alexa lastname 

There are following types of parameters in C#

1. Named Parameters

Named parameter is used to specify parameter as a name into method.The advantage is not remeber order of the parameter in the method. The right parameter value based on their names will be matched to the right variable.The method definition parameter names must match with the parameter names.


using System;

namespace MyCode
{
  class Program
  {
    static void MethodName(string student1, string student2, string student3)
    {
      Console.WriteLine("The intelligent student is: " + student2);
    }

    static void Main(string[] args)
    {
      MethodName(student1: "Max", student2: "Roy", student3: "Alexa");
    }
  }
}



OUTPUT

The intelligent student is: Roy

2. Default Parameters

Default parameters are not neccessary parameters,they are optional. it is not compulsory all the parameters to pass into method. if we do not pass any value to the default parameters then it takes its optional value.


using System;

namespace MyCode
{
  class Program
  {
    static void MethodName(string CourseName = "C#")
    {
      Console.WriteLine(CourseName);
    }

    static void Main(string[] args)
    {
      MethodName("Dot Net");
      MethodName("Java");
      MethodName();
      MethodName("PHP");
    }
  }
}

OUTPUT

Dot Net
Java
C#
PHP

3. Ref Parameters

The ref is a keyword in C# where it is compulsory that the parameters should initialze before it pass to ref. reference parameter is used for passing the value types by reference.The reference type does not pass the value.value should initialze before pass to reference otherwise it throw an exception.


using System;

namespace MyCode
{
  class Program
  {
    static void Main()
    {
       //  value assign
        string val = "Apple";
  
        // pass to a reference parameter
        MatchValue(ref val);
  
        // given value display
        Console.WriteLine(val);
    }

    static void MatchValue(ref string val1)
    {
       // Match the value
        if (val1 == "Apple") 
        {
            Console.WriteLine("Compared!");
        }
  
        //  new value assign
        val1 = "Mango";
    }
    }
  }


OUTPUT

Compared!
Mango

4. Out Parameters

The Out is a keyword in C# where it is not compulsory that the parameters should intialize before it pass to out.only declare the variable before pass to out. The out parameter will be assign in the calling method before any exit. The ref parameters may be assign inside the method where it is called that is not compulsory.


using System;

namespace MyCode
{
  class Program
  {
    static void Main()
    {
      int a; 
      int b;
      AddNumber(out a, out b);
      
    }

    static void AddNumber(out int x, out int y)
    {
       x=2;
       y=3; 
       Console.WriteLine("The value of X is : " + x);
         
       Console.WriteLine("The value of y is : " + y);
          
    }
    }
  }

OUTPUT

The value of X is : 2
The value of y is : 3