Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Main () and Other Methods

Main () and Other Methods (C# vs Java)

This section discusses methods and how method parameters are passed by reference and by value.

Every C# application must contain a single Main method specifying where program execution is to begin. In C#, Main is capitalized, while Java uses lowercase main.

Main can only return int or void, and has an optional string array argument to represent command-line parameters:

static int Main(string[] args)
{
    //... 
    return 0;
}

The string array parameter that contains any command-line arguments passed in works just as in Java. Thus, args[0] specifies the first command-line parameter, args[1] denotes the second parameter, and so on. Unlike C++, the args array does not contain the name of the EXE file.

When you pass parameters to a method, they can be passed by value or by reference. Value parameters simply take the value of any variable for use in the method. Therefore, the variable value in the calling code is not affected by actions performed on the parameters within a method.

Reference parameters, however, point to a variable declared in the calling code, and therefore, methods will modify the contents of that variable when passed by reference.

In both Java and C#, method parameters that refer to an object are always passed by reference, while primitive data type parameters (value types in C#) are passed by value.

In C#, to pass a value type by reference, you need to specify one of the keywords ref or out. The difference between these two keywords is in the parameter initialization. A ref parameter must be initialized before use, while an out parameter does not have to be explicitly initialized before being passed and any previous value is ignored.

Specify this keyword on a value type parameter when you want the called method to permanently change the value of variables used as parameters. This way, rather than passing the value of a variable used in the call, a reference to the variable itself is passed. The method then works on the reference, so that changes to the parameter during the method's execution are persisted to the original variable used as a parameter to the method.

The following code illustrates this in the Add method, where the second int parameter is passed by reference with the ref keyword:

class TestRef
{
    private static void Add(int i, ref int result)
    {
        result += i;
        return;
    }

    static void Main()
    {
        int total = 20;
        System.Console.WriteLine("Original value of 'total': {0}", total);

        Add(10, ref total);
        System.Console.WriteLine("Value after calling Add(): {0}", total);
    }
}

The output of this simple example demonstrates that changes made to the result parameter are reflected in the variable, total, used in the Add method call:

Original value of 'total': 20

Value after calling Add(): 30

This is because the result parameter references the actual memory location occupied by the total variable in the calling code. A property of a class is not a variable, and cannot be used directly as a ref parameter.

The ref keyword must precede the parameter when the method is called, as well as in the method declaration.

The out keyword has a very similar effect to the ref keyword, and modifications made to a parameter declared using out will be visible outside the method. The two differences from ref are that any initial value of an out parameter is ignored within the method, and secondly that an out parameter must be assigned to during the method:

class TestOut
{
    private static void Add(int i, int j, out int result)
    {
        // The following line would cause a compile error: 
        // System.Console.WriteLine("Initial value inside method: {0}", result);

        result = i + j;
        return;
    }

    static void Main()
    {
        int total = 20;
        System.Console.WriteLine("Original value of 'total': {0}", total);

        Add(33, 77, out total);
        System.Console.WriteLine("Value after calling Add(): {0}", total);
    }
}

In this case, the third parameter to the Add method is declared with the out keyword, and calls to the method also require the out keyword for that parameter. The output will be:

Original value of 'total': 20

Value after calling Add(): 110

So, to sum up, use the ref keyword when you want a method to modify an existing variable, and use the out keyword to return a value produced inside the method. It is generally used in conjunction with the method's return value when the method produces more than one result value for the calling code.

Community Additions

ADD
Show:
© 2015 Microsoft