concept ref parameter in category c#

appears as: ref parameters, ref parameter, A ref parameter
C# in Depth, Fourth Edition

This is an excerpt from Manning's book C# in Depth, Fourth Edition.

C# 3 introduced an extra feature to partial types called partial methods. These are methods declared without a body in one part and then optionally implemented in another part. Partial methods are implicitly private and must be void with no out parameters. (It’s fine to use ref parameters.) At compile time, only partial methods that have implementations are retained; if a partial method hasn’t been implemented, all calls to it are removed. This sounds odd, but it allows generated code to provide optional hooks for manually written code to add extra behavior. It turns out to be useful indeed. The following listing provides an example with two partial methods, one of which is implemented and one of which isn’t.

Listing 2.23. Two partial methods—one implemented, one not
partial class PartialMethodsDemo
{
    public PartialMethodsDemo()
    {
        OnConstruction();                            #1
    }

    public override string ToString()
    {
        string ret = "Original return value";
        CustomizeToString(ref ret);                  #2
        return ret;
    }

    partial void OnConstruction();                   #3
    partial void CustomizeToString(ref string text); #3
}

partial class PartialMethodsDemo
{
    partial void CustomizeToString(ref string text)  #4
    {
        text += " - customized!";
    }
}

In listing 2.23, the first part would most likely be generated code, thereby allowing for additional behavior on construction and when obtaining a string representation of the object. The second part corresponds to manually written code that doesn’t need to customize construction but does want to change the string representation returned by ToString(). Even though the CustomizeToString method can’t return a value directly, it can effectively pass information back to its caller with a ref parameter.

Figure 13.4. A ref parameter uses the same piece of paper rather than creating a new one with a copy of the value.
C# in Depth

This is an excerpt from Manning's book C# in Depth.

13.2.3. When is a ref parameter not a ref parameter?

C# normally takes a pretty strict line on ref parameters. You have to mark the argument with ref as well as the parameter, to show that you understand what’s going on—that your variable may have its value changed by the method you’re calling. That’s all fine in normal code, but COM APIs often use ref parameters for almost everything, for perceived performance reasons—they usually don’t modify the variable you pass in. Passing arguments by reference is slightly painful in C#. Not only do you have to specify the ref modifier, you also must have a variable. You can’t just pass values by reference.

In C# 4, the compiler makes this a lot easier by letting you pass an argument by value into a COM method, even if it’s for a ref parameter. Consider a call like this, where argument might happen to be a variable of type string, but the parameter is declared as ref object: