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 Overriding, Methods 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 DSA, Competitive Programming and MERN Stack Web Development. Do upvote our blog to help other ninjas grow.
Happy Learning!