Skip to content

Instantly share code, notes, and snippets.

@sometowngeek
Last active February 24, 2018 18:30
Show Gist options
  • Save sometowngeek/5a5d6cd7e95e621089c9079c58d155cd to your computer and use it in GitHub Desktop.
Save sometowngeek/5a5d6cd7e95e621089c9079c58d155cd to your computer and use it in GitHub Desktop.
Scoping Example
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