

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do we pass parameters by reference in a C# method?
A reference parameter is a reference to a memory location of a variable. When you pass parameters by reference, unlike value parameters, a new storage location is not created for these parameters.
The reference parameters represent the same memory location as the actual parameters that are supplied to the method.
The following is an example displaying how to pass parameters by reference. Declare the reference parameters using the ref keyword.
Example
using System; namespace CalculatorApplication { class NumberManipulator { public void swap(ref int x, ref int y) { int temp; temp = x; /* save the value of x */ x = y; /* put y into x */ y = temp; /* put temp into y */ } static void Main(string[] args) { NumberManipulator n = new NumberManipulator(); /* local variable definition */ int a = 100; int b = 200; Console.WriteLine("Before swap, value of a : {0}", a); Console.WriteLine("Before swap, value of b : {0}", b); /* calling a function to swap the values */ n.swap(ref a, ref b); Console.WriteLine("After swap, value of a : {0}", a); Console.WriteLine("After swap, value of b : {0}", b); Console.ReadLine(); } } }
Output
Before swap, value of a : 100 Before swap, value of b : 200 After swap, value of a : 200 After swap, value of b : 100
- Related Questions & Answers
- How do we pass parameters by value in a Java method?
- How do we pass parameters by value in a C# method?
- Why do we pass a Pointer by Reference in C++?
- How to pass reference parameters PHP?
- How do you pass objects by reference in PHP 5?
- Pass by reference vs Pass by Value in java
- What is the difference between pass by value and reference parameters in C#?
- Is java pass by reference or pass by value?
- Is JavaScript a pass-by-reference or pass-by-value language?
- Describe pass by value and pass by reference in JavaScript?
- How to pass parameters to a method in C#?
- How do we pass an array in a method in C#?
- Differences between pass by value and pass by reference in C++
- What is Pass By Reference and Pass By Value in PHP?
- How to pass arguments by reference in a Python function?
Advertisements