• Profile
  • Resume
  • Portfolio
  • Blog
Avatar

Nelson Silva

A human. Being. Coffee lover.

Blog

I Write My Thoughts Here

goto and #region/#endregion in C#

February 1, 2022 C#

In this post, you will learn how to work with goto and #region/#endregion so that you can use that same knowledge and thus evolve.

  • goto transfers control of our running application directly to a labeled piece of code;
  • goto is widely used in switches and deeply nested loops;
  • #region allows you to specify a piece of code that can be expanded or collapsed and is often used in large files (in this case, classes) with a lot of code because it has the advantage of concentrating (thus, the programmer’s) only on the piece. of code that we are working on because the rest is hidden, that is, collected;
  • to close a #region block #endregion is required.
#region Libraries
using System;
#endregion

namespace Base {
  class GotoAndRegionEndregion {
    #region Run() method
    public void Run() {
      Console.WriteLine($"TestGoto() method result: {TestGoto()}");
    }
    #endregion

    #region TestGoto() method
    private int TestGoto() {
      var number = 0;

      for (int i = 1; i <= 10; i++) {
        for (int j = i; j <= 10; j++) {
          for (int k = 0; k <= 10; k++) {
            if (j == 5 && k == 5)
              goto FirstLoop;
          }

          number++;
        }

        FirstLoop:
          continue;
      }

      return number;
    }
    #endregion
  }
}

// TestGoto() method result: 25

Don’t forget to watch the video and you can always read this post in Portuguese.

Happy coding!

Previous Next

Categories

  • Announcements
  • Python
  • C#
  • Java
  • Go

Latest Posts

  • for Loop in Go
    May 5, 2022
  • Switch in Go
    May 4, 2022
  • Comparison Operators in Go
    May 3, 2022
+351913416022
contact@nelsonsilvadev.com
Porto, Portugal
Nelson Silva © 2022 All Rights Reserved