Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
The programs in this course are going to make heavy use of variables. In fact, almost every program you ever write will use variables. They aren't a complex concept, but they are fundamental to programming.
The programs in this course are going to make heavy use of variables. In fact, almost every program you ever write will use variables. They aren't a complex concept, but they are fundamental to programming.
You probably remember working with variables in your algebra class. They were often denoted with an x, or a y, and were used to hold a variety of possible values. That is, variables are names for values that can vary. In this equation, "Y = X + 5", if we set X equal to 1, then Y would equal 6. If we set X equal to 10, then Y would equal 15. And so on.
- Variables in a programming language are like that, but they can hold other things besides numbers. They can hold text, or a calendar date, or any other piece of data that can be stored in your computer's memory.
- When you assign a value to a variable, you're giving that value a name that you can refer to it by.
- There are a couple different ways to create variables. The first, which we'll show you in this video, is to use the
var
keyword. - You type the keyword
var
, a variable name, an equals sign, and then a value you're going to assign to the variable. - And as we saw earlier, all C# statements need to end in a semicolon (
;
), so we type a semicolon at the end of each line.
var number = 4;
var greeting = "hello";
- We can then use that variable anywhere we might use the original piece of data.
- Remember in our "Hello, World!" program, we used
System.Console.WriteLine
to print some text to the terminal? - We can do the same thing with the contents of variables.
- We can also use a variable that holds a number in math operations, and print the results of those to the terminal.
- Remember in our "Hello, World!" program, we used
System.Console.WriteLine(number);
System.Console.WriteLine(greeting);
System.Console.WriteLine(number + 2);
System.Console.WriteLine(12 - number);
- If we change the values the variables hold, the program will use that new value instead of the old one.
var number = 6;
var greeting = "hi";
System.Console.WriteLine(number);
System.Console.WriteLine(greeting);
System.Console.WriteLine(number + 2);
System.Console.WriteLine(12 - number);
- We can even assign new values to a variable, that is, replace the value a variable holds in the middle of a program.
- For example, I can re-assign the value 10 to
number
:number = 10;
- Now, on the line before the re-assignment,
number
holds6
, sonumber + 2
still prints 8. - After the re-assignment,
number
holds10
, so12 - number
will print 2.
- For example, I can re-assign the value 10 to
var number = 6;
var greeting = "hi";
System.Console.WriteLine(number);
System.Console.WriteLine(greeting);
System.Console.WriteLine(number + 2);
number = 10; // Update the variable value.
System.Console.WriteLine(12 - number);
- Notice that I didn't use the
var
keyword when re-assigning a value to the variable.- That's because the
var
keyword is used to declare a variable. - To declare a variable is to say that you intend to use a variable with a particular name.
- You can only declare one variable at a time with the same name in the same scope. (We'll be talking about scope later in the course.)
- If I use the
var
keyword with the re-assignment, it's treated as declaring another variable with the same name:var number = 10;
- If I save this and run the program, I'll get an error.
- I need to remove the
var
keyword so that it's treated as an assignment, not a declaration. - Now the program will run again.
- That's because the
- So remember, you can only declare a variable once, but you can assign to it as many times as you need.
You can name variables almost whatever you want, but there are a few restrictions.
First up are the rules enforced by the C# language.
- Variable names have to start with a letter, either lower case or upper case, or an underscore character.
- After that, they can contain any combination of letters, numbers, or underscore characters. Other characters, like symbols or spaces, are not allowed.
- You can't use C# keywords as variable names, because that would confuse the compiler. Keywords we've seen so far include
var
,class
, andstatic
. Obviously we haven't had a chance to learn all the keywords yet, so for now just remember that if you accidentally use a keyword as a variable name, you'll get a compile error and you'll need to change it.
OK:
grade
angle2
taxRate
Not OK:
2ndAngle
class
tax rate
Beyond the rules the compiler enforces, there are some rules of thumb, or conventions, that the C# community follows for names. Following these conventions will make your code easier to read, both for you and for other developers.
- Variable names should always begin with a lower-case letter. I know we just said the compiler will allow you to begin a variable name with an upper-case letter, but the convention is to start variables with lower-case letters. It makes them stand out from method and class names.
- You should also avoid using underscore characters. Again, they're legal, but their use is frowned upon.
- If there are multiple words in a variable name, words after the first word should be capitalized. This style is often called "camel case" because the capital letters stick out of the name like the humps on a camel's back. If a variable name consists of a single word, it should be all lower-case.
- Names should clearly describe what the variable holds. Avoid abbreviations unless they're really common. Also avoid single letters.
OK:
studentFirstName
name
Not OK:
StudentFirstName
student_first_name
studentfirstname
stdfstnm
x
We mentioned there are two major ways to declare variables. We showed you the var
keyword in this video. Up next, we'll show you the other way.
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
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