Author avatar

Dániel Szabó

Understanding Scope and Visibility in C#

Dániel Szabó

  • Mar 18, 2020
  • 8 Min read
  • Mar 18, 2020
  • 8 Min read
and Tools
Programming Languages


In this guide, we will talk about two crucial concepts in C#: visibility and scope. Most programming languages out there incorporate these two concepts. They lay out the structure in which your data lives and show the direction they are allowed to flow. First we'll take a deeper dive into scope, then we'll turn our sights to visibility.


This concept defines the part of the application where the variable is accessible, called the scope of a variable. Variables can be defined in classes, methods, loops, and structs.

There are three main scopes for a variable:

  1. Class level
  2. Method level
  3. Block level (Nested scope)

Class Level

Let's create some code to demonstrate this.

1using System;
3namespace Pluralsight
5    public class ClassLevel
6    {
7        int a = 6;
8        string b = "This is a string";
9        void ex() {
10            double c = 10.0;
11            Console.WriteLine($"The value c is {c}");
12        }
13        public static void Main()
14        {
15            ClassLevel a = new ClassLevel();
16            Console.WriteLine($"The value a is {a.a}");
17            Console.WriteLine($"The value b is {a.b}");
18            ClassLevel.ex();
19            Console.ReadKey();            
20        }
21    }

The output is the following.

1The value a is 6
2The value b is This is a string
3The value c is 10

When we declare a variable in a class outside any method, it can be accessed anywhere in the class, just like in our case above with the two variables a and b. When you define variables this way, they are called fields or class members. Class level scoped variables, like c in our case, can only be accessed by the non-static method of the class in which they were defined. If we define an access modifier to a class level variable, it does not effect its scope within the class. Access modifiers provide access to fields from outside the class.

Method Level

Let's create an example to demonstrate this.

1using System;
3namespace Pluralsight
5    public class MethodLevel
6    {
7        double c;
8        void first() {
9            double c = 10.0;
10            Console.WriteLine($"The value c is {c}");
11        }
12        void second() {
13            Console.WriteLine($"The value c is {c}");
14        }
16        public static void Main()
17        {
18            MethodLevel a = new MethodLevel();
19            Console.WriteLine($"The value c is {a.c}");
20            a.first();
21            a.second();
23            Console.ReadKey();            
24        }
25    }

The output is as follows.

1The value c is 0
2The value c is 10
3The value c is 0

This demonstrates how methods encapsulate their own namespace and the variables defined within. We can see how the prints for the value of c are different in this case. Whenever we define a variable within a method, it is assigned the method level scope. and we cannot access it outside this method. This variable cannot overwrite or be overwritten by a variable of the same name from a higher scope. These variables are called local variables. We cannot declare them twice with the same name in the same scope. After the method is executed, the variables cease to exist, as they are collected by the ever-greedy garbage collector.

Block Level

Let's also demonstrate this with an example.

1using System;
3namespace Pluralsight
5    public class BlockLevel
6    {
7        public static void Main()
8        {
9            double roentgen = 3.6;
10            if (roentgen >=3.6)
11                { string message = "Not great, not terrible!"; }
12            else
13                { string message = "Well, could be worse!"; }
15            Console.WriteLine($"Status report: {message");
16            Console.ReadKey();            
17        }
18    }

The above code will not compile or execute. The problem is indicated by the compiler in two stages. The first message we get is that the value of the message variable was declared but never used, and the second message we get is that the message variable does not exist in the current context.

We can circumvent this by modifying the message not to be a string declared in the if, but either bring it up to the scope of the Main() function or make it a class field.

These variables are typically useful when declared inside a for or while loop. Sometimes they are referred to as loop variables because of their limited scope. When you declare a variable outside a loop, it's still accessible within nested loops, which means a class level variable will be accessible to the methods and the loops as well. The method level will be accessible to the method and the loop. The variable declared inside a loop body will not be accessible from higher scopes.


When we talk about visibility, we need to consider class,method, and variable constructs. Visibility tells us how these items can be accessed.

There are five types of visibility:

  1. Public
  2. Protected
  3. Internal
  4. Protected internal
  5. Private


This is the most forgiving visibility type. The member defined this way can be reached from anywhere, by default enums and interfaces are public.


Members specified this way can only be reached from the same class, or from within the class.


Members specified this way can only be reached from the same project.

Protected Internal

Members specified this way can only be reached from the same project, and those classes that inherit from the class, even from another project.


Members specified this way can only be reached by other members of the same class. By default, classes and structs are set to this visibility level, and this is the most restrictive.

Let's create a small demonstration code.

1using System;
3namespace Pluralsight
6    public class AnotherVisibility {
7        public string message = "I am visible from anywhere!";
8        protected string pmessage = "Only from the same class!";
9        internal string imessage = "I am visible only from within the same project!";
10        private string ppmessage = "Untouchable!";
11        public void ProtectedMessage() {
12            Console.WriteLine(pmessage);
13            Console.WriteLine(ppmessage);
14        }
16    }
18    public class Visibility : AnotherVisibility
19    {
20        public static void Main()
21        {
22            Visibility a = new Visibility();
23            AnotherVisibility b = new AnotherVisibility();
24            Console.WriteLine(a.message);
25            Console.WriteLine(a.imessage);
26            Console.WriteLine(a.pmessage);
27            b.ProtectedMessage();
28            Console.ReadKey();            
29        }
30    }

The output is as follows.

1I am visible from anywhere!
2I am visible only from within the same project!
3Only from the same class!
4Only from the same class!

Here we can see the concepts in action. Our message variable, since it is public, is accessible from anywhere. We have our pmessage variable, which is accessible from the same or inherited classes. We have the imessage variable, which is accessible from the same or inherited classes. We also have the ppmessage variable, which is only accessible from the original class.


In this guide, we clarified the concepts of scope and visibility. Using live examples, we saw how to write more efficient code that is easier to maintain. At first, learning to use these concepts takes some time, but later they will become second nature. I hope this guide has been informative to you and I would like to thank you for reading it!