×

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# 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# 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# Tutorial

The C# Tutorial provides basic and advanced knowledge of C# . This will help the beginners as well as professionals in learning just more then enough about C#, A Programming...

3 minutes read.

C# String

String can be defined as an array of characters. In C#, string is an object of System.String which represents sequence of characters. Strings can be concatenated, comparison, getting substring and...

6 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# 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# Variables

A variable is a symbol used to store user’s data. It represents a memory location. Its value can be changed multiple times and can be reused many times.Example: int a; //integer...

1 minute read.

C# Passing array to function

We can pass array to function by using its name only. The formal parameter in function declaration must be of array type. Function accepts arrays to implement any logic on...

2 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.

Switch Statements in C#

In C#, the switch case statement checks one variable for various possible values (cases) and executes the case which is true. Switch case statements are mainly used in menu driven...

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# Properties

In C#, Properties enables class to provide a public way of setting and getting values or fields of the class. Properties provides Encapsulation in the class if we make all...

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# 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# 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# this Keyword

In C#, this is a keyword used to refer current class instance. It is mainly used to distinguish instance variables of the class and formal parameters defined in a constructor...

7 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# Object and classes

As we know, C# is an object oriented programming language, it provides oops concepts which are relevant to real world problems. C# Object: An object is a real world entity. It...

6 minutes read.

C# Abstraction

Abstraction is a mechanism by which we can hide the complexities and show only functionalities. C# provides an abstract keyword to declare a class and method as abstract. In C#,...

2 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.