C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In this tutorial, we will explore method parameters in C#, including the concepts of actual and formal parameters. Understanding method parameters is essential for writing modular and reusable code.
Formal parameters are the variables defined in a method's declaration. These variables act as placeholders for the actual values that will be passed into the method when it is called. The formal parameters are used within the method to perform calculations or other operations based on the provided arguments.
Consider the following example:
public int Add(int a, int b) { return a + b; }
In this example, a
and b
are formal parameters of the Add
method. They represent the two integer values that will be added together when the method is called.
Actual parameters, also known as arguments, are the values that are passed into a method when it is called. The actual parameters are matched with the formal parameters in the method declaration based on their position.
Here's an example of calling the Add
method with actual parameters:
int result = Add(3, 5);
In this case, 3
and 5
are the actual parameters. When the Add
method is called, the value 3
is assigned to the formal parameter a
, and the value 5
is assigned to the formal parameter b
. The method then returns the sum of a
and b
, which is 8
.
C# supports two ways of passing arguments to methods: pass by value and pass by reference.
public void Increment(int number) { number++; }
In this example, the Increment
method does not modify the original value of the input argument because the formal parameter is passed by value.
ref
or out
keyword.public void Increment(ref int number) { number++; }
In this example, the Increment
method modifies the original value of the input argument because the formal parameter is passed by reference.
C# also supports optional and named parameters:
=
operator followed by the default value.public int Add(int a, int b = 0) { return a + b; }
In this example, the b
parameter is optional, and its default value is 0
.
int result = Add(a: 3, b: 5);
In this example, the actual parameters are passed using their parameter names, making the order of the arguments irrelevant.
This tutorial covers method parameters in C#, including the concepts of actual and formal parameters, pass by value and pass by reference, and optional and named parameters.
How to define method parameters in C#
Method parameters in C# are defined within parentheses after the method name. Example:
void PrintMessage(string message) { Console.WriteLine(message); }
Passing parameters in C# methods
void DisplayInfo(string name, int age) { Console.WriteLine($"Name: {name}, Age: {age}"); } // Invoking the method DisplayInfo("John", 25);
C# method parameters examples
void CalculateSum(int a, int b) { int sum = a + b; Console.WriteLine($"Sum: {sum}"); }
Default parameters in C# methods
void PrintInfo(string name, int age = 30) { Console.WriteLine($"Name: {name}, Age: {age}"); }
Optional parameters in C#
Optional parameters allow omitting arguments during method invocation:
void PrintGreeting(string greeting, string name = "Guest") { Console.WriteLine($"{greeting}, {name}!"); }
Named parameters in C#
void DisplayRectangle(int length, int width) { Console.WriteLine($"Length: {length}, Width: {width}"); } // Named parameter usage DisplayRectangle(width: 10, length: 5);
C# method parameters by reference
Passing parameters by reference using ref
or out
:
void IncrementByReference(ref int number) { number++; }
Passing arrays as parameters in C#
void PrintArray(int[] numbers) { foreach (var number in numbers) Console.Write($"{number} "); }
Method parameters and method overloading in C#
Method overloading involves defining multiple methods with the same name but different parameter lists:
void DisplayInfo(string name) { Console.WriteLine($"Name: {name}"); } void DisplayInfo(string name, int age) { Console.WriteLine($"Name: {name}, Age: {age}"); }
C# method parameters and type checking
void DisplayPerson(object person) { if (person is Person) { var realPerson = (Person)person; Console.WriteLine($"Name: {realPerson.Name}"); } else { Console.WriteLine("Invalid person type"); } }
Parameter arrays in C# methods
Using the params
keyword to allow a variable number of arguments:
void PrintNumbers(params int[] numbers) { foreach (var number in numbers) Console.Write($"{number} "); }
Method parameters and scope in C#
Parameters have a scope limited to the method in which they are defined:
void PrintValue(int value) { // value is only accessible within this method Console.WriteLine($"Value: {value}"); }