简体   繁体   中英

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

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?

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.

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. eg int x = 100; int y =200; 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. eg int x; int y; int x; int y;

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.

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

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){};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM