Press "Enter" to skip to content

Lesson 14.1 – Variables

Lesson Objectives

At the end of this lesson, you will know…

  • How to create/declare variables
  • Where variables are visible


Before we start adding the game logic to the program, we should spend a little time going over some of the things you’ll see in the code.

The first thing we’ll talk about is variables.

We’ve already done a little bit with them, but there are some important things you need to be aware of, when creating and using variables.

Here’s the code for this lesson. This class doesn’t really do anything useful, but it shows how to use variables.

namespace Workbench
    public class Variables
        // Class-level variables
        public int classVariable1 = 3; // Visible to other classes
        private int classVariable2 = 7; // Only visible to this class
        private int classVariable3; // Declaring the variable, without setting a value
        public void Function1()
            int functionVariable1;
            if(classVariable1 < 5)
                functionVariable1 = 1;
                int innerVariable1;
            if(classVariable1 >= 5)
                functionVariable1 = 2;
                int innerVariable1;
        public void Function2()
            int functionVariable1;
            if(classVariable2 < 5)
                functionVariable1 = 3;
                int innerVariable1;
            if(classVariable2 >= 5)
                functionVariable1 = 4;
                int innerVariable1;


One trick you can use to figure out where a variable is visible is to look at the curly braces it is between, when it’s “declared”. Generally, the variable is visible anywhere between those curly braces.


Lines 6-8:

These are “class-level variables”. They can be used anywhere in this class.

The variables on line 6 through 8 are declared inside the curly braces for the class (lines 4 and 47). So, they’re visible everywhere between lines 4 and 47 – the whole class.

When we declare the variables on lines 6 and 7, we also set them to a value. We don’t do that on line 8. So, classVariable3 has the default value – which for “int”s is 0.

Different datatypes have different default values. And if you create a variable whose datatype is one of your classes, it has a value of “null” – nothing/empty.

classVariable1 (Line 6) can also be used outside of this class, since it is declared public. So, if another class makes an object of the Variables class, it will be able to change the value of classVariable1.

Since classVariable2 and classVariable3 are private, the other class wouldn’t be able to directly change the values of those variables. However, the other class could call one of the public functions (Function1 or Function2). Since those functions are inside the class, they can modify the private class-level variables (see lines 16, 23, 35, and 42).


Lines 10-27:

“functionVariable1”, on line 12, is declared inside the curly braces for Function1 (lines 11 and 27). So, it’s visible everywhere inside that function.

“innerVariable1” (line 18) is declared between the curly braces on lines 15 and 19. So it’s only visible in between those lines. That’s why we can declare another variable with the same name on line 25, inside another completely separate set of curly braces.

We couldn’t declare another “functionVarable1” on line 18, since we have a functionVariable1 that is already visible at that level – the one declared on line 12.


Lines 29-46:

Notice that we are able to declare a second “functionVariable1” variable on line 31, even though it has the same name as the variable declared on line 12.

We can do that because the “functionVariable1” on line 12 only exists between lines 11 and 27.


General variable declaration rules

It’s a good habit to declare your variables at the lowest level possible.

You could declare all your variables as class-level variables, and then change them in any function of your class. However, that can make it difficult to track down any problems you might have in the future. Since your variable can be changed in lots of places, you won’t necessarily know where it was changed.

Another extremely important rule with variables is to give them meaningful names.

Many programs have long lives. If you come back in six months, or six years, you want to be able to understand what your program is doing. When your class, functions, properties, and variables have clear names, it’s much easier to understand why they exist, and what they’re supposed to do.



Now you know where to declare your variables, and where your variables will be visible.


Source code for this lesson

There is no code for this lesson.


Next lesson: Lesson 14.2 – If statements

Previous lesson: Lesson 13.2 – Creating functions to handle user input

