
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# - Passing Parameters by Output
C# out Parameter
A return statement can be used for returning only one value from a function. However, using out parameters, you can return two values from a function. Out parameters are similar to reference parameters, except that they transfer data out of the method rather than into it.
Syntax
Below is the syntax to use out parameters:
void MethodName(out DataType parameter1, out DataType parameter2)
Here is the syntax for calling a method with an out parameter:
MethodName(out variable1, out variable2);
Returning Single Value Using Output Parameters
The output parameters allow methods to return values through parameters without requiring an initial assignment.
Example
The following example demonstrates how a single value is returned from a method using the out parameter:
using System; namespace CalculatorApplication { class NumberManipulator { public void getValue(out int x) { int temp = 5; x = temp; // Assigning a value inside the method } static void Main(string[] args) { NumberManipulator n = new NumberManipulator(); /* Local variable definition */ int a = 100; Console.WriteLine("Before method call, value of a: {0}", a); /* Calling a function to get the value */ n.getValue(out a); Console.WriteLine("After method call, value of a: {0}", a); Console.ReadLine(); } } }
When the above code is compiled and executed, it produces the following result −
Before method call, value of a: 100 After method call, value of a: 5
Returning Multiple Values Using Output Parameters
The output parameters are particularly useful when you need to return multiple values from a method. The variable supplied for the output parameter does not need an initial value.
Example
The following example demonstrates how multiple values can be returned from a method using the out parameters:
using System; namespace CalculatorApplication { class NumberManipulator { public void getValues(out int x, out int y) { Console.WriteLine("Enter the first value: "); x = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the second value: "); y = Convert.ToInt32(Console.ReadLine()); } static void Main(string[] args) { NumberManipulator n = new NumberManipulator(); /* Local variable definition */ int a, b; /* Calling a function to get the values */ n.getValues(out a, out b); Console.WriteLine("After method call, value of a: {0}", a); Console.WriteLine("After method call, value of b: {0}", b); Console.ReadLine(); } } }
When the above code is compiled and executed, it produces the following result −
Enter the first value: 7 Enter the second value: 8 After method call, value of a: 7 After method call, value of b: 8