Top-Level Statements

Until C# 9.0, all executable code in C# was required to appear within a type definition and a member such as a method within a type. As we saw in Listing 1.1, this is no longer required. Instead, it is possible for a single file to have top-level statements, statements that appear independent of any type definition and even without a Main method. Such statements are only allowed in one file and they will be the first statements to execute within the program – the equivalent of the any statements that appear in the Main method. In fact, given top-level statements, the compiler generates a class called Program that wraps the top-level statements and places them into a Main method. Furthermore, the method has a contextual keyword – args – that is the equivalent of the string[] args parameter of a Main method.

With top-level statements, C# syntax allows the statements outside a method as a simplification – especially for new C# developers familiar with other languages that are less structured – but then moves these statements into the Main method at compiler time. The end result, therefore, is that there are never any statements in the underlying CIL that are not placed within a type definition and within a type member.

Since the C# compiler moves top-level methods into its own generated main method that is defined within a class named Program, the compiler will issue an error if you try to define an additional class called Program.6 One additional restriction on top-level statements is that any type definition within the same file as the top-level statements must appear after the such statements.

The file with top-level statements can also contain methods, which we will call top-level methods, and such methods can optionally appear independent of a type definition as well.

When running some of the New Project wizards in Visual Studio, there is frequently an option, “Do not use top-level statements” allowing you to choose whether to go with the simpler version or to generate the structure explicitly, resulting in code that looks more like Listing 1.1 (without a class or Main method). Similarly, on the dotnet command line, some of the project templates (i.e. the “Console” argument when running the dotnet new Console command) frequently have a --use-program-main option. However, top-level statements are only available to project that have entry points – Main methods. The compiler doesn’t allow top-level statements on programs that do not have Main methods (such as class libraries).

Top-level statements were mainly introduced to remove unnecessary ceremony when writing simple programs – reducing the complexity for beginners especially. Prior to top-level statements, a program with a single statement would require both a type definition and a Main method just to code the single statement. With top-level statements this structure is optionally eliminated. In addition, top level statements allow for easier embedding of seemingly complete C# snippets within text like Polyglot notebooks ( https://github.com/dotnet/interactive ) or the online version of this book ( https://essentialcsharp.com ).

Advanced Method Parameters

So far this chapter’s examples have returned data via the method return value. This section demonstrates how methods can return data via their method parameters and how a method may take a variable number of arguments.

Value Parameters

Arguments to method calls are usually passed by value, which means the value of the argument expression is copied into the target parameter. For example, in Listing 5.16, the value of each variable that Main() uses when calling Combine() will be copied into the parameters of the Combine() method. Output 5.4 shows the results of this listing.

Listing 5.16: Passing Variables by Value
public class Program
{
    public static void Main()
    {
        // ...
        string fullName;
        string driveLetter = "C:";
        string folderPath = "Data";
        string fileName = "index.html";
 
        fullName = Combine(driveLetter, folderPath, fileName);
 
        Console.WriteLine(fullName);
        // ...
    }
 
    static string Combine(
        string driveLetter, string folderPath, string fileName)
    {
        string path;
        path = string.Format("{1}{0}{2}{0}{3}",
            Path.DirectorySeparatorChar,
            driveLetter, folderPath, fileName);
        return path;
    }
}
Output 5.4
C:\Users\Inigo\Data\index.html

Even if the Combine() method assigned null to driveLetter, folderPath, and fileName before returning, the corresponding variables within Main() will maintain their original values because the variables are copied when calling a method. When the call stack unwinds at the end of a call, the copied data is thrown away.

Beginner Topic
Matching Caller Variables with Parameter Names

In Listing 5.16, the variable names in the caller exactly matched the parameter names in the called method. This matching is provided simply for readability purposes, whether names match is irrelevant to the behavior of the method call. The parameters of the called method and the local variables of the calling method are found in different declaration spaces and have nothing to do with each other.

AdVanced Topic
Reference Types versus Value Types

