Variables

Both Java and C# are strongly typed languages, meaning variables must be declared with a specific type before they are used and can hold only values compatible with that declared type. Both Java and C# define seven types of variables. Despite this coincidence, significant differences exist in how each language categorizes its variable types, as described in Table 5-20.

Table 5-20. A Comparison of Variable Types in Java and C#

Java

C#

Includes

class variable

static variable

Fields declared using the static modifier. In Java, this also includes any fields declared in interfaces. Refer to the Members section earlier in this chapter for full details.

instance variable

instance variable

Fields declared as members of a class (or struct in C#) that are not declared with the static modifier. Refer to the Members section earlier in this chapter for full details.

array component

array element

The elements contained within an array. Refer to the Types section earlier in this chapter for full details.

local variable

local variable

Variables declared within the scope of a function member, statement block, for statement, or switch statement. Also, in C#, a using statement, a foreach statement, or a catch clause.

  

The C# compiler will raise a warning if any declared local variables are not used.

method parameter

value parameter

Refer to the Value Parameters section later in this chapter for full details.

N/A

reference parameter

Refer to the Reference Parameters section later in this chapter for full details.

N/A

output parameter

Refer to the Output Parameters section later in this chapter for full details.

constructor parameter

N/A

Java differentiates between parameters used in normal methods and those used in constructors. C# considers these to be the same as value parameters.

exception-handler parameter

N/A

A variable created to hold the exception instance that has been caught in a try...catch statement. C# considers these to be local variables.

Value Parameters

All parameters specified in function member declarations are considered to be value parameters unless they are preceded by the ref or out keyword. We’ll discuss these two keywords shortly.

When a function member is invoked, variables are created for each parameter specified in the member declaration and are automatically initialized to the default value of the arguments provided in the member invocation. These variables are accessible within the scope of the member using the names assigned to the parameter in the member declaration.

This leads to reference type parameters pointing to the object instances provided as arguments and value type parameters containing a copy of the argument value provided by the caller, meaning that although the contents of any referenced objects can be affected, the actual values of any variables used by the caller to invoke the member remain unchanged. For example:

public class SomeObject {
    // implementation details
}

public class MyClass {

    public static void ValParamTest(SomeObject x, int y) {
        x = new SomeObject();
        y = y * 2;
    }

    public static void Main() {
        SomeObject p = null;
        int q = 3;
        MyClass.ValParamTest(p, q);
    }
}

After running this example, p is still null and q is still equal to 3. This is the default behavior in C# and the only behavior available in Java.

Reference Parameters

Use of the ref keyword creates a reference parameter. When a function member is invoked using a ref parameter, the function member parameter variable represents the same variable used by the caller instead of a new variable being created and assigned the value of the calling argument.

The importance of this is that any changes made within the function member to the variables will be visible to the caller. For example:

public class SomeObject {
    // implementation details
}

public class MyClass {

    public static void RefParamTest(ref SomeObject x, ref int y) {
        x = new SomeObject();
        y = y * 2;
    }

    public static void Main() {
        SomeObject p = null;
        int q = 3;
        MyClass.RefParamTest(ref p, ref q);
    }
}

Note the use of the ref keyword in both the method declaration and the method invocation. After the execution of this code, p will reference an instance of SomeClass and q will equal 6. This is distinctly different from the behavior of value parameters, discussed earlier.

The use of reference parameters is a simple way to overcome the limitation that function members can return only a single value. In Java, a container class would be created to hold the values to pass into the method. C# reference parameters provide a simple solution to this problem in situations that do not warrant the use of a more structured approach.

Output Parameters

The out keyword is used the same and has the same effect as ref, but variables modified with the out keyword don’t have to be initialized before they’re passed into a function member. In the following example, without the use of the out keyword in the declaration and invocation of the OutParamTest method, a compiler error would be raised because the variable x is not initialized before it’s used:

public class MyClass {

    public static void OutParamTest(out int x) {
        x = 5;
    }

    public static void Main() {
        int x;
        MyClass.OutParamTest(out x);
        System.Console.WriteLine("x = " + x);
    }
}

Within the called function member (OutParamTest), any out parameters are initially unassigned, as the compiler does not assign them default values. Hence they must be manually initialized before they can be used; otherwise, a compile-time error will occur. A compile-time error will also occur if the out parameter is not assigned a value before the member returns.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset