Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed C# Basics!
      
    
You have completed C# Basics!
Preview
    
      
  If you declare a variable within a method, it's only accessible within that method. But trust us, that's a good thing!
- Variable declared within a method is accessible only within that method
 - Can declare multiple variables with same name in different scopes
 
using System;
class Program
{
    static void MyMethod()
    {
        // This "total" variable is completely
        // separate from the "total" variable in
        // the Main method!
        int total = 0;
        total += 1;
        Console.WriteLine("total in MyMethod:");
        Console.WriteLine(total);
    }
    static void Main(string[] args)
    {
        int total = 0;
        total += 10;
        MyMethod();
        Console.WriteLine("total in Main:");
        Console.WriteLine(total);
    }
}
total in MyMethod:
1
total in Main:
10
              Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
                      A variable that's declared within a method
is accessible only within that method.
                      0:00
                    
                    
                      Let me show you what I mean.
                      0:04
                    
                    
                      So here, we have a program that, within
the main method, declares a variable,
                      0:06
                    
                    
                      total, and then adds 10 to its value.
                      0:10
                    
                    
                      And you can see down in the console
that it prints that value, 10.
                      0:13
                    
                    
                      But what if we were to add another method
and try to access total within it?
                      0:16
                    
                    
                      So let's add one called
static void MyMethod.
                      0:20
                    
                    
                      And here, in the method body, I'm going to
attempt print the value of total again.
                      0:25
                    
                    
                      Console dot WriteLine, total in my method.
                      0:29
                    
                    
                      Console dot WriteLine total.
                      0:32
                    
                    
                      Let me save that, and go down to
the console and try running this.
                      0:34
                    
                    
                      Up arrow to bring up dotnet run again,
Enter to run it.
                      0:40
                    
                    
                      I get a compile error.
                      0:43
                    
                    
                      The name total does not exist
in the current context.
                      0:45
                    
                    
                      And it says it's on line 8,
which is within my method.
                      0:48
                    
                    
                      So here's the problem.
                      0:53
                    
                    
                      We declared the total variable down here,
but
                      0:54
                    
                    
                      no total variable exists up
here within the MyMethod body.
                      0:56
                    
                    
                      This is what's known as variable scoping.
                      1:00
                    
                    
                      A variable scope is the portion of
the program code that that variable is
                      1:03
                    
                    
                      visible within.
                      1:08
                    
                    
                      So since we declare this total
variable within the Main method,
                      1:09
                    
                    
                      it's visible only within Main.
                      1:13
                    
                    
                      When we try to access it up here,
we get a compile error.
                      1:16
                    
                    
                      If we want to access a variable
name total up here in MyMethod,
                      1:19
                    
                    
                      we're going to have to declare a separate
total variable up here in MyMethod.
                      1:23
                    
                    
                      Now, even though this variable is named
total and this variable is name total,
                      1:31
                    
                    
                      these two entirely separate variables.
                      1:35
                    
                    
                      Let me try placing a call to
MyMethod down here within Main.
                      1:38
                    
                    
                      And let's try running it again.
                      1:46
                    
                    
                      And you can see that, here, we declare
the variable total, set it to 0.
                      1:50
                    
                    
                      Then we add 10 to it so
that total is set to 10.
                      1:56
                    
                    
                      Then, we call MyMethod.
                      1:59
                    
                    
                      Up here, in MyMethod, we declare
an entirely separate variable that is also
                      2:03
                    
                    
                      named total, and we set its value to 0.
                      2:06
                    
                    
                      Then, we print the value of
total down here on line 9.
                      2:09
                    
                    
                      And you'll notice that it says here
in the output, total in MyMethod, 0.
                      2:13
                    
                    
                      At that point, MyMethod finishes, and
it goes back to the Main method where we
                      2:19
                    
                    
                      print total in Main, and then print the
total variable as it exists within main.
                      2:25
                    
                    
                      And you'll notice that the value
of total in main is 10.
                      2:31
                    
                    
                      Even though we set total
back to 0 up here,
                      2:34
                    
                    
                      that's a totally separate total variable.
                      2:36
                    
                    
                      We set this total variable to 10 up here,
and
                      2:39
                    
                    
                      therefore that's what gets
printed down here, 10.
                      2:42
                    
                    
                      And this variable scoping
is actually a good thing.
                      2:47
                    
                    
                      When your program gets really big, and
you want to declare a variable name total,
                      2:50
                    
                    
                      you don't wanna have to worry about
whether there's some variable name
                      2:54
                    
                    
                      total elsewhere in your program.
                      2:57
                    
                    
                      If it weren't for variable scoping,
you would have to worry about that.
                      2:59
                    
                    
                      But because the total variable within
MyMethod is totally separate from
                      3:03
                    
                    
                      the total variable within Main, we can go
ahead and do whatever operations we want
                      3:07
                    
                    
                      on one total variable without worrying
about how it will affect the other.
                      3:12
                    
              
        You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up