C#.NET Tutorialprovides basic and advanced concepts of C# for beginners and professionals.

Type of Parameters in C#.Net

Back to: C#.NET Tutorial

In C#, methods can receive parameters in several ways, each affecting how data is passed and modified. Here’s a breakdown of the different types of parameter passing in C#:

1. Value Type Parameters

When you pass a parameter by value, a copy of the actual value is made. Changes to the parameter inside the method do not affect the original variable.

Example:

void ModifyValue(int number)
{
number = 10; // This does not affect the original value
}
int originalValue = 5;
ModifyValue(originalValue);
Console.WriteLine(originalValue);
// Outputs: 5

2. Reference Type Parameters

When you pass a reference type (like classes), the reference (pointer) to the actual object is passed. Modifying the object via the reference will affect the original object.

Example:

class Person
{
public string Name { get; set; }
}
void ModifyPerson(Person person)
{
 person.Name = "Alice";
// This modifies the original object
 }
Person originalPerson = new Person
{
Name = "Bob"
};
ModifyPerson(originalPerson);
Console.WriteLine(originalPerson.Name);
// Outputs: Alice [access_modifier]
return_type MethodName(parameter_list)
{
// Method body // Code to execute
}

 

3. ref Parameters

Using the ref keyword allows you to pass a variable by reference, meaning that any changes made to the parameter inside the method will affect the original variable. The variable must be initialized before being passed.

Example:

void ModifyRef(ref int number)
{
number = 10; // This affects the original value
}
 int originalValue = 5;
 ModifyRef(ref originalValue);
Console.WriteLine(originalValue);
// Outputs: 10

4. out Parameters

The out keyword is similar to ref, but it is specifically used when you want to return multiple values from a method. The variable does not need to be initialized before being passed, but it must be assigned a value before the method returns.

Example:

void GetValues(out int a, out int b)
{
a = 5; b = 10; // Must be assigned before returning
}
GetValues(out int x, out int y);
Console.WriteLine(x);
// Outputs: 5 Console.WriteLine(y);
// Outputs: 10

5. params Keyword

The params keyword allows you to pass a variable number of arguments to a method. This is useful for methods that need to handle an arbitrary number of parameters.

Example:

void PrintNumbers(params int[] numbers)
{
foreach (var number in numbers)
{
Console.WriteLine(number);
}
}
PrintNumbers(1, 2, 3);
// Outputs: 1, 2, 3
PrintNumbers(4, 5);
// Outputs: 4, 5

Summary

  • By Value: Copies value; changes do not affect the original.
  • By Reference: Reference to the object; changes affect the original.
  • ref: Pass by reference; changes affect the original; must be initialized.
  • out: Pass by reference; must be assigned in the method; used for returning multiple values.
  • params: Variable number of arguments; allows flexibility in method calls.
Scroll to Top