RE: Difference between 'out' and 'ref' parameters.



You're almost right, "ref" and "out" are similar in that they can both return
a value from a method, but they have slightly different semantics.

You need to initialize a "ref" parameter before passing it to a method, but
you don't have to initialize an "out" parameter before passing it to a method
because the c# compiler knows that the method will assign a value to the
parameter before returning. In fact, the c# compiler will throw a compiler
error if it detects that the method has not assigned a value to an "out"
parameter before returning.

By value example: The output from the code below will be "x = 0"

int x = 0;
add(x)
Console.Write("x = {0}", x);

void add(ref x)
{
x = x + 1
}

"Ref" example: The output from the code below will be "x = 1"

int x = 0;
add(ref x)
Console.Write("x = {0}", x);

void add(ref x)
{
x = x + 1
}

"out" example: The output from the code below will be "x = 1"

int x;
add(out x)
Console.Write("x = {0}", x);

void add(out x)
{
x = x + 1
}

Note: The variable "x" is not initialized prior to the call to "add"

For more info:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfref.asp

"Chris Mayers" wrote:

> 'out' and 'ref' parameters in C#...
>
> Both these can be used to pass parameter values BACK from a Method, but
> obviously they are different techniques.
>
> As I understand it,
>
> 'ref' parameter passes the object into the Method by Reference, ie passes
> the memory location of the original object in the parameter list. The Method
> may change the value of the object and by doing so, will be changing the
> value of the original object.
>
> 'out' parameter passes an object BY VALUE back into the object in the
> calling parameters.
>
> 1) Am I right?
> 2) Is it true to say that you are usually better to use 'out' unless the
> original value of the parameter being passed in effects its value when you
> pass it back. Or if your method needs to return more than one calculated
> result.
>
> eg
>
> private void GetPosition(string lorryId, out int xLocation, out int
> yLocation)
>
> Hope this makes sense, I'm very tired...
>
> Thanks,
>
> Chris.
>
>
>
.



Relevant Pages

  • Re: WMEncoder
    ... public Guid majortype; ... int GetLength( ... void GetStreamNumber( ... ref int pcchStreamName); ...
    (microsoft.public.windowsmedia.sdk)
  • Re: .NET and IE Favourites
    ... public enum FMTID_InternetSite: int ... , ref propertyStorage); ... public static void PrintUrlMetadata(IPropertyStorage ...
    (microsoft.public.dotnet.framework.interop)
  • ExecutionEngineException trying to override IInternetSecurityManager
    ... public void QueryService(ref Guid guidService, ref Guid riid, out object ... void SaveObject; ... int GetSecuritySite; ... out UInt32 pdwZone, UInt32 dwFlags); ...
    (microsoft.public.dotnet.languages.csharp)
  • .NET and IE Favourites
    ... public enum STGM: int ... , ref propertyStorage); ... ref System.Guid rfmtid, ... public void FromObject ...
    (microsoft.public.dotnet.framework.interop)
  • Re: ref Class and a value by reference
    ... |> % is for passing value types by ref. ... public ref class MyClass ... void TestFunc2 ...
    (microsoft.public.dotnet.languages.csharp)