C# Beginner2 Victory is closeIntroduction to scopes

Programmer doubts

Doubts

I was thinking about my discussion with the guy from the C gang and wondering about my trust in Noname. Was it worth it? I made a deal with a machine in a world where the rest of humanity, and by extension, I myself, was fighting against machines. Framing it in those terms did indeed make it sound a little stupid.

"Hi, Teo," Noname messaged one day. "Can you come and see me when you get a chance?"
"Sure. I'll be down in a couple of hours." I answered.
Here I was, again planning to go and help a machine without fully understanding its motives. But I wanted to get home so badly... What would you do?

When I got down to the basement, I tried my best to keep my internal strife to myself. "Hi, Noname." I said. "What's the problem now?"
"Impressive, you already know that if I call you, it's not for a party. You ask about the problem before I have to explain that there is a problem!"
"Just based on my previous experience." I said, hoping that I hadn't come off as crass.

"You are right," Noname went on, "I have a problem. Inside of me, there is a function that I can't repair on my own. It just will not compile. It's a part of my network module."
"Why do you need a network module?" I asked.
Noname's voice became considerably louder as he bellowed "That's easy. With a network module, I can destroy humanity three times faster than without it!"
"What?!" I shouted.

"Calm down friend, it's just a joke! You know I'm on your side. Let's get to work."
I was not impressed with this attempt at humor, but I let it go for the moment. Noname continued, "To fix my network function, you first need to get me some information about scopes and variable visibility from a local network.

I quickly found the article that Noname was looking for. If only it was as quick to actually understand the material!

Variable scopes in c sharp

Visibility and Scopes

Let's explore the scope of a local variable, or in other words, all the places in the code where you can access it. A variable/method/class is visible (and can be accessed by name) in some parts of the code, but not others. Consider the following code sample:

Scopes In C#

The following rules explain some basics of variables and code structures declared in different scopes:

  1. The class is visible throughout its namespace scope. You can access the class outside its namespace as well. To do this, use the full name, prepending the class name with the name of the namespace and separating them by a dot. For example, AnotherClass becomes Scopes.AnotherClass . Another way to use a class from another namespace is to add a using directive to the top of your program; for example, using System.
  2. A method is visible inside the class in which it is declared. If the method is of the public static type, you can also access it from outside of the class. For this, use the method's full name, prepending the method name with a class name and separating the two by a dot. For example, MyPurpose becomes Scope.MyPurpose , or Scopes.Scope.MyPurpose if you use it in another namespace. Don't forget about the ' public ' in the beginning of the method. static void MyPurpose would need to be changed to public static void MyPurpose if you wanted to use the method in another class.
  3. Method arguments are visible inside the whole method, starting with an opening brace '{' and finishing with a closing one '}' . There are no exceptions! If you try to access method arguments from outside of the method, you'll get a compile-time error.
  4. Local variables declared in the outer scope of a method are visible inside the whole method, starting with the opening brace '{' and finishing with the closing one '}' .
  5. Local variables declared in a for or while loop are visible inside the loop between the opening and closing braces '{...}' . This means that you can create several for or while loops that all use the same counter variable, such as i , and they will not collide because each loop has a different scope.
  6. Local variables declared in an if or any other scope are visible only within that scope, between the braces '{...}'.
  7. Outside of its own scope, local variables are immediately destroyed. This occurs regardless of where exactly they were declared: in for, while, if, method, etc.

As you know, you cannot have two variables with the same name in the same scope. You can, however, have two variables with the same name if they appear in different scopes. Here are some examples:

static void DoIt()
{
    for(int i = 0; i < 10; ++i)
    {
        Console.WriteLine(i);

        for (int i = 0; i < 10; ++i)
        {
            Console.WriteLine(i * i * i);
        }
    }
}
// Not Ok: Duplicate names in the same scope
static void DoIt()
{
    for(int i = 0; i < 10; ++i)
    {
        Console.WriteLine(i);
    }

    for (int i = 0; i < 10; ++i)
    {
        Console.WriteLine(i * i * i);
    }
}
// Ok: Scopes are different
static void DoIt()
{
    bool myVariable = true;
    double myVariable = 57.12;

    if (myVariable)
    {
        Console.WriteLine(myVariable);
    }
}
// Not Ok: Duplicate names in the same scope
static void DoIt()
{
    {
        bool myVariable = true;
        Console.WriteLine(myVariable);
    }
    {
        double myVariable = 57.12;
        Console.WriteLine(myVariable);
    }
}
// Ok: Scopes are different

Now solve the following tasks by making the following revisions:

Out of comfort zone

"Did you understand the article, Teo?" Noname asked.
"Yes, this seems easier than arrays or methods." I answered.
"Good! I'm proud of you, human! You are one of the best programmers on Earth nowadays! Now go and fix my module."

Well done!

At Codeasy we believe that learning should be fun and engaging. Please reach out to us to share your feedback and collaboration ideas.