Post #9 Scopes

Today's topic is scopes. You have the telescope. It allows you to look at far away places. Pirates and the royal navies used scopes and maps to navigate. You also have camera scopes which you use to take pictures. Then there's a stethoscope which you use to listen to heartbeats. 

That was for a little humor since today's topic is very short. 

Scopes: Scopes are important in C#. This is a very basic topic but an interesting one. Scopes are basically boundaries. Scopes are used all the time in classes, namespaces, methods, interfaces, and others. The scope of the main function is considered a global scope. This is because a regular scope is basically defined as a set of curly braces({}). Scopes are another way like namespaces where you can isolate code. The difference being there are ways of using namespaces elsewhere. Anything created in a scope is stuck in the scope. Anything can be accessed in it's scope but also in any scopes nested inside it. That being said if there is a scope inside another scope. Anything from the outer scope can be used in the inner scope or outer scope but anything in the inner scope can only be used in the inner scope. You cannot declare variables with the same name in nested scopes. Inner scope variable cannot have the same name as an outer scope variable. As I mentioned above there are a lot of things that use scopes. The creator of C#, Anders Hejlsberg, along with the fellow authors of The C# Programming Language Fourth Edition point out 23 things that use scopes in C#. You cannot refer to a variable if it is declared after the scope. 

That's pretty much scopes. I'm sad to say unfortunately they have nothing to do with pirates, cameras, or doctors in C#. Now before I go let me give an example. 

using System;
namespace Scopes
{
    // A simple application using C#

    class Program
    {
        // The main function
        static void Main(string[] args)
        {
            //Outer Scope
            int i = 1;
            {
                //Inner Scope
                int j = 2;

                // Displaying the Outer Scope value
                Console.WriteLine("The Outer scope value is " + i);

                // Displaying the Inner Scope value
                Console.WriteLine("The Inner scope value is " + j);
                Console.Read();

            }
        }
    }
}
This is a very basic example from Nathan Clark C# Programming Basics For Absolute Beginners. I'm using this because it is short concise and easy to understand but also is a good representation of scopes. I'm only going to explain the one line because the rest should be self explanatory. Console.Read(); is how C# gets keyboard input. 

That is scopes.

This is the end of this post.

I used all three books below.
Resources: Same Resources

MICROSOFT VISUAL C# .NET by Mickey Williams
C# PROGRAMMING BASICS FOR ABSOLUTE BEGINNERS by NATHAN CLARK

Added the rest of Nathan Clark's C# collection and also a programming basics book.
The C# Programming Language Fourth Edition by Anders Hejlsberg, Mads Torgusen,
Scott Wiltamuth and Peter Golde
Visual Studio Code Environment:

Link to get started:

Comments

Popular posts from this blog

Post #11 Complete Arcade

Post #10 Arcade

Post #2 Hello World!