CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Parameters in C# - Part 5

By Satheesh babu
Posted On Aug 02,2008
Article Rating:
Be first to rate
this article.
No of Comments: 2
Category: C#
Print this article.

Parameters in C# – Part 5

 

Introduction

Parameters in C#, Part - 5 of this article series will help us understand, passing parameters in C#. Moving forward, this part will concentrate on Pass by Reference with an example wherever necessary.

Reference parameters

Reference parameters don't pass the values of the variables used in the function member invocation - they use the variables themselves. Rather than creating a new storage location for the variable in the function member declaration, the same storage location is used, so the value of the variable in the function member and the value of the reference parameter will always be the same.

 Reference parameters need the ref modifier as part of both the declaration and the invocation - that means it's always clear when you're passing something by reference. Let's look at our previous examples, just changing the parameter to be a reference parameter:

 

void Foo(ref StringBuilder x)
{
    x = null;

}

...

StringBuilder y = new StringBuilder();
y.Append ("hello");
Foo (ref y);
Console.WriteLine (y==null);

 

EXAMPLE

using System;
using System.Text;

public class Example6
{
    // Note that Foo is declared static here just
    // to make the sample app simple, so we don't
    // need to instantiate the example class. This
    // has no bearing on the parameter passing
    // discussed
    static void Foo (ref StringBuilder x)
    {
        x = null;
    }

    public static void Main (string[] args)
    {
        StringBuilder y = new StringBuilder();
        y.Append ("hello");
        Foo (ref y);
        Console.WriteLine (y==null);
    }

}

Output:
True

 




Here, because a reference to y is passed rather than its value, changes to the value of parameter x are immediately reflected in y. In the above example, y ends up being null. Compare this with the result of the same code without the ref modifiers. Now consider the struct code we had earlier, but using reference parameters:

void Foo (ref IntHolder x)
{
    x.i=10;

}

...

IntHolder y = new IntHolder();
y.i=5;
Foo (ref y);
Console.WriteLine (y.i);

EXAMPLE

using System;
using System.Text;

public class Example7
{
    struct IntHolder
    {
        public int i;
    }

    // Note that Foo is declared static here just
    // to make the sample app simple, so we don't
    // need to instantiate the example class. This
    // has no bearing on the parameter passing
    // discussed
    static void Foo (ref IntHolder x)
    {
        x.i=10;
    }

    public static void Main (string[] args)
    {
        IntHolder y = new IntHolder();
        y.i=5;
        Foo (ref y);
        Console.WriteLine (y.i);
    }

}

Output:
10

 

The two variables are sharing a storage location, so changes to x are also visible through y, so y. i has the value 10 at the end of this code.

 

Note: what is the difference between passing a value object by reference and a reference object by value?

 You may have noticed that the last example, passing a struct by reference, had the same effect in this code as passing a class by value. This doesn't mean that they're the same thing, however.


Consider the following code:

void Foo (??? IntHolder x)
{
    x = new IntHolder();

}

...

IntHolder y = new IntHolder();
y.i=5;
Foo (??? y);

 

In the case where IntHolder is a struct (i.e. a value type) and the parameter is a reference parameter (i.e. replace ??? with ref above), y ends up being a new IntHolder value - i.e. y.i is 0. In the case where IntHolder is a class (i.e. a reference type) and the parameter is a value parameter (i.e. remove ??? above), the value of y isn't changed - it's a reference to the same object it was before the function member call.

 This difference is absolutely crucial to understanding parameter passing in C#, and is why I believe it is highly confusing to say that objects are passed by reference by default instead of the correct statement that object references are passed by value by default.

 

Conclusion

Thus, we have understood pass by reference in C# in detail. Don’t hesitate to post your queries in the feedback section of this article. We will see about output parameters in the next part of this article series.

Thanks for reading this article!

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
need help to pass datarow as ref parameter
i want to pass a datarow as dt.rows[0] by ref to a method but i get an error that indexer cannot be passed as ref parameter. can you help me?
Help needed in regard to ref parameter
i want to pass a datarow as dt.rows[0] by ref to a method but i get an error that indexer cannot be passed as ref parameter. can you help me?