Javatpoint Logo
Javatpoint Logo

Difference between Ref and Out keywords in C#

The ref and out keywords in C# are used to pass arguments to a method by reference, allowing the method to modify the original variable that was passed to it.

What is the Ref Keyword?

In C#, "ref" is a keyword. The ref keyword is used to pass arguments by reference to a method or function, rather than passing them by value. This means that changes made to the parameter within the method will affect the original variable passed to the method.

Syntax:

It has the following syntax:

  • public: The term "public" indicates the ability to access the method outside the class.
  • static: When a method gets assigned as "static", it indicates that it is part of the class and not specific instances.
  • Return Type: The term "void" indicates that the method used returned nothing.
  • parameter: The 'ref int x' designates a single int-type parameter with the name x. The argument type int is preceded by the ref keyword, which indicates that x is passed by reference. This implies that any changes made to 'x' within the method will have an effect on the variable that was passed to it.

Example:

Let us take an example to illustrate the Ref keyword in C#.

Output:

The Previous value of integer 'q' is:1
The Current value of integer 'q' is:2

Explanation:

In this program, the GetNextItem method concatenates "Next-" with the numeric value passed in, increases the integer, and returns the concatenated string. Using this method and displaying the changes made to the integer variable "q" both within and outside the method because of passing the ref parameter are demonstrated in the Main method.

What is the Out Keyword?

In C#, "out" is a keyword. Arguments are sent by reference using the out keyword. In C#, methods that define output parameters utilize the out keyword. It offers a method to return multiple values. The compiler gets notified that a method is expected to assign a value to a parameter before returning when a parameter is specified using the out keyword. Unlike "ref" parameters, which must be initialized before being passed on to a method, the parameters do not need to be initialized before being supplied, but they still need to be set a value within the method.

Syntax:

It has the following syntax:

  • public: The term "public" indicates the ability to access the method outside the class.
  • static: When a method gets assigned as "static", it indicates that it is part of the class and not specific instances.
  • Return Type: The term "void" indicates that the method used returned nothing.
  • Parameter: 'out int x' defines a single int-type parameter named x. The int parameter is preceded by the out keyword, which indicates x as an output parameter. It indicates that before returning, the method is supposed to assign a value to x.

The out parameter is not like a regular parameter in that it can be passed to the method without first having any values assigned to it. Instead, the method is responsible for initializing it before it completes.

Example:

Let us take an example to illustrate the Out keyword in C#.

Output:

The Previous value of integer j:1
The Current value of integer j:2

Explanation:

In conclusion, the GetNextItem method modifies the value of the out parameter 'j' to 2, and the Main method prints this updated value to demonstrate how 'out' parameters are used in C#.

Difference between Ref and Out keywords:

Difference between Ref and Out keywords in C#

There are many main differences between the Ref and Out Keywords in C#. Some of the main differences are as follows:

Feature 'ref' keyword 'out' keyword
Initialization The variable cannot be passed on to the method until it has been initialized. Initializing the variable before passing it is not necessary.
Method Requirement The variable must be initialized before passing, and the method can read and modify it. Before returning, the method needs to assign the parameter a value.
Pass by Reference Variables are passed by reference. The original variable is affected by changes performed inside the method and outside of it. Additionally, the variable is passed by reference. Must be assigned within the method.
Usage It is used when the method needs to modify the original variable. It is employed when a method needs to return a value via a parameter and does not require the variable to be initialized before calling it.
Typical Use Cases Modifying parameters passed to the method, such as in-place swapping of values or modifying the original value. Multiple values from a method or a success/fail indication.
Data Flow Using the ref keyword may cause the data to flow in both directions. Data is only sent unidirectionally when the out keyword is used.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news