All lessons: Learn C# by Building a Simple RPG Index


  1. Jason
    Jason August 11, 2015

    “When we declare the variables on lines 6 and 7, we also set them to a value. We don’t do that on line 7.”

  2. Ottar
    Ottar September 14, 2020

    Typo on line 29. Fuction2

  3. Tommy
    Tommy January 22, 2021

    Regarding your comment

    ‘Since those functions are inside the class, they can modify the private class-level variables (see lines 16, 23, 35, and 42) ‘

    I can understand why we can consider them ‘private’ since they can only be access within the method itself .

    Why are they consider ‘class level variables’ when they were created within the method rather with the rest of the other class-level variables ( lines 6-8 ) ?

    Thank you

    • Scott Lilly
      Scott Lilly January 22, 2021

      Hi Tommy,

      The “class” is everything from lines 3-47. The “methods” are everything from lines 10-27 and lines 29-46.

      So, the variables declared on lines 6-8 are inside the class, but outside the methods. That’s what makes them “class-level variables”.

      The variables declared on lines 12 and 31 are inside the “class” (lines 3-47), but are also inside methods. So, they are not “class-level variables”. They only exist in their methods. That’s how we can declare “functionVariable1” inside both functions (lines 12 and 31). They only exist within their own methods. So, the methods don’t see the “functionVariable1” that was declared in the other method.

      You can think of the class as a big box, and each method is a smaller box inside the big “class” box. The variables declared in the big box, but outside the small “function” boxes can be seen and used by anything inside the class box – which includes the “function” boxes. But, the variables declared within the small “function” boxes can only be used inside the “function” box.

      Let me know if that didn’t answer the question.

      • Tommy
        Tommy January 24, 2021

        Thanks for you quick response. I do understand regarding ‘scope’ of variables but I must have misinterpreted your below quote

        ‘‘Since those functions are inside the class, they can modify the private class-level variables (see lines 16, 23, 35, and 42) ‘’

        I thought you meant that lines 16, 23, 35, and 42 are private class-level variables hence why I was puzzled.

        Thank you for all your great work, Its really hard to find much beginner friendly C# resource for games unless you want to learn ‘unity’

  4. Kaguyya
    Kaguyya April 1, 2021

    Hey i started last month with programming , maybe i am just too dumb to understand it yet but i want to grasp my mistake

    when you create this function :
    public void Function1()
    int functionVariable1;
    if (classVariable1 = 5)
    functionVariable1 = 2;
    int innerVariable1;
    if (functionVariable1 < 3)
    given that classVariable1 is = 3 , functionVariable1 should be assigned = 1 through the first if statement. Because 3<5. So why when i add a third if statement if ( functionVariable1 <3) {} it gives me an error saying it is unassigned even tho it gets assigned on the first if statement. if i just put functionVariable1 = … under the creation of it it fixes it.But wouldnt that erase the whole point of these if statements. Thanks for your Guide step by step i am working through it ! 🙂

    • Scott Lilly
      Scott Lilly April 1, 2021

      Hi Kaguyya,

      In the code you posted, you have if(classVariable1 = 5). If there is only one equal sign, the line does an assignment, not a comparison. So, when it gets to that line, it sets the value of classVariable1 to 5 and does not run the code inside the “if” block – so functionVariable1 does not get assigned the value of 2.

      If you change if(classVariable1 = 5) (one equal sign) to if(classVariable1 == 5) (two equal signs)

      Let me know if that doesn’t work, or if you have any other questions.

      • Kaguyya
        Kaguyya April 4, 2021

        I kinda forget the comparison of the first 2 if statements in the question.

        So in the first text of the function the functionVariable1 gets created without a value ( default value 0 )

        The first if statement compares 3 < 5 what is correct so it sets the functionVariable1 to 1

        So given that functionVariable1 is 1 now why it gives the error that it hasnt been assigned any value in the third if comparison functionVariable1 < 3

        thanks for the reply

        • Scott Lilly
          Scott Lilly April 5, 2021

          Hi Kaguyya,

          The reason Visual Studio shows an error is because it is not running the program, to check the variable values against the “if” statements. It only knows there are two “if” statements. If they are both false, “functionVariable1” will be unassigned.

          If you change the code to this (, Visual Studio will not show an error. This is because it knows if the “if” is false, the “else” will run and assign a value to “functionVariable1”.

          Let me know if that is not clear.

  5. Kaguyya
    Kaguyya April 6, 2021


    Thanks for the explanation i got it now.

    But i still got a question i thought int variables have a standard value of 0 , why if i just write the the functionVariable1 in the else clause it still counts for the next if statement and gives no error. Even though i technically didnt assign any value to it.

    Best Regards
    Apprentice Kaguyya,

    • Scott Lilly
      Scott Lilly April 7, 2021

      int variables do default to 0. If you had the line “int variable1;” and didn’t use it in a function, you could use the debugger to see it is set to 0.

      The problem is that Visual Studio is trying to be helpful, and it is not perfect. But, it usually points to things that are a problem, or could be a problem.

Leave a Reply

Your email address will not be published. Required fields are marked *