Jul 6 2006

Parameter Types in C#

Category: General | GeneralBil@l @ 08:35

I read a nice article this morning, wanted to share with you, it is an old and every day article I guess. The article is titled: Parameter passing in C#. It explains in details the different parameter types we have in C#. So I will be just summarizing the ideas mentioned there!

In .NET we have mainly two types: reference types and value types. Reference types mainly hold a reference to an object, while a value type holds the data itself.

For example,

public void myMethod (StringBuilder mySB)
{
        mySB = null;
}

StringBuidler sb = new StringBuilder();
sb.Append("Hello World");
myMethod(sb);
Console.WriteLine(sb);


In this example, we created a reference called sb to a StringBuilder and appended some data, then called the myMethod and passing to it the value of sb and not sb itself and the value of sb in this case is a reference to the StringBuilder object. myMethod would now create a new instance of StringBuilder (as a new storage location) whose value is the value of the passed parameter and hence mySB points to the same StringBuilder object. myMethod then sets the mySB to null which means that the value of the instance created within the method is set to null, and hence when we print out the value of sb, it will be Hello World. I hope you were able to see the idea here which is that, we passed a reference to an object and not the object itself.

However, we can still see the value type in .NET in this example with a small varaiation.

public void myMethod (StringBuilder mySB)
{
        mySB.Append("World");
}

StringBuidler sb = new StringBuilder();
sb.Append("Hello ");
myMethod(sb);
Console.WriteLine(sb);
The same example as above, but within the body of the method, we have appended data to the same reference object. When we print out the value of sb it will be "Hello World". Remember, both instance created refere to the same StringBuilder object and hence when you update the value or data inside the object through one reference, the update will be seen by all objects refering to that StringBuilder object. 

We have 4 different types of parameters in C#:

  1. Value Parameters
    Value parameters hold the data itself. When passed to a method, a new storage location is being created in memory. An example illustrates this type:

    public void myMethod (int myInt)
    {
            myInt = 10;
    }
    
    int newInt = 50;
    myMethod(newInt);
    Console.WriteLine(newInt);
    Since the simple data types such as int, double, character are passed by value, the method myMethod will update the current value of newInt and return it back tobe 10.
  2. Reference Parameters
    By default, classes, delegates, interfaces, arrays, etc .. are passed as a reference. However, sometimes you need to pass not only the reference to an object, but rather the whole object. Check this example:
    public void myMethod (ref StringBuilder mySB)
    {
            mySB = null;
    }
    
    StringBuidler sb = new StringBuilder();
    sb.Append("Hello ");
    myMethod(ref sb);
    Console.WriteLine(sb);
    In this example, we have used the ref keyword which is required when we want to pass a variable by ref. Notice that when we pass a parameter to a method preceeded by ref keyword, we are not passing the value of sb (which is a reference to StringBuilder object) but rather the object itself "sb". myMethod won't create a new storage location but rather use the same storage location of sb, so when it set mySB to be null, means that it is setting the reference itself of sb to null, which means that sb now is null and doesn't refer to any StringBuilder object.
  3. Output Parameters
    Output parameters work much like the ref keyword. You pass a parameter with the out keyword, the calling method would fill out some data inside its body, and the updated data will be reflected outside the method. An example clarifies it more:
    public void myMethod (out int myInt)
    {
            myInt = 10;
    }
    
    int newInt;
    myMethod(out newInt);
    Console.WriteLine(sb);
    

  4. Array Parameters
    Array parameters allows you to pass a variable number of parameters to a method.
    public void myMethod (params int[] myInts)
    {
            foreach(int x in myInts)
            {
                    Console.WriteLine(x + " ");
            }
    }
    
    // Either you pass an array
    int[] newInts = {1,2,3};
    myMethod(newInts);
    
    // Or you send seperate parameters
    myMethod(3,5,7,8);
    

Hope you found my post helpful.

Regards

Tags: ,

Comments are closed