1 00:00:00,000 --> 00:00:04,572 [MUSIC] 2 00:00:04,572 --> 00:00:07,511 Before we could even start on the cat food store program, 3 00:00:07,511 --> 00:00:12,060 we had to spend the first stage learning about variables, types and name spaces. 4 00:00:12,060 --> 00:00:14,950 But now we're ready to actually begin work on the store. 5 00:00:14,950 --> 00:00:18,450 The first requirement is to display a welcome message to the user. 6 00:00:18,450 --> 00:00:20,160 Let's work on that now. 7 00:00:20,160 --> 00:00:22,990 If you click the launch workspace button on this video's page, 8 00:00:22,990 --> 00:00:26,530 it will start a workspace with a new C# project. 9 00:00:26,530 --> 00:00:29,750 There are a few files here but there's really only one we care about. 10 00:00:29,750 --> 00:00:32,465 The Program.cs file. 11 00:00:32,465 --> 00:00:35,530 C# source code is stored in plain text files, but 12 00:00:35,530 --> 00:00:40,250 instead of .txt, the file name usually ends in .cs. 13 00:00:40,250 --> 00:00:43,560 If we click the file to open it, we'll see that it's set up and ready for 14 00:00:43,560 --> 00:00:44,550 us to write code. 15 00:00:44,550 --> 00:00:48,410 We need to add our code between the two curly braces following this static 16 00:00:48,410 --> 00:00:49,550 void Main line here. 17 00:00:51,530 --> 00:00:54,650 We need to print a welcome message for the user. 18 00:00:54,650 --> 00:00:57,764 Previously we were able to display text on the terminal with 19 00:00:57,764 --> 00:01:00,380 the System.Console.WriteLine method. 20 00:01:00,380 --> 00:01:01,315 Let's try that here. 21 00:01:01,315 --> 00:01:04,160 System.Console.WriteLline. 22 00:01:04,160 --> 00:01:06,440 Welcome to the cat food store. 23 00:01:06,440 --> 00:01:08,280 But as we showed in the previous stage, 24 00:01:08,280 --> 00:01:12,040 we don't wanna have to type the system namespace all over our code. 25 00:01:12,040 --> 00:01:16,780 So here at the top of the file let's add a using directive for the system namespace. 26 00:01:16,780 --> 00:01:21,010 Using system, and make sure they end up with a semicolon. 27 00:01:21,010 --> 00:01:25,270 And then we can delete the system name space before Console.WriteLine. 28 00:01:25,270 --> 00:01:29,972 We can try running this by going to the console area and typing dotnet run. 29 00:01:29,972 --> 00:01:33,910 The dotnet framework will find our Program.cs file, compile it and 30 00:01:33,910 --> 00:01:35,860 execute the code it contains. 31 00:01:35,860 --> 00:01:38,780 And there in the terminal, we'll see the string we specified. 32 00:01:38,780 --> 00:01:41,660 Our program is displaying a welcome message to the user. 33 00:01:41,660 --> 00:01:44,060 We can cross the first requirement off our list. 34 00:01:45,060 --> 00:01:49,535 So we were able to print a welcome message by calling the Console.WriteLine method. 35 00:01:49,535 --> 00:01:52,020 We've talked about methods briefly in the first stage, but 36 00:01:52,020 --> 00:01:53,840 we never really explained what they are. 37 00:01:53,840 --> 00:01:55,856 It's time to do that now. 38 00:01:55,856 --> 00:01:59,310 When we called GetType on various values to see what their type was, 39 00:01:59,310 --> 00:02:00,640 that was a method. 40 00:02:00,640 --> 00:02:04,780 When we called ToUpper on a string to capitalize it, that was a method. 41 00:02:04,780 --> 00:02:07,460 And we're actually writing code for a new method right now. 42 00:02:08,540 --> 00:02:12,430 This static void Main code here defines a new method, 43 00:02:12,430 --> 00:02:15,670 a special one that gets called when our program starts. 44 00:02:15,670 --> 00:02:18,610 A method is a group of code statements that are put together 45 00:02:18,610 --> 00:02:20,770 to perform a particular task. 46 00:02:20,770 --> 00:02:22,130 WriteLine is a method. 47 00:02:22,130 --> 00:02:26,160 The task it carries out is to write a line of text out to the console. 48 00:02:26,160 --> 00:02:27,970 You run a method by calling it. 49 00:02:27,970 --> 00:02:31,910 To call a method, type its name, followed by a pair of parentheses. 50 00:02:31,910 --> 00:02:35,800 When calling a method, you can provide arguments inside the parentheses. 51 00:02:35,800 --> 00:02:38,710 Arguments are values associated with the method call that 52 00:02:38,710 --> 00:02:40,570 change what the method does when it runs. 53 00:02:41,630 --> 00:02:44,480 Let's write the string Hello here within the parentheses, so 54 00:02:44,480 --> 00:02:46,280 its path is an argument to the method. 55 00:02:47,410 --> 00:02:51,630 Most methods have a specific number and type of arguments they expect. 56 00:02:51,630 --> 00:02:53,920 Some methods expect no arguments. 57 00:02:53,920 --> 00:02:56,950 Some expect two, three, or more. 58 00:02:56,950 --> 00:03:00,200 And of course, every statement in C# needs to end with a semicolon, so 59 00:03:00,200 --> 00:03:01,860 let's add one here at the end of the line. 60 00:03:03,130 --> 00:03:04,430 Let's try running this. 61 00:03:04,430 --> 00:03:06,950 I'll put it back down in the console to activate it, and 62 00:03:06,950 --> 00:03:09,550 hit the up arrow to bring up my previous command. 63 00:03:09,550 --> 00:03:13,204 And you can see that in addition to our original call to Console.WriteLine 64 00:03:13,204 --> 00:03:15,350 which prints welcome to the cat food store. 65 00:03:15,350 --> 00:03:19,280 Our second call to Console.WriteLine runs as well and prints Hello. 66 00:03:24,854 --> 00:03:26,505 Let's try a different method call. 67 00:03:26,505 --> 00:03:29,870 System.Threading.Thread.Sleep. 68 00:03:29,870 --> 00:03:32,090 This method causes the program to wait for 69 00:03:32,090 --> 00:03:34,850 a certain amount of time before continuing. 70 00:03:34,850 --> 00:03:37,410 How does the sleep method know how long it should wait? 71 00:03:37,410 --> 00:03:40,187 We can pass the duration in as an argument to the method call. 72 00:03:40,187 --> 00:03:44,959 It takes an integer with a number of milliseconds, or thousandths of a second, 73 00:03:44,959 --> 00:03:46,466 that it should wait for. 74 00:03:46,466 --> 00:03:51,550 So 1,000 milliseconds would cause it to sleep for one second. 75 00:03:51,550 --> 00:03:53,639 We type the argument here between the parentheses. 76 00:03:53,639 --> 00:03:55,915 Let’s also add another call to WriteLine so 77 00:03:55,915 --> 00:03:58,325 we can tell when the program is done sleeping. 78 00:03:58,325 --> 00:04:05,100 Let me save this and click down in the console to activate it. 79 00:04:05,100 --> 00:04:08,510 And let’s try running it, I hit the up arrow to bring up the previous command and 80 00:04:08,510 --> 00:04:10,910 hit Enter to run dotnet run. 81 00:04:10,910 --> 00:04:15,100 And you can see it prints welcome to the cat food store, pauses for a moment and 82 00:04:15,100 --> 00:04:16,510 then prints done sleeping. 83 00:04:16,510 --> 00:04:18,780 Let me try that one more time to show you again. 84 00:04:18,780 --> 00:04:24,149 Welcome to the cat food store, pause, and done sleeping. 85 00:04:24,149 --> 00:04:27,169 Let's try changing the argument to sleep so that it pauses longer. 86 00:04:27,169 --> 00:04:31,572 I'll change it to 3000 milliseconds so that it pauses for 3 seconds. 87 00:04:33,687 --> 00:04:34,234 Let me save this. 88 00:04:34,234 --> 00:04:36,558 Click down on the console, run it again. 89 00:04:40,790 --> 00:04:44,260 And you can see it pauses for 3 seconds this time. 90 00:04:44,260 --> 00:04:48,300 A quick issue with the sleep method that I want to address before we move on. 91 00:04:48,300 --> 00:04:51,450 You'll notice that System.Threading.Thread.Sleep 92 00:04:51,450 --> 00:04:54,420 appears to start with the System namespace. 93 00:04:54,420 --> 00:04:58,080 Since we have a using System directive at the top of the file, you might 94 00:04:58,080 --> 00:05:02,225 think that we can remove System.dot from the front of the method method Main. 95 00:05:02,225 --> 00:05:03,635 But let's try to run this. 96 00:05:03,635 --> 00:05:08,545 Let me save it, click down in the console, and say dotnet run. 97 00:05:09,635 --> 00:05:14,025 We get an error, the name threading does not exist in the current context. 98 00:05:14,025 --> 00:05:18,705 The problem is that system.threading is a separate namespace from system, so 99 00:05:18,705 --> 00:05:22,200 we can't just remove system from the front of the method Main. 100 00:05:22,200 --> 00:05:23,900 Let me undo that change. 101 00:05:23,900 --> 00:05:27,450 Click my Edit menu and choose undo, 102 00:05:27,450 --> 00:05:31,710 or there will be a shortcut for your system, a keyboard shortcut. 103 00:05:31,710 --> 00:05:34,380 You might want to remember that and use that instead next time. 104 00:05:36,280 --> 00:05:40,200 Instead, we need to also add a using System.Threading 105 00:05:40,200 --> 00:05:42,330 directive at the top of the file. 106 00:05:42,330 --> 00:05:45,320 Note that we're not replacing the using System directive. 107 00:05:45,320 --> 00:05:47,414 We're writing this directive in addition. 108 00:05:47,414 --> 00:05:52,433 Now that we have a using System.Threading directive, we can remove not System, 109 00:05:52,433 --> 00:05:56,670 but System.Threading from the front of the call to Thread.Sleep. 110 00:05:56,670 --> 00:06:02,630 Let me save this, and click down on the console. 111 00:06:02,630 --> 00:06:05,240 I'll hit Ctrl L to clear it. 112 00:06:05,240 --> 00:06:08,747 And I'll hit the up arrow to bring up my previous command and run dotnet run. 113 00:06:12,660 --> 00:06:15,530 And our code works as before but now it's a bit shorter. 114 00:06:15,530 --> 00:06:19,670 So now you know, whenever we been typing a name followed by parentheses, 115 00:06:19,670 --> 00:06:21,380 we've been calling methods. 116 00:06:21,380 --> 00:06:24,080 Up next, we'll show you how to define your own methods.