Introduction
Before understanding the ref and out keywords, it is necessary to understand what value and reference types are, so let us start with reference types.
Reference types are a pointer type that stores a memory address that points to a memory location in heap memory. Here the key point is that a reference type uses both the stack and the heap. That means it stores the address in the heap, and the value is stored in the stack, so it means that without the stack, there is no heap at all.
Example. All user-defined classes.
Value types are those that directly store a value inside it and that use stack memory.
Example. All pre-defined data types. (For example int, float, double, and so on.),
Note. For example, I do like this.
int x = 4;
int x1 = x; // This assigns the value of x (which is 4) to x1.
// Now, if you want to change the value of x to 100, you can do this:
x = 100; // This updates the value of x to 100.
And if we print x1, the output we get is 4 only, so it cannot be changed because it holds the value directly, but if a reference type changes in one type, it must be reflected in another type.
Now let's start with our main topic, the ref, and our keywords.
Ref Keyword in C#
The ref keyword can be used with any data type. The main purpose of the ref keyword is to make any type a reference type; in other words, when we add a ref keyword in a method call, then the C# compiler does not create a new variable, it just creates a copy of that type that only points to that specific variable that we declared and any changes made to that variable will be reflected in that variable. For example, we do it like.
using System;
class Program
{
static void Main()
{
int x = 10;
Show(ref x);
}
static void Show(ref int x)
{
// Method declaration
}
}
Public static show (ref int number) //Here it does not create a new variable, rather it makes a copy of that type so any changes made to it is reflected to the original variable, That's how ref is used in primitive data type.
Example
using System;
class RefDemo
{
public static void Show(ref int number)
{
number = 23;
}
static void Main()
{
int x = 10;
Show(ref x);
Console.WriteLine(x); // This will print 23
}
}
In the preceding, the output is 10 because all value type variables are directly storing a value due to this, here if we print the number we get the output as 23, and for x, we get 10.
But it is changed when we call this method using the ref keyword. That can be as in the following.
using System;
class RefDemo
{
public static void Show(ref int number)
{
number = 23;
}
static void Main()
{
int x = 10;
Show(ref x);
Console.WriteLine(x); // This will print 23
}
}
Here we got the output as 23 because the C# compiler doesn't create a variable. It makes a copy of type x. So the changes made anywhere are reflected to x.
A class or all reference type variables also do that. As we know, in the case of a class type without using ref also point to the same object when we assign one object to a class variable. Consider Case 1.

Mukesh KumarPosted Oct 18, 2015, 4:58 AM
Thanks Santhakumar
Santhakumar MunuswamyPosted Oct 18, 2015, 4:56 AM
Good one
Yaduveer SainiPosted Oct 16, 2015, 5:36 AM
Good Job Sir...keep it up
Banketeshvar NarayanPosted Oct 16, 2015, 3:33 AM
Great job.. Keep it up!
Mukesh KumarPosted Oct 15, 2015, 7:58 AM
Thanks Shakti
Shakti SaxenaPosted Oct 15, 2015, 7:54 AM
Great
Mukesh KumarPosted Oct 9, 2015, 1:51 AM
Thanks Sibeesh
Sibeesh VenuPosted Oct 9, 2015, 1:13 AM
Nice Share
Mukesh KumarPosted Oct 8, 2015, 9:27 AM
Thanks Nilesh Sir
Nilesh JadavPosted Oct 8, 2015, 9:09 AM
Nice one sir
Mukesh KumarPosted Oct 8, 2015, 5:04 AM
Thanks all of you.
Sujeet SumanPosted Oct 8, 2015, 4:55 AM
Good one.............
Harshad PansuriyaPosted Oct 8, 2015, 4:10 AM
Nice one
Rajeesh MenothPosted Oct 8, 2015, 4:01 AM
Nice Share..