Beginners Guide to Programming In C#: Scope

Seeing this is written for beginners, I don’t expect you to understand what some of the lingoes are (methods, variables, classes, etc). I will have more blog posts to come that dive into these topics directly. For this post, focus on understanding when something is and isn’t accessible rather than what the things I am talking about actually mean. I will refer to these items in simple terms following their actual names in parentheses.

What is Scope?

The topic of scope is rudimentary for programming. Scope, in English, is defined as “the extent of the area or subject matter that something deals with or to which it is relevant” (Scope, 2021). If we reword that definition a bit, scope simply refers to the area of the relevance of any given thing.

So when we talk about scope in programming, we are referring to the visibility of an entity (a thing) within our program. This means that the items we define in our code are only visible in certain areas of our code. This applies to variables, methods, classes – really anything that is programmer-defined. Generally speaking, when talking about scope we are talking about Local and Global scope. The scope of our items will directly affect its accessibility and also where it is stored in memory at runtime – we’ll go over this more later in the post.

Local Scope

Local scope is an item defined within our current context. So let’s take a code example:

public class Dog
{
    private string _name;

    public GetAge()
    { 
        int age = 3; // Local to the GetAge() method
        return age;
    }

    public void SetName(string name)
    {
        this._name = name;
    }
}

In this code, let’s focus on the method called GetAge(). At face value, we can see this method is defining a variable and returning the value of our variable – neat! Taking a closer look at the variable declaration, int age = 3;, this is an example of a locally scoped variable. In other words, the variable’s value is only known during this call to the GetAge() method. The value of age would not be accessible from anywhere else, such as the SetName(name) method because it is defined in a different scope (area).

In more simple terms, this means that the scope (area of relevance) for that variable (thing) is local to that method (area).

To make sure we understand this, lets take a look at another line of our code example:

public class Dog
{
    private string _name; // Local to the Dog instance

    public GetAge()
    { 
        int age = 3;
        return age;
    }

    public void SetName(string name)
    {
        this._name = name;
    }
}

Let’s focus on the instance variable (thing) _name. At face value, it appears that it is a variable that holds the value that represents the name of our dog. Paying attention to where it is being declared, we can see that it is locally scoped to this Dog object. That means that all members (things) within this object know the value of _name, so GetAge() and SetName(name) could both use its stored value and assign values to it, which is precisely what the SetName(name) method does! However, if we were to try and use _name from outside of the Dog class, it would not be available. This is because the scope (area of relevance) of our instance variable (thing) is local to this class (area).

Global Scope

Taking our understanding of local scope, we can now start to determine what global scope is. Global Scope is an item defined outside of our current context. Let’s look at the same code example:

public class Dog
{
    private string _name; // Global to Dog.GetAge() and Dog.SetName(name) methods

    public GetAge()
    { 
        int age = 3;
        return age;
    }

    public void SetName(string name)
    {
        this._name = name;
    }
}

In the previous paragraph, I said that _name is locally scoped to the Dog class which means all of the methods (things) contained within Dog know the value of _name. This is because we are looking at the full context of Dog. However, if we shift our focus from Dog to the SetName(name) method within Dog, _name is global. I know that might be confusing at first, but just remember what defines a local scope – an item declared without our current context. Seeing that _name is defined outside of our current context, it’s considered to be globally scoped.

The Simple Difference Between Local and Global Scope

The one thing that differentiates the two scopes is your current context – where are you currently looking in the code? When determining if something has local and global scope we want to be aware of part of the program/script we are currently looking at.

If an item is defined outside of that context, it is a globally scoped item. If an item is defined within that context, it is a locally scoped item. Determining which is which is always going to be related to where it was defined relative to where you are currently looking.

Resources

Key Terms
  • Variable: A piece of memory that is carved out, given a name, a type, and a value.
  • Method/Function: A fixed block of code that takes inputs and produces various outputs.
  • Class: A blueprint that defines the variables and the methods common to all objects of a certain kind (our class).
Further Education
References

Scope. (2021). Retrieved March 19, 2021, from https://www.lexico.com/definition/scope

Problem with the content?

If there is an inaccuracy in my post or something doesn’t make sense, please feel free to let me know via the comments below.

About the Author

Picture of Vincent Lakatos
Vincent Lakatos
Vincent is a software developer and mentor with a deep passion for programming and teaching others. He started programming at the age of 13 and has spent years since developing his craft and expanding his knowledge by trying new and different things, all while taking that knowledge and sharing it with others to help them on their own journey.