Method Parameter | Code2night.com
Code2night
  • Home
  • Blogs
  • Guest Posts
  • Tutorial
  • Post Blog
  • Register
  • Login
  1. Home
  2. Blogpost

Method Parameter

Date- Dec 09,2023

1306

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

Comments

Tags

LinkedinLogin
LinkedinProfile
GetLinkedinProfile
C#
Aspnet
MVC
Linkedin
ITextSharp
Export to Pdf
AspNet Core
AspNet
View to Pdf in Aspnet
Model Validation In ASPNET Core MVC 60
Model Validation
Model Validation In ASPNET Core MVC
Model Validation In ASPNET
Image Compression in AspNet
Compress Image in c#
AspNet MVC
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1210
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook

Welcome To Code2night, A common place for sharing your programming knowledge,Blogs and Videos

  • Panipat
  • info@Code2night.com

Links

  • Home
  • Blogs
  • Tutorial
  • Post Blog

Popular Tags

Copyright © 2025 by Code2night. All Rights Reserved

  • Home
  • Blog
  • Login
  • SignUp
  • Contact
  • Terms & Conditions
  • Refund Policy
  • About Us
  • Privacy Policy
  • Json Beautifier
  • Guest Posts