In C#, a method is a block of code that performs a specific task and can be reused across the program. Methods may or may not return a value and in many cases, they require inputs to operate on, these inputs are known as parameters.
Parameters define the type, number and order of values a method expects when it is called. The values passed to parameters are called arguments. C# provides several types of method parameters that control how data is passed between the caller and the method.
Types of Method Parameters in C#
- Value Parameters
- Ref Parameters
- Out Parameters
- Default (Optional) Parameters
- Named Parameters
- Params
- Dynamic Parameters
1. Value Parameters
Value parameters pass a copy of the variable’s data to the method. Any changes made inside the method do not affect the original variable.
CSharp
using System;
class Program
{
static void Main()
{
int num = 10;
Increment(num);
Console.WriteLine(num); // 10 (unchanged)
}
static void Increment(int value)
{
value++;
}
}
Changes are local to the method because the argument is passed by value.
2. Ref Parameters
The ref keyword passes arguments by reference, allowing the called method to modify the original variable’s value.
Rules:
- The variable must be initialized before passing to ref.
- Changes made inside the method reflect in the calling scope.
CSharp
using System;
class Program
{
static void Main()
{
string name = "Dog";
Update(ref name);
Console.WriteLine(name); // Cat
}
static void Update(ref string text)
{
if (text == "Dog")
Console.WriteLine("Matched!");
text = "Cat";
}
}
3. Out Parameters
The out keyword is used to pass parameters by reference, mainly to return multiple values from a method.
Rules:
- The variable need not be initialized before passing.
- The method must assign a value before returning.
CSharp
using System;
class Program
{
static void Main()
{
int sum;
Add(out sum);
Console.WriteLine(sum); // 80
}
static void Add(out int result)
{
result = 40;
result += result;
}
}
4. Default (Optional) Parameters
Optional parameters allow you to omit arguments when calling a method. Each optional parameter has a default value, which is used when no argument is provided.
Rules:
- Must be defined after all required parameters.
- Can be omitted at the time of the call.
CSharp
using System;
class Program
{
static void Details(string name, int id, string dept = "Development", string city = "Pune")
{
Console.WriteLine($"Name: {name}, ID: {id}, Department: {dept}, City: {city}");
}
static void Main()
{
Details("Aman", 101);
Details("Riya", 102, "Testing");
Details("Neha", 103, "Design", "Delhi");
}
}
OutputName: Aman, ID: 101, Department: Development, City: Pune
Name: Riya, ID: 102, Department: Testing, City: Pune
Name: Neha, ID: 103, Department: Design, City: Delhi
5. Named Parameters
Named parameters allow you to pass arguments by specifying the parameter names, instead of relying on their order. It was introduced in C# 4.0.
CSharp
using System;
class Program
{
static void AddStrings(string first, string middle, string last)
{
Console.WriteLine(first + middle + last);
}
static void Main()
{
AddStrings(first: "Geeks", last: "Geeks", middle: "for");
}
}
Named parameters must appear after all positional arguments in a method call.
6. Params
The params keyword allows a method to accept a variable number of arguments of the same type.
Rules:
- Only one params parameter is allowed per method.
- Must be the last parameter in the definition.
CSharp
using System;
class Program
{
static int Multiply(params int[] numbers)
{
int result = 1;
foreach (int n in numbers)
result *= n;
return result;
}
static void Main()
{
Console.WriteLine(Multiply(2, 3, 4)); // 24
Console.WriteLine(Multiply()); // 1
}
}
7. Dynamic Parameters
The dynamic keyword (introduced in C# 4.0) allows a parameter whose type is resolved at runtime, not at compile time.
CSharp
using System;
class Program
{
static void Square(dynamic value)
{
Console.WriteLine(value * value);
}
static void Main()
{
Square(10);
Square(4.5);
}
}
Dynamic parameters offer flexibility but should be used carefully as type checking occurs at runtime, which may lead to runtime exceptions.
Which parameter type in C# allows returning multiple values from a method?
In programming, parameters are:
-
Actual values passed into a function
-
Variables defined in a function declaration
-
-
In the function call add(5, 3), what are 5 and 3?
Which of the following statements about ref parameters is TRUE?
-
The variable passed with ref must be initialized before passing.
-
The variable passed with ref cannot be modified inside the method.
-
ref is used to return multiple values from a method.
-
The method must assign a value before returning when using ref.
Which parameter type allows a method to accept a variable number of arguments?
Quiz Completed Successfully
Your Score : 2/5
Accuracy : 0%
Login to View Explanation
1/5
1/5
< Previous
Next >
Explore
Introduction
Fundamentals
Control Statements
OOP Concepts
Methods
Arrays
ArrayList
String
Tuple
Indexers