Introduction to Scope in C#

What is scope?

The term Scope is all about the parts of an application where the same instance of a variable is accessible.

Refer to the Box class below. Notice the Curly Braces, i.e. {This text is inside of opening and closing curly braces}. The Member variables m_iLength, m_iWidth, and m_iHeight are members of the class called Box. They have scope within the Box class. We know this because they are declared directly inside the class Box opening brace. We know that they are declared directly inside of class Box because the name of the variables is preceded by a type keyword called int. We are declaring that m_iLength is a type int (integer). We are also declaring that it is private, which means that it is not accessible outside of Box class.

Notice that we have also declared the variables Length and Width at the class level. We have declared that they are integers and that they are public. We declared them as public because we want them to be accessible outside of the Box class. This means that if we are working in another class and we create an instance of the Box class, we will be able to get or set m_iLength via the Length property, which is a public variable.

If we declare a variable inside of a method, then that variable only has scope inside of that method. Furthermore, if we declare a variable inside of a for loop, for example, that variable has scope only within that for loop. This means that outside of that for loop, we will not be able to access that variable because it is out of scope. Out of scope means that we will not be able to read the value of that variable or change the value of that variable. A private variable only has scope within the immediate set of curly braces that it is declared in.

    namespace Shapes
    {// Beginning of namespace
        class Box
        {//Beginning of class
            private int m_iLength; //member variable accessible to this entire class
            private int m_iWidth; //member variable accessible to this entire class
            private int m_iHeight; //member variable accessible to this entire class

            public int Length //property accessible to any class that instantiates this class
            {
                get {return m_iLength;}
                set {m_iLength = value;}
            }

            public int Width //property accessible to any class that instantiates this class
            {
                get {return m_iWidth;}
                set {m_iWidth = value;}
            }

            public int Height //property accessible to any class that instantiates this class
            {
                get {return m_iHeight;}
                set {m_iHeight = value;}
            }   

            public Box()
            {
                //Default Constructor
            }

            public void DecreaseBoxHeight() // method 
            {// accessible to any class that instantiates this class because it is public
                if(m_iHeight > 1) 
                {
                    m_iHeight--; //decrease height
                }
            }
        }//End of class
    }//End of namespace
namespace example
{
    class scopeExample
    {
        private void myFirstMethod()
        {
            int firstMethodVariable = 5;
        }

        private void mySecondMethod()
        {
            int secondMethodVariable = 0;
            secondMethodVariable = firstMethodVariable; // this won't work
        }
    }
}

The scopeExample class will not compile because there is an error in mySecondMethod. firstMethodVariable is not accessible from inside of mySecondMethod because firstMethodVariavble was declared inside of myFirstMethod. Therefor, firstMethodVariable has scope in myFirstMethod, but not in mySecondMethod.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: