1 00:00:00,068 --> 00:00:04,721 Declaring variables using the var keyword, only works if you initialize the variable 2 00:00:04,721 --> 00:00:07,727 with a value, that is assign a value to it immediately. 3 00:00:10,906 --> 00:00:14,367 If you don't, you'll get a syntax error saying, 4 00:00:14,367 --> 00:00:18,000 implicitly-typed variables must be initialized. 5 00:00:18,000 --> 00:00:21,411 So what does it mean, implicitly-typed variables? 6 00:00:21,411 --> 00:00:25,551 Values in C# are classified into different types that specify 7 00:00:25,551 --> 00:00:27,667 what the values can be used for. 8 00:00:27,667 --> 00:00:31,739 You can view of a value by calling the GetType method on it. 9 00:00:34,102 --> 00:00:35,758 We haven't covered methods yet. 10 00:00:35,758 --> 00:00:40,399 But for now, all you need to know is that a method is a collection of code which you 11 00:00:40,399 --> 00:00:42,425 can call to get it to do something. 12 00:00:42,425 --> 00:00:46,920 You call a method by putting a dot, that's the period character, 13 00:00:46,920 --> 00:00:50,863 the method name, and a pair of parentheses after a value. 14 00:00:50,863 --> 00:00:57,088 For whole numbers, the GetType method gives a type of System.Int32. 15 00:00:57,088 --> 00:01:01,133 For strings of text surrounded by double quotes, 16 00:01:01,133 --> 00:01:04,517 GetType gives a type of System.String. 17 00:01:07,957 --> 00:01:10,888 C# has dozens of types built in. 18 00:01:10,888 --> 00:01:16,261 Integers, as we mentioned, have a type of System.Int32 by default. 19 00:01:18,018 --> 00:01:21,838 Floating point numbers, that is, numbers with a decimal point, 20 00:01:21,838 --> 00:01:24,303 have a type of System.Double by default. 21 00:01:24,303 --> 00:01:28,704 The special values true and false has a type of System.Boolean. 22 00:01:28,704 --> 00:01:32,738 Strings of text characters surrounded by double quotes, 23 00:01:32,738 --> 00:01:36,210 as we mentioned, have a type of System.String. 24 00:01:38,758 --> 00:01:43,481 And individual characters of text surrounded by single quotes have a type of 25 00:01:43,481 --> 00:01:44,524 System.Char. 26 00:01:46,083 --> 00:01:50,110 As we mentioned, types specify what a value can be used for. 27 00:01:50,110 --> 00:01:53,960 To understand this, think about objects in the world around you. 28 00:01:53,960 --> 00:01:58,167 You can read a book or a magazine because those are printed materials. 29 00:01:58,167 --> 00:02:01,573 You can drink coffee or soda because those are beverages. 30 00:02:01,573 --> 00:02:05,905 But if you try to drink a book or read coffee, it won't go very well. 31 00:02:05,905 --> 00:02:10,670 Likewise, the type of a C# value specifies what you can do with it. 32 00:02:10,670 --> 00:02:15,157 You can do math with numbers or you can capitalize text. 33 00:02:15,157 --> 00:02:19,147 Likewise, the type of a C# value specifies what you can do with it, 34 00:02:19,147 --> 00:02:22,442 you can do math with numbers or you can capitalize text. 35 00:02:22,442 --> 00:02:26,249 But it does not make any sense to try to capitalize the number. 36 00:02:29,480 --> 00:02:33,060 Nor does it make sense to do math with a string. 37 00:02:33,060 --> 00:02:36,031 You'll get errors if you try. 38 00:02:36,031 --> 00:02:38,465 Those errors are actually a good thing. 39 00:02:38,465 --> 00:02:42,273 C# detects that you made a mistake, and is letting you know about it, so 40 00:02:42,273 --> 00:02:45,407 you can fix the problem before users of your program see it. 41 00:02:45,407 --> 00:02:50,052 And the only reason C# can detect these kinds of errors is because of types. 42 00:02:50,052 --> 00:02:54,862 Types help protect you from making mistakes in your code. 43 00:02:54,862 --> 00:02:57,865 In software development, this is known as type safety. 44 00:02:57,865 --> 00:03:01,302 The assurance that when you have a value of a particular type, 45 00:03:01,302 --> 00:03:05,880 you can do particular operations on it and be certain those operations will work. 46 00:03:05,880 --> 00:03:10,382 Type safety is an important feature of the C# language. 47 00:03:10,382 --> 00:03:15,132 So now let's look again at that error we got when we declared the variable with 48 00:03:15,132 --> 00:03:17,999 the VAR keyword but didn't assign it a value. 49 00:03:17,999 --> 00:03:22,758 It says implicitly type variables must be initialized. 50 00:03:22,758 --> 00:03:25,752 Every variable in C# needs to have a type. 51 00:03:25,752 --> 00:03:30,645 When you use the VAR keyword C# will look the type in the value your initialized 52 00:03:30,645 --> 00:03:34,589 in the variable with, and use that as the type of the variable. 53 00:03:34,589 --> 00:03:37,393 That's what it means by implicitly typed, 54 00:03:37,393 --> 00:03:41,808 the initial value implies that variable should have a certain type. 55 00:03:41,808 --> 00:03:44,920 But if you don't provide an initial value there's no way to 56 00:03:44,920 --> 00:03:49,355 determine what the variables type should be, and that's when you'll get errors. 57 00:03:54,153 --> 00:03:57,692 So how do you declare a variable without assigning an initial value? 58 00:03:57,692 --> 00:03:59,821 You give it an explicit type, 59 00:03:59,821 --> 00:04:03,916 you specify what type of value the variable should hold. 60 00:04:03,916 --> 00:04:06,818 Just write the type name followed by the variable name and 61 00:04:06,818 --> 00:04:08,216 the variable is declared. 62 00:04:20,794 --> 00:04:25,669 Then you can assign a value to the variable later on when you're ready 63 00:04:25,669 --> 00:04:26,435 to use it. 64 00:04:26,435 --> 00:04:28,672 Let me try saving this and running it. 65 00:04:31,536 --> 00:04:34,581 And you can see everything is working. 66 00:04:34,581 --> 00:04:38,752 If you want, you can initialize a variable with an explicit type declaration 67 00:04:38,752 --> 00:04:41,649 just like you can when you’re using the VAR keyword. 68 00:04:41,649 --> 00:04:43,720 So let me edit these four statements together. 69 00:04:45,816 --> 00:04:52,353 I'll delete my number = 10 assignment and say instead System.Int32 number = 10. 70 00:04:52,353 --> 00:04:57,677 And I'll delete my what's up greeting assignment, And 71 00:04:57,677 --> 00:05:01,711 instead, initialize the greeting variable with that value. 72 00:05:01,711 --> 00:05:04,139 Let me save that, try rerunning it. 73 00:05:07,594 --> 00:05:09,243 And I get the same result. 74 00:05:12,014 --> 00:05:15,604 By the way, we did something a little unusual with the types for 75 00:05:15,604 --> 00:05:18,805 our variables that C# developers don't normally do. 76 00:05:18,805 --> 00:05:24,137 When you call the GetType method on those signatures, you get System.Int32 back. 77 00:05:24,137 --> 00:05:27,135 For strings, you GetType return System.String. 78 00:05:27,135 --> 00:05:28,985 Let me run this real quick so we can confirm that. 79 00:05:31,684 --> 00:05:34,382 Oops I forgot to say first, let me say that. 80 00:05:35,919 --> 00:05:37,503 And go back to the console and run it again. 81 00:05:39,584 --> 00:05:44,140 There we go System.Int32 and System.String. 82 00:05:44,140 --> 00:05:47,681 So to make the connection clear we use System.Int32 and 83 00:05:47,681 --> 00:05:50,581 System.String is the types of our variables. 84 00:05:50,581 --> 00:05:56,182 But typing System.Int32 and System.String all the time would get really tedious. 85 00:05:56,182 --> 00:05:59,184 So C# offers some shortcuts. 86 00:05:59,184 --> 00:06:04,483 A type of int with a lower case i is equivalent to System.Int32. 87 00:06:04,483 --> 00:06:09,888 And a type of string with a lower case s is equivalent to System.String. 88 00:06:09,888 --> 00:06:12,020 Let me save this, and let's try running that. 89 00:06:15,619 --> 00:06:17,387 And you'll see we get the same result. 90 00:06:17,387 --> 00:06:23,723 Int converts to System.Int32 and string converts to System.String. 91 00:06:23,723 --> 00:06:26,734 There are shortcuts for other frequently used types too. 92 00:06:26,734 --> 00:06:30,616 Here's a program that uses variable declarations with explicit types for 93 00:06:30,616 --> 00:06:34,314 the same integer, floating point number, Boolean value, string and 94 00:06:34,314 --> 00:06:36,302 character value that we saw earlier. 95 00:06:36,302 --> 00:06:39,421 You can see that we use types of System.Int32, 96 00:06:39,421 --> 00:06:44,110 System.Double, System.Boolen, System.String and System.Char. 97 00:06:44,110 --> 00:06:47,952 Each one of those types has an equivalent shortcut type name. 98 00:06:47,952 --> 00:06:52,697 I can replace the type in each variable declaration with it's shortcut name, and 99 00:06:52,697 --> 00:06:55,116 the program will still work the same way. 100 00:06:55,116 --> 00:07:00,225 So System.Int32 as we already demonstrated can be replace with lowercase int. 101 00:07:02,229 --> 00:07:05,750 System.Double can be replace with lowercase double. 102 00:07:07,536 --> 00:07:10,840 System.Boolean can be replaced with just bool. 103 00:07:12,384 --> 00:07:17,604 System.String can be replaced by the single word string in lowercase. 104 00:07:19,202 --> 00:07:25,045 And System.Char can be replaced with the abbreviation char in lowercase. 105 00:07:25,045 --> 00:07:26,757 Let me save this and run it. 106 00:07:29,524 --> 00:07:33,214 And you can see it still works and it prints the full names out for each type. 107 00:07:33,214 --> 00:07:37,078 C# developers almost never use the full type names for 108 00:07:37,078 --> 00:07:42,044 these built in types in their programs, they use shortcuts instead. 109 00:07:42,044 --> 00:07:45,869 In your code you should use lowercase shortcut names like int, 110 00:07:45,869 --> 00:07:48,116 double, bool, string, and char. 111 00:07:48,116 --> 00:07:53,634 But don't be surprised when the GetType method returns the type System.Int32, 112 00:07:53,634 --> 00:07:58,775 System.Double, System.Boolean, System.String, and System.Char.