In the c#, Method is a block that contains a series of statements which only runs when it is called. We can pass different types of data, known as parameters, into a method.
Information can be passed to methods as parameter. Parameters act as variables inside the method
C# contains the given below Method Parameters:
- Named Parameters
- Ref Parameters
- Out Parameters
- Default or Optional Parameters
- Dynamic Parameters
- Value Parameters
- Params
Named Parameters
Using named parameters, we can specify the value of the parameter by parameter name regardless of its ordering in method. This concept is introduced in C# 4.0.
Single Paramerter
using System;
public class Program
{
static void SingleParameter(string singleParam)
{
Console.WriteLine(singleParam + " Is Information Technology");
}
public static void Main(string[] args) { SingleParameter("Find and solve");
} }
Ouptput
Find and solve Is Information Technology
Default Parameter Value
using System;
public class Program
{
static void SingleParameter(string singleParam="Find and solve")
{
Console.WriteLine(singleParam + " Is Information Technology");
}
public static void Main(string[] args)
{ SingleParameter("Microsoft");
}
}
Output
Microsoft Is Information Technology
Multiple Parameters
using System;
public class Program
{
static void MultipleParameter(int a,int b)
{
int sum =a+b;
Console.WriteLine("The sum is {0}",sum);
}
public static void Main(string[] args)
{
MultipleParameter(10,12);
}
}
Output
The sum is 22
Return Type Parameter
using System;
public class Program
{
static int ReturnTypeParameter(int a,int b)
{
return a+b;
}
public static void Main(string[] args)
{
int sum=ReturnTypeParameter(10,12);
Console.WriteLine("The Total Sum is {0}",sum);
}
}
Output
The Total Sum is 22
Ref Parameters
The ref is a keyword in C# which is used for passing the value types by reference. You can say that type of parameters if any changes made in this argument in the method will reflect in that variable when the control return to the calling method. Does not pass property in ref parameter.
using System;
public class Program
{
// Main Method
public static void Main()
{
// Assigning value
string str = "Cat";
// Pass as a reference parameter
CompareValue(ref str);
// Display the given value
Console.WriteLine(str);
}
static void CompareValue(ref string val)
{
// Compare the value
if (val == "Cat")
{
Console.WriteLine("Matched!");
}
// Assigning new value
val = "Dog";
}
}
Output
Matched!
Dog
Out Parameters
An out param is similar to like as ref param but any types of argument passed as out parameter need not to be initialized whereas in case of ref it must be initialized.It is generally used when a method returns multiple values and this type of parameter does not pass the property.
using System;
public class Program
{
public static void OutParameterExample(out int number)
{
number = 14;
number += number;
} static public void Main()
{
int number;
OutParameterExample(out number);
// Display the value of num
Console.WriteLine("The sum of"
+ " the value is: {0}",number);
}
}
Output
The sum of the value is: 28
Default or Optional Parameters
This type of parameters optional parameters which is not compulsory parameters. It helps to exclude arguments for some parameters. Or you can say in optional parameters, this is not compulsory to pass all the parameters in the method. This concept is introduced in C# 4.0.
using System;
public class Program
{ static public void StudentDetails(string fName,int age,string block = "D block")
{
Console.WriteLine("First Name: {0}",fName);
Console.WriteLine("Age: {0}", age); Console.WriteLine("Block: {0}",block);
}
// Main Method
static public void Main()
{
// Calling the detail method
StudentDetails("Rohani",25);
StudentDetails("Sylvia",18, "B block");
}
}
Output
First Name: Rohani
Age: 25
Block: D block
First Name: Sylvia
Age: 18
Block: B block
Dynamic Parameters
Dynamic parameter introduced In C# 4.0. Dynamic parameter means that compiler does not check the type of the dynamic type variable at compile-time, instead of this, the compiler gets the type at the run time .
using System;
public class Program
{
public static void DynamicParameterExample(dynamic param)
{
param *= param;
Console.WriteLine(param);
}
// Main method
static public void Main()
{
// Calling mulval method
DynamicParameterExample(20);
}
}
Output
400
Value Parameters
In the c# value parameters is a normal value parameter in a method or we can say the passing of value types by value.
using System; public class Program
{
public static string ValueParameterExample(string str, string str1)
{
return str + str1;
} static public void Main()
{
string str = "FindAndSolve";
string str1 = "findandsolve";
string result = ValueParameterExample(str, str1);
Console.WriteLine(result);
}
}
Output
FindAndSolvefindandsolve
Params
The ‘param’ keyword can be used to specify a method parameter that takes a variable number of arguments. When you use ‘param’ keyword to pass variable number of arguments we can send any number of arguments or no arguments. But all the arguments passed should be of same type.
using System;
public class Program
{ public static int ParamExample(params int[] num)
{
int result = 1;
// foreach loop
foreach(int i in num)
{
result *= i;
} return result;
}
public static void Main(string[] args)
{
// Calling mulval method
int res = ParamExample(14, 15, 20, 33, 48, 54);
// show result
Console.WriteLine(res);
}
}
Output
359251200