Team LiB
Previous Section Next Section

Scopes

In C#, the life or accessibility of an object or type is determined by its scope. C# has the following scopes:

We will discuss scope and accessibility in Chapter 5, which covers classes and methods. But to give you an idea of how scopes work, the following shows examples of namespace, class, and method-level scopes.

Code Example: Scopes
Start example
using System;

//Defines namespace scope
namespace MyNamespace
{
      //Defines class scope
      class MyMainClass
      {
            public int MyClassInt;
            public long MyClassLong;
            static void Main(string[] args)
            {
                  //Defines method-level scope
                  int MyInt = 5;
            }
      }
}
End example

Team LiB
Previous Section Next Section