4.3. Using a Variable in a Program

Now that you understand what a symbol table is and the information it contains, let's examine how Visual Studio processes something as simple as an assignment. Assuming that you have defined an integer variable named i, suppose you write the following statement:

i = 10;

The program statement seems simple enough, but let's examine the steps Visual Studio has to perform to process the statement.

  1. Syntax checking: As before, Visual Studio must first make sure that the program statement obeys the syntax rules of the C# language. Because Intellisense doesn't find anything wrong with the statement's syntax, Visual Studio progresses to the next step.

  2. Symbol table checking: Because the statement wants to assign the value 10 into a variable named i, Visual Studio needs to verify that variable i has been previously defined. Two things could go wrong in this step. First, the programmer may have forgotten to define a variable named i. If you forgot to define variable i, Visual Studio issues the following error message:

    The name 'i' does not exist in the current context

    The second possible problem is that a variable named i was defined, but at a scope level that the current context cannot reach. Without going into details at this moment, the scope of a variable refers to its visibility at each point in a program. As I mentioned in Chapter 3, hiding data is a basic principle of encapsulation and a cornerstone of object-oriented programming. The fact that a variable ...

Get Beginning C# 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.