.Net Framework
C# Tutorial
C# Control Statement
C# Arrays
C# Object Class
C# Properties
C# Inheritance
C# Polymorphism
C# Abstraction
C# Strings
C# Exception Handling
C# File IO
C# Collections
C# Generics
C# Delegates
C# Reflection
Anonymous Function
C# Multithreading
C# Synchronization
C# Web Service
C# Misc
C# New Features
C# Programs
C# Interview Questions
ADO.NET Tutorial
ASP.NET Tutorial
C# Call By Reference
C# Call By Reference
C# provides a ref keyword to pass argument as reference-type. It passes reference of arguments to the function rather than copy of original value. The changes in passed values are permanent and modify the original variable value.
C# Call By Reference Example
using System;
namespace CallByReference
{
class Program
{
// User defined function
public void Show(ref int val)
{
val *= val; // Manipulating value
Console.WriteLine("Value inside the show function "+val);
// No return statement
}
// Main function, execution entry point of the program
static void Main(string[] args)
{
int val = 50;
Program program = new Program(); // Creating Object
Console.WriteLine("Value before calling the function "+val);
program.Show(ref val); // Calling Function by passing reference
Console.WriteLine("Value after calling the function " + val);
}
}
}
Output:
Value before calling the function 50
Value inside the show function 2500
Value after calling the function 2500