For the purposes of this section, it is inconsequential whether the parameter passed is a value type or a reference type. Rather, the important issue is whether the called method can write a value into the caller’s original variable. Since a copy of the caller variable’s value is made, the caller’s variable cannot be reassigned. Nevertheless, it is helpful to understand the difference between a variable that contains a value type and a variable that contains a reference type.

The value of a reference type variable is, as the name implies, a reference to the location where the data associated with the object is stored. How the runtime chooses to represent the value of a reference type variable is an implementation detail of the runtime; typically it is represented as the address of the memory location in which the object’s data is stored, but it need not be.

If a reference type variable is passed by value, the reference itself is copied from the caller to the method parameter. As a result, the target method cannot update the caller variable’s value, but it may update the data referred to by the reference.

Alternatively, if the method parameter is a value type, the value itself is copied into the parameter, and changing the parameter in the called method will not affect the original caller’s variable.

Reference Parameters (ref)

Consider Listing 5.17, which calls a function to swap two values, and Output 5.5, which shows the results.

Listing 5.17: Passing Variables by Reference
public class Program
{
    public static void Main()
    {
        // ...
        string first = "hello";
        string second = "goodbye";
        Swap(ref first, ref second);
 
        Console.WriteLine(
            $@"first = ""{ first }"", second = ""{ second }""");
        // ...
    }
 
    static void Swap(ref string x, ref string y)
    {
        string temp = x;
        x = y;
        y = temp;
    }
}
Output 5.5
first = "goodbye", second = "hello"

The values assigned to first and second are successfully switched. To do this, the variables are passed by reference. The obvious difference between the call to Swap() and Listing 5.16’s call to Combine() is the inclusion of the keyword ref in front of the parameter’s data type. This keyword changes the call such that the variables used as arguments are passed by reference, so the called method can update the original caller’s variables with new values.

When the called method specifies a parameter as ref, the caller is required to supply a variable, not a value, as an argument and to place ref in front of the variables passed. In so doing, the caller explicitly recognizes that the target method could reassign the values of the variables associated with any ref parameters it receives. Furthermore, it is necessary to initialize any local variables passed as ref because target methods could read data from ref parameters without first assigning them. In Listing 5.17, for example, temp is assigned the value of first, assuming that the variable passed in first was initialized by the caller. Effectively, a ref parameter is an alias for the variable passed. In other words, it is essentially giving a parameter name to an existing variable, rather than creating a new variable and copying the value of the argument into it.

note
The ref modifier assigns a parameter to refer to an existing variable on the stack rather than creating a new variable and copying the argument value into the parameters.
Output Parameters (out)

As mentioned earlier, a variable used as a ref parameter must be assigned before it is passed to the called method, because the called method might read from the variable. The “swap” example given previously must read and write from both variables passed to it. However, it is often the case that a method that takes a reference to a variable intends to write to the variable but not to read from it. In such cases, clearly it could be safe to pass an uninitialized local variable by reference.

To achieve this, code needs to decorate parameter types with the keyword out. This is demonstrated in the TryGetPhoneButton() method in Listing 5.18, which returns the phone button corresponding to a character.

Listing 5.18: Passing Variables Out Only
public static int Main(string[] args)
{
    if(args.Length == 0)
    {
        Console.WriteLine(
            "ConvertToPhoneNumber.exe <phrase>");
        Console.WriteLine(
            "'_' indicates no standard phone button");
        return 1;
    }
    foreach(string word in args)
    {
        foreach(char character in word)
        {
            if (TryGetPhoneButton(character, out char button))
            {
                Console.Write(button);
            }
            else
            {
                Console.Write('_');
            }
        }
    }
    Console.WriteLine();
    return 0;
}
 
static bool TryGetPhoneButton(char character, out char button)
{
    bool success = true;
    switch(char.ToLower(character))
    {
        case '1':
            button = '1';
            break;
        case '2':
        case 'a':
        case 'b':
        case 'c':
            button = '2';
            break;
 
        // ...
 
        case '-':
            button = '-';
            break;
        default:
            // Set the button to indicate an invalid value
            button = '_';
            success = false;
            break;
    }
    return success;
}

