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 4

By SelvaKumar
Posted On Jul 16,2008
Article Rating:
Be first to rate
this article.
No of Comments: 0
Category: C#
Print this article.

Parameters in C# – Part 4

Introduction

Parameters in C#, Part - 4 of this article series will help us understand, passing parameters in C#. In particlular, this part will concentrate on pass by value with examples wherever necessary.

 

Value parameters

By default, parameters are value parameters. This means that a new storage location is created for the variable in the function member
declaration, and it starts off with the value that you specify in the function member invocation. If you change that value, that doesn't
alter any variables involved in the invocation.

 

For instance, if we have:

void Foo (StringBuilder x)
{
    x = null;

}

...

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

 

EXAMPLE
using System;
using System.Text;

public class Example3
{
    // 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 (StringBuilder x)
    {
        x = null;
    }

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

}

 

Output:
False

 

The value of y isn't changed just because x is set to null. Remember though that the value of a reference type variable is the reference -
if two reference type variables refer to the same object, then changes to the data in that object will be seen via both variables.

 

For example:

void Foo (StringBuilder x)
{
    x.Append (" world");

}

...

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

 

EXAMPLE

using System;
using System.Text;

public class Example4
{
    // 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 (StringBuilder x)
    {
        x.Append (" world");
    }

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

}

 

Output:
hello world




 

After calling Foo, the StringBuilder object that y refers to contains "hello world", as in Foo the data " world" was appended to that object
via the reference held in x.
Now consider, what happens when value types are passed by value. As I said before, the value of a value type variable is the data itself.
Using the previous definition of the struct IntHolder, let's write some code similar to the above:

 

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

}

...

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

 

EXAMPLE

using System;
using System.Text;

public class Example5
{
    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 (IntHolder x)
    {
        x.i=10;
    }

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

}

 

Output:
5

 

When Foo is called, x starts off as a struct with value i=5. Its i value is then changed to 10. Foo knows nothing about the variable y,
and after the method completes, the value in y will be exactly the same as it was before (i.e. 5).
As we did earlier, check that you understand what would happen if IntHolder was declared as a class instead of a struct. You should understand why y.i would be 10 after calling Foo in that case.

 

Conclusion

Thus, we have understood pass by value in C# in detail. Don’t hesitate to post your queries in the feedback section of this article. We will see about pass by reference 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