
- C# Basic Tutorial
- 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# - Decision Making
- C# - Loops
- 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# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to assign a reference to a variable in C#
To assign reference to a variable, use the ref keyword. 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. Declare the reference parameters using the ref keyword.
Let us see an example −
Here, we are swapping two values using the ref keyword −
Example
using System; namespace Demo { class Program { 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) { Program p = new Program(); /* local variable definition */ int a = 99; int b = 110; 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 */ p.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 : 99 Before swap, value of b : 110 After swap, value of a : 110 After swap, value of b : 99
- Related Articles
- Can we assign a reference to a variable in Python?
- Reference assignment operator in PHP to assign a reference?
- How to assign a PHP variable to JavaScript?
- How can we assign a function to a variable in JavaScript?
- How to assign multiple values to a same variable in Python?
- How to assign multiple values to same variable in C#?\n
- Is it possible to assign a reference to "this" in java?
- How do I assign a dictionary value to a variable in Python?
- What is a reference variable in C++?
- Assign other value to a variable from two possible values in C++
- How to assign the result of a MySQL query into a variable?
- How to assign int value to char variable in Java
- How can we assign a bit value as a number to a user variable?
- What are the differences between a pointer variable and a reference variable in C++?
- In C++ What are the differences between a pointer variable and a reference variable?

Advertisements