Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
  • Register
  • Login
  1. Home
  2. Blogpost

Method Parameter

Date- Dec 09,2023

3092

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

S
Shubham Saini
Programming author at Code2Night โ€” sharing tutorials on ASP.NET, C#, and more.
View all posts โ†’

Comments

Tags

AspNet
C#
programming
AspNet MVC
c programming
AspNet Core
C
software development
tutorial
MVC
memory management
Paypal
coding
coding best practices
data structures
programming tutorial
tutorials
object oriented programming
Slick Slider
StripeNet
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 | 1760
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
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
By Language
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ยท  Terms
Translate Page