Output 5.6 shows the results of Listing 5.18.

Output 5.6
>ConvertToPhoneNumber.exe CSharpIsGood
274277474663

In this example, the TryGetPhoneButton() method returns true if it can successfully determine the character’s corresponding phone button. The function also returns the corresponding button by using the button parameter, which is decorated with out.

An out parameter is functionally identical to a ref parameter; the only difference is which requirements the language enforces regarding how the aliased variable is read from and written to. Whenever a parameter is marked with out, the compiler checks that the parameter is set for all code paths within the method that return normally (i.e., the code paths that do not throw an exception). If, for example, the code does not assign button a value in some code path, the compiler will issue an error indicating that the code didn’t initialize button. Listing 5.18 assigns button to the underscore character because even though it cannot determine the correct phone button, it is still necessary to assign a value.

A common coding mistake when working with out parameters is to forget to declare the out variable before you use it. Starting with C# 7.0, it is possible to declare the out variable inline when invoking the function. Listing 5.18 uses this feature with the statement TryGetPhoneButton(character, out char button) without ever declaring the button variable beforehand. Prior to C# 7.0, it would be necessary to first declare the button variable and then invoke the function with TryGetPhoneButton(character, out button).

Another C# 7.0 feature is the ability to discard an out parameter entirely. If, for example, you simply wanted to know whether a character was a valid phone button but not actually return the numeric value, you could discard the button parameter using an underscore: TryGetPhoneButton(character, out _).

Prior to C# 7.0’s tuple syntax, a developer of a method might declare one or more out parameters to get around the restriction that a method may have only one return type; a method that needs to return two values can do so by returning one value normally, as the return value of the method, and a second value by writing it into an aliased variable passed as an out parameter. Although this pattern is both common and legal, there are usually better ways to achieve that aim. For example, if you are considering returning two or more values from a method and C# 7.0 is available, it is likely preferable to use C# 7.0 tuple syntax. Prior to that, consider writing two methods, one for each value, or still using the System.ValueTuple type but without C# 7.0 syntax.

note
Each and every normal code path must result in the assignment of all out parameters.
Read-Only Pass by Reference (in)

In C# 7.2, support was added for passing a value type by reference that was read only. Rather than passing the value type to a function so that it could be changed, read-only pass by reference was added: It allows the value type to be passed by reference so that not only copy of the value type occurs but, in addition, the invoked method cannot change the value. In other words, the purpose of the feature is to reduce the memory copied when passing a value while still identifying it as read only, thus improving the performance. This syntax is to add an in modifier to the parameter. For example:

int Method(in int number) { ... }

With the in modifier, any attempts to reassign number (number++, for example) will result in a compile error indicating that number is read only.

Return by Reference

Another C# 7.0 addition is support for returning a reference to a variable. Consider, for example, a function that returns the first pixel in an image that is associated with red-eye, as shown in Listing 5.19.

Listing 5.19: ref Return and ref Local Declaration
// Returning a reference
public static ref byte FindFirstRedEyePixel(byte[] image)
{
    // Do fancy image detection perhaps with machine learning
    for (int counter = 0; counter < image.Length; counter++)
    {
        if (image[counter] == (byte)ConsoleColor.Red)
        {
            return ref image[counter];
        }
    }
    throw new InvalidOperationException("No pixels are red.");
}
public static void Main()
{
    byte[] image = new byte[254];
    // Load image
    int index = new Random().Next(0, image.Length - 1);
    image[index] =
        (byte)ConsoleColor.Red;
    Console.WriteLine(
        $"image[{index}]={(ConsoleColor)image[index]}");
    // ...
 
    // Obtain a reference to the first red pixel
    ref byte redPixel = ref FindFirstRedEyePixel(image);
    // Update it to be Black
    redPixel = (byte)ConsoleColor.Black;
    Console.WriteLine(
        $"image[{index}]={(ConsoleColor)image[redPixel]}");
}

By returning a reference to the variable, the caller is then able to update the pixel to a different color, as shown in the highlighted lines of Listing 5.19. Checking for the update via the array shows that the value is now black.

