What Is The Difference Between Passing Parameters By Output And By Reference In C#

4.5k views Asked by At

I have been making an effort on C# methods. There are three ways that parameters can be passed to a C# methods.

Value parameters : This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.

Reference parameters : This method copies the reference to the memory location of an argument into the formal parameter. This means that changes made to the parameter affect the argument.

Output parameters : This method helps in returning more than one value.

I understood above types of passing parameters with below sample codes.

using System;
namespace PassingParameterByReference
{
   class MethodWithReferenceParameters
   {
      public void swap(ref int x)
      {
        int temp = 5;
        x = temp;
      }
   
      static void Main(string[] args)
      {
         MethodWithReferenceParameters n = new MethodWithReferenceParameters();
         /* local variable definition */
         int a = 100;
         Console.WriteLine("Before swap, value of a : {0}", a);
         /* calling a function to swap the value*/
         n.swap(ref a);
         Console.WriteLine("After swap, value of a : {0}", a);
         Console.ReadLine();

      }
   }
}

When the above code is compiled and executed, it produces the following result:

Before swap, value of a : 100

After swap, value of a : 5

With this codes i could understand to pass parameters to methods by reference. And then i examine below code to understand passing parameters to methods by output.

using System;
namespace PassingParameterByOutput
{
   class MethodWithOutputParameters
   {
      public void swap(out int x)
      {
        int temp = 5;
        x = temp;
      }
   
      static void Main(string[] args)
      {
         MethodWithOutputParameters n = new MethodWithOutputParameters();
         /* local variable definition */
         int a = 100; 
         Console.WriteLine("Before swap, value of a : {0}", a);
         /* calling a function to swap the value */
         n.swap(out a);
         Console.WriteLine("After swap, value of a : {0}", a);
         Console.ReadLine();
       }
   }
}

When the above code is compiled and executed, it produces the following result:

Before swap, value of a : 100

After swap, value of a : 5

These sample codes make same action with different ways. And i can't understand difference between two approaches.( Passing Parameters To Method By Output And By Reference). Two example's output is the same. What is this small difference?

5

There are 5 answers

0
Matt Rowland On BEST ANSWER

Reference and Output parameters are very similar. The only difference is that ref parameters must be initialized.

int myInt = 1;
SomeMethod(ref myInt); //will work
SomeMethod(out myInt); //will work

int myInt;
SomeMethod(ref myInt); //won't work
SomeMethod(out myInt); //will work

The compiler will actually view the ref and out keywords the same. For example if you cannot have overloaded methods where the only difference is the ref and out keywords.

The following methods signatures will be viewed the same by the compiler, so this would not be a valid overload.

private void SomeMethod(ref int Foo){};
private void SomeMethod(out int Foo){};
0
T_D On

Output parameters dont have to be initialized before the method is called like it is the case for reference parameters.

int someNum;
someMethod(out someNum); //works
someMethod(ref someNum); //gives compilation error

Furthermore, the Output parameter needs to be set or changed within the method, which is not necessary for reference parameters.

0
bateloche On

An output parameter needs to have it's value changed inside the method otherwise the compiler will throw an error.

The reference parameter may or may not have it's reference (reference objects) changed by the method.

Also value types (types defined in structs) cannot be passed by reference.

0
n00b On

See this What's the difference between the 'ref' and 'out' keywords?

The difference is that for an out parameter you have to set it before leaving the method. So even if you dont set it to any value before calling the method the compiler knows that it will get a value during the method call.

Appart from technical differences, to have a good readable code, you should use out when the method has more than one output. And use ref when the method just may update the variable

0
Melfodonius Debb On

With "Reference" parameters, the value needs to be assigned before a method can use it. X and Y in your examples would have needed to have been declared outside the method. e.g. int x = 100; int y =200;

With "Output" parameters, the value for your parameters don't have to be assigned a value before you can use them. X and Y could have been declared in your example with no starting values assigned to them. e.g int x; int y;