Last active
February 24, 2018 18:30
-
-
Save sometowngeek/5a5d6cd7e95e621089c9079c58d155cd to your computer and use it in GitHub Desktop.
Scoping Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Class1 | |
{ | |
// These are global variables | |
// because they are declared outside of all methods inside of this class. | |
// Therefore, they can be accessed in any functions defined in this class. | |
public int GlobalIntVariable; | |
public string GlobalStringVariable; | |
public void Foo() | |
{ | |
// Because these are local (or instance) variables, | |
// they cannot be accessed outside of this method. | |
int LocalInt; | |
string LocalString; | |
} | |
public void Bar() | |
{ | |
// These are local (or instance) variables | |
// because they are created inside of this method | |
// Therefore, they cannot be accessed outside of this method. | |
int LocalIntVariable; | |
string LocalStringVariable; | |
// This will not work | |
// unless I declare them in this method. | |
LocalInt = 5; | |
LocalString = "hello"; | |
} | |
public void ThisMethod(string localString) | |
{ | |
// However, this works | |
// because it was passed via the parameters. | |
Console.WriteLine(localString); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment