1 00:00:00,000 --> 00:00:04,260 The programs in this course are going to make heavy use of variables. 2 00:00:04,260 --> 00:00:08,177 In fact, almost every program you ever write will use variables. 3 00:00:08,177 --> 00:00:12,739 They aren't a complex concept, but they are fundamental to programming. 4 00:00:12,739 --> 00:00:16,598 You probably remember working with variables in your algebra class. 5 00:00:16,598 --> 00:00:19,399 They were often denoted with a x or a y, and 6 00:00:19,399 --> 00:00:22,612 were used to hold a variety of possible values. 7 00:00:22,612 --> 00:00:26,900 That is, variables are names for values that can vary. 8 00:00:26,900 --> 00:00:33,874 In this equation y = x + 5, if we set x = 1 then y would equal 6. 9 00:00:33,874 --> 00:00:38,806 If we set x = 10, then y would equal 15 and so on. 10 00:00:38,806 --> 00:00:41,700 Variables in a programming language are like that but 11 00:00:41,700 --> 00:00:44,153 they can hold other things besides numbers. 12 00:00:44,153 --> 00:00:46,677 They can hold text or a calendar date, or 13 00:00:46,677 --> 00:00:51,075 any other piece of data that can be stored in your computer's memory. 14 00:00:51,075 --> 00:00:53,466 When you assign a value to a variable, 15 00:00:53,466 --> 00:00:57,223 you're giving that value a name that you can refer to it by. 16 00:00:57,223 --> 00:01:00,090 There are couple of different ways to create variables. 17 00:01:00,090 --> 00:01:04,608 The first, which we'll show you in this video, is to use the VAR keyword. 18 00:01:04,608 --> 00:01:08,699 You type the keyword var, a variable name an equals sign, and 19 00:01:08,699 --> 00:01:12,175 then a value you are going to assign to the variable. 20 00:01:12,175 --> 00:01:16,621 And as we saw earlier, all # statements being to end in a semicolon, so 21 00:01:16,621 --> 00:01:19,302 we type a semicolon at the end of each line. 22 00:01:19,302 --> 00:01:24,556 So line 5 here we'll create a variable named number and assignment the value 4. 23 00:01:24,556 --> 00:01:27,848 Let me create a second variable called greeting, and 24 00:01:27,848 --> 00:01:30,349 I'll assign it the string value hello. 25 00:01:30,349 --> 00:01:32,466 Once we've declared a variable, 26 00:01:32,466 --> 00:01:36,272 we can use it anywhere we might use the original piece of data. 27 00:01:36,272 --> 00:01:41,009 Remember in our hello world program we used system.console.writeline to 28 00:01:41,009 --> 00:01:43,084 print some text to the terminal? 29 00:01:43,084 --> 00:01:46,689 We can do the same thing with the contents of variables. 30 00:01:46,689 --> 00:01:51,524 So here I'll use System.Console.WriteLine to print the contents of the number 31 00:01:51,524 --> 00:01:52,232 variable. 32 00:01:52,232 --> 00:01:56,213 And then I'll use System.Console.WriteLine again to print the contents 33 00:01:56,213 --> 00:01:57,650 of the greeting variable. 34 00:01:57,650 --> 00:02:04,856 Let me save this and let's try running it, dotnet run. 35 00:02:04,856 --> 00:02:08,678 And it'll print the contents of the two variables. 36 00:02:08,678 --> 00:02:12,374 We can also use a variable that holds a number in math operations, and 37 00:02:12,374 --> 00:02:14,747 print the results of those to the terminal. 38 00:02:14,747 --> 00:02:18,648 So here I'll use System.Console.WriteLine to print 39 00:02:18,648 --> 00:02:21,971 the result of adding 2 to the number variable. 40 00:02:21,971 --> 00:02:26,800 And here, I'll subtract the variable number from the value 12 and 41 00:02:26,800 --> 00:02:28,638 print the result of that. 42 00:02:28,638 --> 00:02:32,146 Let me save this and run it. 43 00:02:32,146 --> 00:02:35,247 And you can see that first, it prints the values of the number and 44 00:02:35,247 --> 00:02:37,037 greeting variables by themselves. 45 00:02:37,037 --> 00:02:40,705 And then it prints the result of the math operations we did. 46 00:02:40,705 --> 00:02:44,831 The number variable holds the value 4, so 47 00:02:44,831 --> 00:02:49,765 first it prints the result of 4 + 2, which is 6. 48 00:02:51,319 --> 00:02:58,583 And then it prints the result of 12- 4 which is 8. 49 00:02:58,583 --> 00:03:00,887 If we change the values to variables hold, 50 00:03:00,887 --> 00:03:04,024 the program will use that new value instead of the old one. 51 00:03:04,024 --> 00:03:08,029 So let me change the number variable from 4 to 6, and 52 00:03:08,029 --> 00:03:13,469 let me change the greeting variable to the string hello to the string hi. 53 00:03:13,469 --> 00:03:16,614 Save this, and rerun the program, and 54 00:03:16,614 --> 00:03:21,299 you can see that it prints the new value, 6, for number. 55 00:03:21,299 --> 00:03:24,638 It prints the new value, hi, for greeting. 56 00:03:24,638 --> 00:03:32,223 And because number now holds 6 instead of 4, 6 + 2 is 8, and 12- 6 is 6. 57 00:03:32,223 --> 00:03:34,924 We can even assign new values to a variable, 58 00:03:34,924 --> 00:03:39,162 that is replace the value a variable holds in the middle of a program. 59 00:03:39,162 --> 00:03:42,196 For example, I can reassign the value 10 to number. 60 00:03:45,202 --> 00:03:50,075 So I type the name of the variable I want to assign for, a single equal sign and 61 00:03:50,075 --> 00:03:52,371 the new value I want to assign to it. 62 00:03:57,751 --> 00:04:00,309 Let me save this and run it. 63 00:04:05,596 --> 00:04:09,669 Now on the line before re-assignment number holds 6, so 64 00:04:09,669 --> 00:04:11,755 number + 2 still prints 8. 65 00:04:15,835 --> 00:04:23,341 But after the re-assignment number holds 10, so 12- number will print 2. 66 00:04:23,341 --> 00:04:28,550 Notice that I didn't use the var keyword when reassigning a value to the variable. 67 00:04:28,550 --> 00:04:32,429 That's because the var keyword is used to declare a variable. 68 00:04:32,429 --> 00:04:36,530 To declare a variable is to say that you intend to use a variable with 69 00:04:36,530 --> 00:04:37,785 a particular name. 70 00:04:37,785 --> 00:04:42,998 You can only declare one variable at a time with the same name in the same scope. 71 00:04:42,998 --> 00:04:46,269 We'll be talking about scope later in the course. 72 00:04:46,269 --> 00:04:50,373 If I use the var keyword with the reassignment is treated as declaring 73 00:04:50,373 --> 00:04:52,649 another variable with the same name. 74 00:04:52,649 --> 00:04:57,466 If I save this and try to rerun it, I'll get an error. 75 00:04:57,466 --> 00:04:59,977 I need to remove the var keyword so 76 00:04:59,977 --> 00:05:04,384 that it's treated as an assignment, not a declaration. 77 00:05:04,384 --> 00:05:08,362 If I try running this now, the error goes away. 78 00:05:08,362 --> 00:05:11,895 So remember, you can only declare a variable once, but 79 00:05:11,895 --> 00:05:14,763 you can assign to it as many times as you need. 80 00:05:14,763 --> 00:05:19,243 You can name variables whatever you want, but there are a few restrictions. 81 00:05:19,243 --> 00:05:22,512 First up are the rules enforced by the C# language. 82 00:05:22,512 --> 00:05:26,956 Variable names have to start with a letter either lowercase or uppercase or 83 00:05:26,956 --> 00:05:28,586 an underscore character. 84 00:05:28,586 --> 00:05:32,662 After that they can contain any combination of letters numbers or 85 00:05:32,662 --> 00:05:34,303 underscore characters. 86 00:05:34,303 --> 00:05:37,986 Other characters like symbols or spaces are not allowed. 87 00:05:37,986 --> 00:05:40,636 You can't use C# keywords as variable names 88 00:05:40,636 --> 00:05:43,154 because that would confuse the compiler. 89 00:05:43,154 --> 00:05:47,841 Keywords we've seen so far include var, class, and static. 90 00:05:47,841 --> 00:05:51,154 Obviously we haven't had a chance to learn all the keywords yet. 91 00:05:51,154 --> 00:05:54,418 So for now just remember that if you accidentally use a keyword 92 00:05:54,418 --> 00:05:58,520 as a variable name you'll get a compiler error and you'll need to change it. 93 00:05:58,520 --> 00:06:03,187 Beyond the rules that compiler enforces, there are rules of example or 94 00:06:03,187 --> 00:06:06,857 conventions that the C# community follows for names. 95 00:06:06,857 --> 00:06:10,473 Following these conventions will make your code easier to read, 96 00:06:10,473 --> 00:06:12,649 both for you and for other developers. 97 00:06:12,649 --> 00:06:16,013 The first convention is that variable names should always begin with 98 00:06:16,013 --> 00:06:17,061 a lowercase letter. 99 00:06:17,061 --> 00:06:20,871 I know we just said the compiler will allow you to begin a variable name with 100 00:06:20,871 --> 00:06:22,050 an uppercase letter. 101 00:06:22,050 --> 00:06:26,366 But the convention is to start variables with lowercase letters. 102 00:06:26,366 --> 00:06:29,665 It makes them stand out from method and class names. 103 00:06:29,665 --> 00:06:32,580 You should also avoid using underscore characters. 104 00:06:32,580 --> 00:06:36,010 Again, they're legal, but their use is frowned upon. 105 00:06:36,010 --> 00:06:38,949 If there are multiple words in a variable name words, 106 00:06:38,949 --> 00:06:41,446 after the first word should be capitalized. 107 00:06:41,446 --> 00:06:44,870 This style is often called camel case, because the capital 108 00:06:44,870 --> 00:06:48,647 letters stick out of the name, like the humps on a camel's back. 109 00:06:48,647 --> 00:06:53,017 If a variable name consists of a single word, it should be all lowercase. 110 00:06:53,017 --> 00:06:55,898 Names should clearly describe what the variable holds. 111 00:06:55,898 --> 00:06:58,923 Avoid abbreviations unless they're really common. 112 00:06:58,923 --> 00:07:01,492 Also avoid single letters. 113 00:07:01,492 --> 00:07:05,023 We mentioned there are two major ways to declare variables. 114 00:07:05,023 --> 00:07:07,303 We should you the var keyword in this video. 115 00:07:07,303 --> 00:07:09,160 Up next we'll show you the other way.