×

Ref and Out in C#

 Ref in c#

The ref is keyword that describes that any value that passes through the reference. We can use the ref keyword in 4 ways;

  • In a programming code, in that method block or the method call, by that we can pass any argument to that particular method through the reference.
  • To that method we can returns any value to call the method by the reference.
  • In the functional body, we can notice that the reference values are basically stored in the local by the reference that can be altered.
  • In the functional body we can declare the struct function or to declare the ref struct function or to declare the readonly ref struct function.

Let's see a simple program using ref keyword

Example 1:

//C# program to define the ref keyword in the program
// use of ref keyword
using System;    //  objectname


namespace refkeyword
{
class JTP {
static void Main(string[] args)  // main method
{
int x = 19, y = 11;   assigning value x and y
Console.WriteLine("The first Num of x is {0}", x);   // printing the first values
Console.WriteLine(" The first Num of y is {0}", y);  // printing y values
Console.WriteLine();
addNum(x);   // adding by ref
Console.WriteLine(" The Number of x after addition of modification"+
" operation is {0}", x);
subtractNum(ref y);  // subtraction by ref
Console.WriteLine("The Number of y after subtraction of modification "+
"operation is {0}", y);
}
public static void addNum(int x)  // showing add values by ref
{
x += 19;
}
public static void subtractNum(ref int y) // showing subtracting values by ref
{
y -= 11;
}
}
}

Output:

Ref and Out in C#

Out in c#

The out is one of the keywords in C#. This is also like the ref keyword. Out keyword is a keyword that describes that any value that passes through the reference type. This is used for when a method that returns the more than one values (I.e. Multiple values)

The following ways that are used for the Out keywords as:

  • This is same as the ref keyword. The only difference between these both keywords are that ref keyword initialize the values before it throws to the method. Whereas in the out keyword does not initialize the values before it throws the variable into the method.
  • It is as same as the keyword that does not allow the method to change the values but the ref keyword allows the method to change.
  • By using the out keyword, we must notice that method callings and the method definitions are defined the out keyword explicit.
  • We can have more than one out parameters in the method which we allow to change.
  • The out parameter does not allow the iterator methods and asynchronous methods.

Let us see a simple program using the out-keyword

Example 2:

// C# program that defines the out keyword
using System;
class JTP  // object name
{
static public void Main()  // main method
{
int a;  // initializing a value
Addition(out a); // passing a value to the out parameter
Console.WriteLine("The value after performing addition operation is: {0}", a);
} // printing a values
public static void Addition(out int a) // the out parameter is in method which passes the method
{
a = 10;
a += a;
}
}

Output:

Ref and Out in C#

Let's see another program using multiple out parameters:

Example 3:

// C# program to define the multiple out parameters
using System;
class JTP
{
static public void Main() // main method
{
		int x, y; // initializing x, y values without assigning the vales to it.
Addition(out x, out y); // pass multiple variables to the method Console.WriteLine("The value after performing the addition operation is: {0}", x);
Console.WriteLine("The value after performing the addition operation is: {0}", y);
} // printing x and y values
	public static void Addition(out int a, out int b)
{
a = 10;
b = 20;
a += a;
b += b;
}
}

Output:

Ref and Out in C#

Difference between Ref and Out in C#

Let us see the difference between ref and out in c#

ref keywordout keyword
We have to initialize the parameters or the arguments before it passes through the ref by methodIs not must necessary that the parameters before it passes through the out by method
It is not important that the values of the arguments or the parameters before returns the calling function or method.It is important that the values of the arguments or the parameters before returns the calling function or method.
The moving of the value from one method to another through the ref parameter that is important when calling the method to change the value of that parameter passed.The initializing of parameters by the out parameter that is useful for the method which returns the multiple values.
The ref keyword that is useful for the data which passes in bi-directional.The out keyword is useful for the data which passes in the unidirectional.

Related Topics

C# Multilevel Inheritance

In Multilevel inheritance, the class inheriting its parent class is further inherited by another class and so on. This type of inheritance is transitive that’s why the last derived class...

2 minutes read.

C# Destructor

Destructors works in the opposite way. They basically destruct the objects. We can’t send parameter in a destructor. We also can’t apply any modifier to the destructor. Likewise constructor, destructor...

1 minute read.

C# While Loop

while loop iterates a part of the code until the given condition fails. This is an entry controlled loop. While loop is recommended to use if the condition depends on...

4 minutes read.

C# enum

Enum in C# is also known as enumerations. It basically refers to a named set of constants such as months, days, seasons, etc. Enum improves type safety. They contain fixed set...

3 minutes read.

C# Design Patterns

What are the Design Patterns? Design patterns are the reusable solutions to the problems that developers facing while solving any problem related to software development.Design patterns are the templates provided in...

6 minutes read.

C# Operators

Operator is a symbol that is responsible for the operations. There can be variety of operators for example arithmetic, logical, bitwise and many more. There are following type of operators used...

1 minute read.

C# Constructors

In C#, Constructor is the special method with no return type. It is invoked automatically at the time of object creation. Its name must be the same as the class...

2 minutes read.

C# Call by reference

In C#, we can use ref keyword to pass the reference to the original value instead of the copy of the original value. the changes that are made in the...

2 minutes read.

Boxing and Unboxing in C#

The boxing and unboxing are the terms that refers to the data types in the c# language. The c# language is the frontend language for developing applications. We know that the...

3 minutes read.

C# Jagged Arrays

C# provides the concept of jagged array which means array of arrays. The elements of jagged array are also arrays. The element size of jagged array can vary.C# Jagged Arrays...

3 minutes read.

Dictionary in C#

C #: We can also pronounce C # programming language as "C-Sharp programming language. It is a Microsoft-developed object-oriented programming language that utilizes the.NET Framework. C # is related to other widely...

3 minutes read.

C# Inheritance

In C#, Inheritance is a mechanism by which one object acquires all the properties of another object automatically. The object which is inheriting the other object is called chilled object...

4 minutes read.

C# Array Class

In C#, Array class deals with all array related operations. It provides methods for various purposes such as for sorting, searching, minimum, maximum, and many more. This class works as...

5 minutes read.

C# Base

C# provides a base keyword to access base class properties in the chilled class. C# Base is almost similar to the super in java. It can only be used inside...

3 minutes read.

C# Method Overriding

Method overriding is the mechanism where derived class defines the same method as parent class with more functionality in the chilled class. It provides runtime polymorphism in C#. It provides...

1 minute read.

C# Params

There are cases when we don’t know the actual number of parameters in the function call. Well, in C# we can pass any number of parameters in the function call...

3 minutes read.

C# Sealed

C# provides sealed keyword to apply restrictions on class and methods. If a class is defined as sealed then it can’t be inherited. If a method is defined as sealed...

3 minutes read.

C# Member overloading

If we define two members in a class with the same name then it's called member overloading. Advantage of member overloading is the thing that we don't need to give...

4 minutes read.

C# Namespace

C# Provides Concept of namespacing to organize your classes in a good manner. It makes the application easy to handle. It is like packaging of java. The System is a namespace...

1 minute read.

C# Interface

Interface can be defined as a blueprint of the class. It can only have abstract methods. It has to be implemented by a class or struct. It is mainly used...

1 minute read.