Table of contents
1.
Introduction
2.
What are C# Method Parameters?
3.
More about Parameters and their Syntax
4.
Types of Method Parameters
4.1.
1. Named Parameters
4.2.
2. Ref Parameters
4.3.
3. Out Parameters
4.4.
4. Default or Optional Parameters
4.5.
5. Dynamic Parameters
4.6.
6. Value Parameters
4.7.
7. Params
5.
Frequently Asked Questions
5.1.
What are C# Method Parameters?
5.2.
Name different types of method parameters.
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

C# Method Parameters

Author Rushali Patnaik
3 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

To save excessive use of memory and time and provide better readability to a code, there are methods in the C# language that allows the user to reuse the same code. Parameters are specific input values required by the methods to complete their tasks. In this article, we will learn about the method parameters of the C# language. Let us dive deeper into the topic.

What are C# Method Parameters?

Methods are defined as the code blocks or statements in a program that gives the user the ability to reuse the same code, saving time, excessive memory use, and, more importantly, providing better code readability. A method is a collection of statements that perform some specific task and may or may not return the result to the caller function.

In certain situations, when the user wants to execute a method, but that method requires some valuable inputs to execute and complete its tasks. These input values are known as Parameters in computer language terms.

More about Parameters and their Syntax

Parameters are used to forward and collect data from methods. The keywords associated with parameter and methods are as follow:

  • Method Name: It uniquely identifies a method.
  • Return Type: Non-void methods return value to the calling function. The return type is the data type of the value that the method returns. 
  • Access Specifier: It determines the visibility of the method from another class. 
  • Parameter List: It refers to the type of parameter, number of parameters passed, etc. 
  • Method Body: It contains the instructions, code logic, etc.

Types of Method Parameters

1. Named Parameters

Named parameters specify the parameter's value according to their names and not their order in the method. It facilitates us not to remember parameters according to their order. This concept introduced in C# 4.0 makes your program more understandable while working with a significant number of parameters in your method. But named parameters always appear after fixed arguments. Providing a fixed argument after a named parameter will make the compiler throw an error.

Let us write a program to illustrate the concept of named parameters. 

using System;
public class Ninja {
public static void add(string s1, string s2) {
     string sum = s1 + s2;
        Console.WriteLine("The final string is: " + sum);
}
static public void Main()
{
     add(s1: "Coding", s2: "Ninjas");          
}
}

Output

The final output is: Coding Ninjas.

2. Ref Parameters

The ref in C#  is a keyword used to pass values by reference. The changes made to the variables passed by reference in the called function reflects in the calling function. The ref parameter doesn’t pass the property, and it is compulsory to initialise the parameter before passing it. 

using System;
class CodingNinja {
public static void Main(){
     string val = "Code";
     CompareValue(ref val);
        Console.WriteLine(val);
}
static void CompareValue(ref string val){
     if (val == "Code") {
            Console.WriteLine("Matched!");
     }
     val = "Ninja";
}
}

Output

The final output is: Ninja.

3. Out Parameters

The out keyword in C# is used to pass an argument as a reference type to the method. It is used when a function returns multiple values to the calling function. The out parameter doesn’t pass the property. Initialization of parameter before passing it to out is not required. 

using System;
class CodingNinja{
static public void Main(){
     int val;
     AddNum(out val);
        Console.WriteLine("The sum of + " the value is: {0}", val);             
}
public static void AddNum(out int val){
        val = 90;
     val += val;
}
}

Output

The final output is: The sum of the value is: 180

4. Default or Optional Parameters

The parameters are not compulsory and are optional. These parameters help to exclude arguments for some parameters, and it is optional to pass all the parameters in the method. All the optional parameters have a default value, and if an argument is not passed to the optional parameter, it uses the default value. They are usually defined at the end of the parameter list. 

using System; 
class Ninja{
static public void detail(string name,  int id,  string dept= "Business"){
     Console.WriteLine("Student name: {0}", name);
        Console.WriteLine("Student ID: {0}", id);
        Console.WriteLine("Job: {0}", dept);
}
static public void Main() {
        detail("Ninja", 0001);
        detail("Sai", 0129, "Software Developer");
}
}

Output

The final output is:
Student name: Ninja
Student ID: 0001
Job: Business
 
Student name: Sai
Student ID: 129
Job: Software Developer

5. Dynamic Parameters

The parameters are passed dynamically, and the compiler doesn’t check the parameter type at compile time. The compiler receives the type at runtime. The dynamic keyword is used to declare a dynamic variable. 

using System;
class CodingNinja{
public static void num_num(dynamic num){
     num*=num;
        Console.WriteLine(num);
}
    static public void Main(){
     num_num(50);
}
}

Output

The final output is: 2500

6. Value Parameters

When values are passed instead of references, and the parameters contain data. The changes made to the value parameter are not reflected on the variable in the calling function.

static void Main(string[] args)  
{  
   int a = 10;  
   int b = 25;  
   int res = Add(a, b);  
}  
public static int Add(int x, int y)  
{  
   return x = y;  
}  

7. Params

When the number of parameters to be passed is unknown or is not a fixed number, the params keyword is used that allows and accepts any number of arguments. The length of params is zero when no argument is passed.

using System;
namespace Examples {
class Geeks {
public static void func(params int[] nums){
     foreach(int x in nums){
          Console.WriteLine(x);
     }
}
static void Main(string[] args){
func(10,20,30,40,50);
}
} }

Output

The final output is:
10
20
30
40
50

Frequently Asked Questions

What are C# Method Parameters?

To save excessive use of memory and time and provide better readability to a code, there are methods in the C# language that allows the user to reuse the same code. Parameters are specific input values required by the methods to complete their tasks.

Name different types of method parameters.

The different method parameters are named parameters, ref parameters, out parameters, params, etc.

Also Read About - singleton design pattern in c#

Also see - Difference between argument and parameter

Conclusion

In this article, we have extensively discussed  the C# language's method hiding concept Having gone through this article, I am sure you must be excited to read similar blogs. Coding Ninjas has got you covered. Here are some similar blogs to redirect: C# Method OverridingMethods in C#Introduction to C#C# Type Conversion and Properties of C# We hope that this blog has helped you enhance your knowledge, and if you wish to learn more, check out our Coding Ninjas Blog site and visit our Library. Here are some courses provided by Coding Ninjas: Basics of C++ with DSACompetitive Programming and MERN Stack Web Development. Do upvote our blog to help other ninjas grow.

Happy Learning! 

Live masterclass