Difference Between Out parameters and REF parameters in C#

In C#, both out and ref are used to pass arguments to methods by reference rather than by value. However, they have different behaviors:

ref Parameter:

The ref keyword is used to indicate that a parameter is passed by reference.

When using ref, the variable must be initialized before it is passed to the method. This is because the method may read and modify the value of the variable, and it expects the variable to already have a value.

Changes made to the parameter inside the method are reflected in the calling method's variable.

  • Its Two way transmission

Out Parameter:

The out keyword is used to indicate that a parameter is passed by reference, but it does not require the variable to be initialized before it is passed to the method. Instead, it guarantees that the method will assign a value to the parameter before it returns.

With out, the method is not required to read the initial value of the parameter; it can assign a new value to it.

Changes made to the parameter inside the method are reflected in the calling method's variable.

  • Its one way Transmission

Normal Paramater:

 ref Parameter:

out Parameter:





Comments

Popular posts from this blog

Basic Plugin Code in D365 using C#

CURD (Create, Update, Retrieve and Delete) Operation in D365 using Power Shell Script

Meta Data Using WebApiRequest