There are two important restrictions on return by reference, both due to object lifetime: (1) Object references shouldn’t be garbage collected while they’re still referenced, and (2) they shouldn’t consume memory when they no longer have any references. To enforce these restrictions, you can only return the following from a reference-returning function:

For example, FindFirstRedEyePixel() returns a reference to an item in the image array, which was a parameter to the function. Similarly, if the image was stored as a field within the class, you could return the field by reference:

byte[] _Image;

public ref byte[] Image { get { return ref _Image; } }

In addition, ref locals are initialized to refer to a particular variable and can’t be modified to refer to a different variable.

There are several return-by-reference characteristics of which to be cognizant:

Parameter Arrays (params)

In the examples so far, the number of arguments that must be passed has been fixed by the number of parameters declared in the target method declaration. However, sometimes it is convenient if the number of arguments may vary. Consider the Combine() method from Listing 5.16. In that method, you passed the drive letter, folder path, and filename. What if the path had more than one folder, and the caller wanted the method to join additional folders to form the full path? Perhaps the best option would be to pass an array of strings for the folders. However, this would make the calling code a little more complex, because it would be necessary to construct an array to pass as an argument.

To make it easier on the callers of such a method, C# provides a keyword that enables the number of arguments to vary in the calling code instead of being set by the target method. Before we discuss the method declaration, observe the calling code declared within Main(), as shown in Listing 5.20 with Output 5.7.

Listing 5.20: Passing a Variable Parameter List
using System;
using System.IO;
 
public class Program
{
    public static void Main()
    {
        string fullName;
 
        // ...
 
        // Call Combine() with four parameters
        fullName = Combine(
            Directory.GetCurrentDirectory(),
            "bin""config""index.html");
        Console.WriteLine(fullName);
 
        // ...
 
        // Call Combine() with only three parameters
        fullName = Combine(
            Environment.SystemDirectory,
            "Temp""index.html");
        Console.WriteLine(fullName);
 
        // ...
 
        // Call Combine() with an array
        fullName = Combine(
            new string[] {
                $"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}""Documents",
                "Web""index.html" });
        Console.WriteLine(fullName);
        // ...
    }
 
    static string Combine(params string[] paths)
    {
        string result = string.Empty;
        foreach(string path in paths)
        {
            result = Path.Combine(result, path);
        }
        return result;
    }
}
Output 5.7
C:\Users\Inigo\src\Chapter05.Tests\bin\Debug\net7.0\index.html
C:\WINDOWS\system32\Temp\index.html
C:\Users\Inigo\index.html

In the first call to Combine(), four arguments are specified. The second call contains only three arguments. In the final call, a single argument is passed using an array. In other words, the Combine() method takes a variable number of arguments—presented either as any number of string arguments separated by commas or as a single array of strings. The former syntax is called the expanded form of the method call, and the latter form is called the normal form.

To allow invocation using either form, the Combine() method does the following:

1.
Places params immediately before the last parameter in the method declaration
2.
Declares the last parameter as an array

With a parameter array declaration, it is possible to access each corresponding argument as a member of the params array. In the Combine() method implementation, you iterate over the elements of the paths array and call System.IO.Path.Combine(). This method automatically combines the parts of the path, appropriately using the platform-specific directory separator character. Note that PathEx.Combine() is for demonstration only as it provides a rough implementation of what System.IO.Path.Combine() does already.

There are a few notable characteristics of the parameter array:

Using a parameter array, you can pass a variable number of arguments of the same type into a method. The section “Method Overloading,” which appears later in this chapter, discusses a means of supporting a variable number of arguments that are not necessarily of the same type.

Guidelines
DO use parameter arrays when a method can handle any number—including zero—of additional arguments.

By the way, a path Combine() function is a contrived example since, in fact, System.IO.Path.Combine() is an existing function that is overloaded to support parameter arrays.

________________________________________

6. As the compiler warning indicates, you could define a Program class as partial, indicating that the two definitions will be merged into the same Program class. For more information see Chapter 6 – Partial Classes.
{{ snackbarMessage }}