Functionality of ":" in C#

Asked

Viewed 130 times

10

Recently I was developing a C# application with Visual Studio and I came across a somewhat unusual situation:

public void Upload(object model)
{
    FOO:
    var text = "teste";
}

In the code, FOO: does not appear as error, and anything I put before the : compiler keeps accepting, even without being commented or something like that, but if I put it after the variable declaration, for example, then it starts to give error normally.

This way of error

public void Upload(object model)
{
    var text = "teste";
    FOO:
}

What is this feature for?

  • I’ve been using C for years and I’ve never wondered about it, great.

1 answer

9


The : is used in several contexts and each one can have a difference of meaning.

In this context it is an indicator of completion of a label.

A label is a symbol marking any position in the code (or a more specific position within a command switch, but not in the case presented). It is used for in some command goto can specify where the code flow should go. This symbol is no different than a variable name or function name.

The goto is an unconditional execution flow deviation command, so it switches to the location of the label, whenever the execution goes through there. Its usefulness is limited and heavily questioned, so you should avoid until you have a good reason to use.

Not using the goto there’s no point in having the label. In the examples they serve for nothing and can be removed. But if leaving does not cause any harm, except the fact that another programmer will find very strange this in the code.

Once compiled this disappears, it is only indicative of the code. Instead of the label of goto the address of the location of the code where the label, in case the FOO.

The reason for the error in the second example is that there is no instruction after the label, so there’s no actual address there, there’s no way label point to no address because it is the end of the function. If you need to close the function use the return.

A more real use, but not a good example because it has old, bad things and the use of goto is unnecessary (taken from the documentation):

public class GotoTest1 {
    static void Main() {
        int x = 200, y = 4;
        int count = 0;
        string[,] array = new string[x, y];
        // Initialize the array.
        for (int i = 0; i < x; i++)
            for (int j = 0; j < y; j++)
                array[i, j] = (++count).ToString();
        // Read input.
        Console.Write("Enter the number to search for: ");
        // Input a string.
        string myNumber = Console.ReadLine();
        // Search.
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                if (array[i, j].Equals(myNumber)) {
                    goto Found;
                }
            }
        }
        Console.WriteLine($"The number {myNumber} was not found.");
        goto Finish;
    Found:
        Console.WriteLine($"The number {myNumber} is found.");
    Finish:
        Console.WriteLine("End of search.");
        // Keep the console open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}

Without goto and improved (can improve more):

using static System.Console;

public class GotoTest1 {
    static void Main() {
        int x = 200, y = 4;
        var count = 0;
        var array = new string[x, y];
        for (int i = 0; i < x; i++)
            for (int j = 0; j < y; j++)
                array[i, j] = (++count).ToString();
        Write("Enter the number to search for: ");
        var myNumber = ReadLine();
        bool found = Search(array, x, y, myNumber);
        if (found) WriteLine($"The number {myNumber} is found.");
        else WriteLine($"The number {myNumber} was not found.");
        WriteLine("End of search.");
    }
    private static bool Search(string[,] array, int x, int y, string myNumber) {
        for (int i = 0; i < x; i++) for (int j = 0; j < y; j++) if (array[i, j] == myNumber) return true;
        return false;
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Wow! I had heard of the drop but I didn’t even imagine it would be this case. It helped me a lot here. Thank you very much.

  • @Gustavopinatti now you can vote for everything on the site.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.