What is the difference between passing parameters by output and by reference in C #

I am making an effort with C # methods. There are three ways to pass parameters to C # methods.

Value parameters . This method copies the actual value of the argument to the formal parameter of the function. In this case, changes made to the parameter inside the function do not affect the argument.

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

Outputs : This method helps to return more than one value.

I figured out the above types of pass parameters with below example 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 output:

Before replacement, the value of a: 100

After swap, the value of a: 5

With these codes, I could figure out how to pass parameters to methods by reference. And then I'll go over the below code to understand passing parameters to methods on exit.

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 output:

Before replacement, the value of a: 100

After swap, the value of a: 5

These code examples do the same thing in different ways. And I cannot figure out the difference between the two approaches. (Passing parameters to the method by pins and by reference). The two output examples are the same. What is this slight difference?

+3


source to share


5 answers


The input and output parameters are very similar. The only difference is that the parameters ref

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 look at the keywords ref

and out

. For example, if you cannot overload methods where the only difference is keywords ref

and out

.



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

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

      

+1


source


Output parameters do not need to be initialized prior to calling the method, as is the case for reference parameters.

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

      



In addition, the Output parameter must be set or changed within the method, which is not needed for the reference parameters.

+7


source


With Reference parameters, a value must be assigned before the method can use it. The X and Y in your examples should have been declared outside the method. egint x = 100; int y =200;

With Output parameters, your parameters do not need to be assigned a value before you can use them. X and Y can be declared in your example with no initial values โ€‹โ€‹assigned to them. eg,int x; int y;

+3


source


The output parameter must have a value changed inside the method, otherwise the compiler will generate an error.

The reference parameter may or may not have the reference (referenced objects) modified by the method.

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

+2


source


See What is the difference between the 'ref' and 'out' keywords?

The difference is that for the out parameter, you must set it before exiting the method. This way, even if you didn't set it to a value before calling the method, the compiler knows that it will get the value when the method is called.

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

+1


source







All Articles