YouTube2Text

C# Tutorial - Full Course for Beginners — Transcript

by freeCodeCamp.org · 55,812 words · 3,203 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Hey, welcome to draft Academy. My name is Mike in this course, I'm going to teach you
  2. 0:03guys everything you need to know to get started using C sharp, a C sharp is an extremely powerful
  3. 0:09programming language. And it's used in millions of applications around the world, there are
  4. 0:13literally 1000s of things you can do with C sharp, and there's tons of different applications
  5. 0:18for this language is extremely powerful. And I'm excited to be bringing you guys a course,
  6. 0:23that will teach you everything you need to know to get started. So we're going to start
  7. 0:26the course with the basics, we're going to install C sharp, we're going to install a
  8. 0:29text editor called Visual Studio, we're going to get everything set up. And we'll start
  9. 0:34writing our first program. I'm going to teach guys the basics of programming. So we're going
  10. 0:38to look at some of the core programming concepts, things like working with data variables, data
  11. 0:43types, getting input from users, then we're going to move on to some more advanced stuff.
  12. 0:47We'll talk about things like loops, and if statements in different data structures. And
  13. 0:52finally, we'll talk about some object oriented programming concepts, things like classes,
  14. 0:57and objects, inheritance, all that fun stuff. If you're new to programming, or you just
  15. 1:01want to try and pick up C sharp, then this course has something for you, I'm really excited
  16. 1:06to be bringing you guys C sharp tutorials. It's an awesome language, and there are literally
  17. 1:101000s of things you can do with it. So without further ado, let's get started. Let's go into
  18. 1:15the course and start learning some C sharp. In this tutorial, I'm going to show you guys
  19. 1:23how to install and get everything set up to start programming in C sharp. Now in order
  20. 1:28to start programming in C sharp, we're actually just going to need one thing, which is going
  21. 1:31to be a program called Visual Studio community. And this is basically a program that will
  22. 1:37allow us to write our C sharp programs. And this is what's called an integrated development
  23. 1:42environment. And then a lot of times you'll hear people call those IDs, it's essentially
  24. 1:47just a special program where we can write and work with our code. So in this case, this
  25. 1:53is an ID that will allow us to work with the C sharp programming language. So over here
  26. 1:58on this website, it's just visual studio.com, forward slash vs forward slash community.
  27. 2:05And this is the website where we can go to download this, you'll see over here, there's
  28. 2:07an option to download on Windows. And then there's also an option to download on Mac
  29. 2:13over here. So you can just click whichever one you need. On my case, I'm on a Windows,
  30. 2:17so I'm just going to go ahead and click the one for Windows. So click this and that should
  31. 2:21start downloading and we should get an installer that we can use to install Visual Studio.
  32. 2:26So once that's downloaded, I'm just gonna click it. And we'll go ahead and run this
  33. 2:29installer. So when the installer loads up, you'll see that we have this little window
  34. 2:33over here. And basically this is a little program that we can use to install what we
  35. 2:37need. Now here's the thing, this Visual Studio program is huge. And there's tons of stuff
  36. 2:43that you can actually do with it. So you can see there's all these different things we
  37. 2:46can install. I mean, there's probably like, over 100 gigabytes worth of stuff that we
  38. 2:49can install here, we basically just need this guy over here says dotnet desktop development,
  39. 2:55this is all we're going to need, we're going to be building something called a console
  40. 2:58application, which is essentially like the simplest type of application that we can build.
  41. 3:02And you'll see over here, it says the install size is around three gigabytes. So that's
  42. 3:07actually not too bad. Some of these things are like a couple, you know, maybe like dozen
  43. 3:11gigabytes, something like that. So yeah, click this dotnet desktop environment. And I'm just
  44. 3:15going to click install, and we're going to go ahead and install that hopefully doesn't
  45. 3:18take too long. Alright, so once that is finished installing and downloading, we can just go
  46. 3:22ahead and click Launch. And we can launch right into Visual Studio and see over here,
  47. 3:27it's asking me to sign in, you don't actually have to sign in. So I'm just gonna click Not
  48. 3:31now, maybe later. And then over here, we have a bunch of options. You'll see down here,
  49. 3:35there's this drop down, I'm just going to click general. So just make sure it's on general.
  50. 3:38And then we can pick whatever style we want to have, we can just go with the basics. So
  51. 3:42I'll just stick with blue. And then I'm going to click Start Visual Studio. Alright, so
  52. 3:47this should go ahead and open up this little starting page, I'm just going to click out
  53. 3:50of this, we don't need to have this up here. And I'm going to go over here. And we're just
  54. 3:54going to click File, I'm going to click new project. So we want to start a new project.
  55. 3:59And this is basically going to allow us to write our applications. So over here in sort
  56. 4:05of Visual C sharp, you can just click here. And you'll see over here, there's an option
  57. 4:09for console app, this is what we want. So don't get too intimidated by all this different
  58. 4:14stuff. Like I said before, Visual Studio is a very powerful program. And it can do a lot
  59. 4:18of stuff. But make sure you just click console app, and we should be good. So I'm just going
  60. 4:22to click OK. And actually down here, we can give this a name. So I'm just going to call
  61. 4:26this draft and then we could store it whatever we wanted to store it. So when this loads
  62. 4:31up, you'll see that we now have this file open. And this is just called program.cs and
  63. 4:36that.cs extension stands for C sharp. And then down here we have a basic program. So
  64. 4:44this is essentially a very simple program. And it's sort of like the template for a program.
  65. 4:48And don't worry too much about what all this stuff is. In the next video. I'm going to
  66. 4:51talk you guys through what all this stuff is what it means and we'll actually write
  67. 4:55our first program but for now, we have everything set up. So we're ready to go and right To
  68. 5:00get started with C sharp development. And this tutorial, I'm going to show you guys
  69. 5:07how to write your first C sharp program. We're also just going to talk in general about what
  70. 5:12a program is, we're going to look at the different instructions that we can give to the computer
  71. 5:17and how those instructions are going to play out and allow us to write applications. So
  72. 5:23over here, we basically just have our basic Project Setup. And I kind of went over how
  73. 5:28to do this in the last tutorial. And we're basically given this little text file, and
  74. 5:33this is a C sharp file. So it's called program.cs. And that CS extension stands for C sharp.
  75. 5:40You'll also notice over here in our solution explorer, we have a bunch of different stuff
  76. 5:45here. So I called my project draft. And then over here, I have like this little properties
  77. 5:51thing, this references thing, really all we need to worry about over here. And I know
  78. 5:56there's a lot of stuff over here is this guy right here, program.cs, that is the C sharp
  79. 6:02file that we're going to be programming and that's the file we're going to be using for
  80. 6:05this course. And this is really all you need to worry about over here. And you'll see that
  81. 6:10this is actually populated with some code with some text by default, you'll see up here
  82. 6:16we have all of these little commands, it just says using system using system collections,
  83. 6:21all this stuff, essentially what this is doing is it's just configuring the file. So you
  84. 6:25don't really have to worry too much about what this stuff is up here. Just make sure
  85. 6:29that you leave it alone, you know, don't touch it. But basically, there's some things that
  86. 6:33we're going to have to use with C sharp and up here, we can specify that we want to use
  87. 6:37them. So there's these things called system. And there's all these different things that
  88. 6:41we can use inside of there. But for the most part, you can just kind of leave this alone
  89. 6:45for now, we'll talk more about what this is later in the course. But for now, you don't
  90. 6:49have to worry about it too much. Down here, you'll see this as namespace draft, you'll
  91. 6:54notice that this is actually the same name as the project that we're using. And essentially,
  92. 6:59that's what this is saying. It's saying that we're in the draft project. And actually,
  93. 7:02this is doing some more stuff. Again, we're going to talk more about namespaces later
  94. 7:06in the course. So you don't really have to worry about that too much. And really, you
  95. 7:10know, all this stuff up here, you can can just leave alone, you don't have to worry
  96. 7:13about that in the beginning. And down here we have these two curly brackets, and these
  97. 7:18two curly brackets essentially just signify that our like program is starting. So inside
  98. 7:24of these curly brackets, we can actually write some code and the computer will be able to
  99. 7:28execute that code. Here we have this keyword class, and it's called program. Now a class
  100. 7:34is essentially just a container where we can store a bunch of code. So inside of a class,
  101. 7:39we can write all of the code for our program, and it'll be able to be executed. So again,
  102. 7:44we're going to talk more about classes as we go on through this course. But for now,
  103. 7:48just know that a class is essentially just a container where we can put code. Down here
  104. 7:53we have this line of code here, it says static, void main. And this is actually what's called
  105. 8:00a method or a function. Again, we're going to talk more about methods and functions later
  106. 8:05in the course. But this is sort of just like another container where we can put code. And
  107. 8:11this is a very important container. Because this container is where we can put all the
  108. 8:17code that we want our program to execute. So this main method, or this main function,
  109. 8:22any code that we put inside of here, in other words, any code that we put inside of these
  110. 8:27open and closed curly brackets, is actually going to get executed by our computer. So
  111. 8:32whatever code I want my program to have, I need to put it inside of this main method.
  112. 8:37So inside of these open and closed curly brackets. Now, I understand that like a lot of this
  113. 8:43stuff doesn't make sense right now. And to be perfectly honest with you, we kind of have
  114. 8:47to just set our program up this way in order for it to work. It doesn't matter if this
  115. 8:51stuff doesn't make sense. Like obviously, you're not going to understand what all this
  116. 8:54stuff is. But just know that that's totally fine. Like as a beginner, you're not expected
  117. 8:58to know any of that you're not going to know any of that through this course though, I'm
  118. 9:02going to be teaching you guys all of this stuff. So by the end of the course, we'll
  119. 9:05understand what all this stuff is doing, why it's important, why it's there. But for now,
  120. 9:09I just need you to trust me that we need to have everything set up like this, in order
  121. 9:13for our programs to run an any code that we're going to write inside of our C sharp file
  122. 9:18that we want to execute, I want to put in between these two curly brackets inside of
  123. 9:22this main block. Okay. So now that we've kind of talked about the structure a little bit,
  124. 9:28we've talked about, you know, some of the stuff that we needed to know, what I want
  125. 9:31to do is actually write our first line of code. So we're going to go ahead and write
  126. 9:36a line of code, which is going to print something out onto the screen. So one of the things
  127. 9:41that is really useful to do when writing a program is to print out information. And the
  128. 9:45most basic instruction that we can give the computer is basically telling it to print
  129. 9:49something out. So I'm going to go ahead and do that down here. We're going to write our
  130. 9:53first line of code. So I'm just going to say console with a capital C dot right? line.
  131. 10:01And then we're going to make an open and closed parentheses. And I'm going to make a semi
  132. 10:05colon. Now inside of these opening closed parentheses, I'm going to make an open and
  133. 10:10closed quotation mark. And I'm just going to type out hello world. So essentially, what
  134. 10:15we're doing here is we're telling C sharp that we want to write this text out onto the
  135. 10:21screen. So let's kind of look through this line of code. I'm saying console dot write
  136. 10:25line. And this is an instruction. So essentially, I'm telling C sharp that I want to write a
  137. 10:30line out onto the console. And as we'll see in a second, the console is basically just
  138. 10:36a little window where we can see the output of our program. I'm saying console dot write
  139. 10:41line. And inside of these parentheses, I'm specifying what I want to write out onto the
  140. 10:45console, or out onto the screen. And I'm basically just saying, hello world. And then over here,
  141. 10:51I'm putting this semi colon. Now whenever we're writing a line of code in C sharp, whenever
  142. 10:57you're done writing that line of code, you always need to end it with a semicolon. So
  143. 11:01for the most part, whenever you write an instruction, whenever you write a line of code, you're
  144. 11:05going to end it with a semicolon. And that's very important. And as a beginner, you're
  145. 11:10going to forget to put the semicolon trust me, everybody does, but eventually, we'll
  146. 11:12just kind of get used to putting it in there. Alright, so I'm going to go ahead and save
  147. 11:16this file. And now we're going to actually execute this program. So when I say execute
  148. 11:20the program, essentially what I'm doing is I'm telling the computer to go through and
  149. 11:25carry out all of the instructions that we specify. In our program, right now, we only
  150. 11:29really have one instruction, which is telling the computer to write a line out onto the
  151. 11:34console. I'm gonna come up here, and you'll see there's this start button. So this is
  152. 11:38going to be our best friend throughout the course, anytime I want to run my program,
  153. 11:42I'm going to go ahead and click this start button. So I'm just gonna click it. And when
  154. 11:45I click this start button, my program is actually going to run. And if you look there, you'll
  155. 11:50see that a blank window popped up for just a second, I'm continuing to run it, essentially
  156. 11:55what's happening is C sharp is printing out the line hello world onto a little console
  157. 12:01window that's popping up. But the problem is that our program is printing that out,
  158. 12:07and then the program is terminating. So that window is going away. In order to keep that
  159. 12:11window up. So we can see what's going on, I'm going to type out another line of code,
  160. 12:15I'm just going to type console dot read line. And basically what this line of code is going
  161. 12:21to do is it's going to keep that little console window up until I enter in a line of text.
  162. 12:27So now when I run my program, you guys will see that this little black window up here
  163. 12:33appears and it stays open. So before it was kind of flashing up and then disappearing.
  164. 12:39But now it's going to stay open. And you'll see we're printing out hello world onto the
  165. 12:43screen. Now, if I click the Enter key, so I'm just gonna click enter, this window is
  166. 12:48going to go away. So anytime I click enter, that basically tells our program that we're
  167. 12:53done executing it. So we want it to go away. And that's basically all we have to do. So
  168. 12:57I can write out instructions in here, I can write out lines of code, I can press the start
  169. 13:01button. And then I'm basically executing all of those lines of code. And then obviously,
  170. 13:06to get out of this, like I said, you can just like type a space and type enter, or you can
  171. 13:10really type anything, and just hit enter and then the program will terminate. This is sort
  172. 13:15of our first C sharp instruction. And like I said, console dot write line basically just
  173. 13:21writes a line of text out onto the screen. And then we can include this console dot read
  174. 13:25line instruction in order to keep that window up so we can see what we're printing out.
  175. 13:31Now that we know how to print out something onto the screen, I want to show you guys how
  176. 13:34we can draw a little shape. So I'm actually going to get rid of this. And I'm going to
  177. 13:40go ahead and copy this right line piece of code. And I'm going to paste it a few times
  178. 13:44down here. So we're actually going to use this line of code to draw out a shape onto
  179. 13:49the screen. So down here, I'm going to create like a forward slash. And then up here, we'll
  180. 13:53create one. And I'm going to keep creating them in this little pyramid structure. So
  181. 13:59now you can see I have like a little pyramid and then I'm going to make a vertical bars
  182. 14:02going all the way down. So essentially, we're drawing out like a little right triangle.
  183. 14:06And actually, you know, I want to put underscores here, so it's a little more obvious. So you
  184. 14:12can see I'm saying console dot write line. And in each one of these instructions, I'm
  185. 14:17telling the computer to write out a different line. So over here, we're writing out like
  186. 14:21the tip of the triangle. Over here, we're writing out the middle part of the triangle,
  187. 14:25and then the base of the triangle. So now let's go ahead and run this program. And you'll
  188. 14:30see over here, we're actually able to print out this entire triangle. So just by using
  189. 14:36that one instruction console dot write line instruction, I was actually able to draw out
  190. 14:41an entire shape onto the screen. So now I'm going to go ahead and exit off of this. And
  191. 14:46you'll see down here how easy it was for us to write this program. Now, essentially what
  192. 14:51happens is when I click that start button, C sharp is going to go into my program. It's
  193. 14:56going to find this block of code right here. Main It's gonna go inside of these curly brackets.
  194. 15:03And it's going to execute each instruction. In other words, each line of code, one after
  195. 15:08the other. So it's going to start with this first line of code. And it's basically going
  196. 15:12to say, Okay, what does the user want me to do, they want me to write a line out onto
  197. 15:16the console. And they want me to write this text. So it's going to go ahead and write
  198. 15:19that out onto the console, then when it's done with this instruction, it's going to
  199. 15:23come down here and go to this second instruction, it's going to do the same thing. It'll execute
  200. 15:28that instruction, and then it'll go down here, execute this one and execute this one. So
  201. 15:33essentially, what C sharp does is it executes all of the lines of code, all the instructions
  202. 15:38that we give it in order. And one of the things you're going to learn is that when we're programming,
  203. 15:43the order matters. So the order in which you write these lines of code actually matters
  204. 15:47a lot. So if I was to take this instruction over here, and I was to paste it up here on
  205. 15:54the top, now, this is going to change the way that the shape looks. So instead of having
  206. 15:58that normal triangle, you can see we have this kind of like funky looking shape, or
  207. 16:02the base is up at the top. And that's because we changed the order of the program. Essentially,
  208. 16:07all a program is is just a set of instructions specified in a particular order. That's it.
  209. 16:13And as we go through this course, and as you learn more about C sharp, you're going to
  210. 16:16find more and more complex instructions that you can use. So the more complex instructions,
  211. 16:23you're able to give the computer and the better, you're able to organize them and order them,
  212. 16:27the more powerful your programs are going to become. So obviously, we only really know
  213. 16:31two instructions, we know this console dot write line, which prints something out in
  214. 16:36console dot read line, which basically pauses the screen until we enter something. But just
  215. 16:41with these two lines of code, we were actually able to draw out an entire shape onto the
  216. 16:45screen. And that was pretty cool. The takeaways from this tutorial is that a program is essentially
  217. 16:50just a set of instructions that the computer is going to follow the order of those instructions
  218. 16:55matter. And in C sharp, the instructions that are going to get executed by the computer
  219. 16:59are going to be inside of these open and closed curly brackets in this main block of code.
  220. 17:06And that's sort of the bare basics of writing a C sharp program, we were able to print something
  221. 17:10out into the screen, we were able to pause that little console until we told it we were
  222. 17:14done. And now we can kind of go forward and start learning more and more complex instructions
  223. 17:19and more and more complex ways to build our programs. In this tutorial, I'm going to talk
  224. 17:28to you guys about variables in C sharp. Now a lot of times when we're writing programs
  225. 17:32in C sharp, we're going to be dealing with a bunch of different data. So there's going
  226. 17:37to be all sorts of information, all sorts of data values in our programs. And a lot
  227. 17:41of times that information, those data values can be kind of hard to keep track of and manage.
  228. 17:47So in C sharp, we have a special container called a variable. And a variable is basically
  229. 17:52just a container where we can store some of that information. So I can store a piece of
  230. 17:57data or a piece of information that I want to keep track of and manage in my programs
  231. 18:03inside of a variable. So in this tutorial, I'm gonna show you guys what variables are,
  232. 18:06why they're useful. And we'll look at kind of how we can work with them in our C sharp
  233. 18:10programs. So down here, I have written out this very basic program. It's basically just
  234. 18:16writing out a bunch of lines of text, it says There once was a man named George, he was
  235. 18:2170 years old. He really liked the name George, but didn't like being 70. So this is basically
  236. 18:26just a program that I wrote, which prints out a little story onto the screen. So if
  237. 18:31I was to run this program, you'll see that we get this story it says There once was a
  238. 18:35man named George who was 70, etc. So I'm able to print out this story onto the screen. So
  239. 18:41this is a perfectly functional program, we've actually written a program which prints out
  240. 18:45a story, right, but let's say that I'm looking at my story. And I'm thinking to myself, Hmm,
  241. 18:49I think I want to change the character's name. Well, let's say instead of having the character
  242. 18:55be named George, I want to change his name to john. Well, I can just come up here, I
  243. 18:59can search through my story, and find every place where I'm mentioning the character's
  244. 19:03name, and I can change it to john. Cool. So now my story is updated. I went through I
  245. 19:10manually changed the name to john, and everything works. But let's say I'm looking at it again.
  246. 19:16And I'm thinking maybe I want to try to appeal to a younger audience. So instead of having
  247. 19:21the character be 70, why don't we have the character be like 35. So once again, I can
  248. 19:26just go through I can look through and every time they mentioned the character's age, I
  249. 19:30can just change it to 35. Here's the next one. Cool. So I basically went through and
  250. 19:37I found out every place where the character's name was mentioned, and I changed it to 35.
  251. 19:41Here's the problem though, in order to make those changes in order to modify the character's
  252. 19:47name and of the characters age, I had to go through and manually change it at every place
  253. 19:53inside of my program. So every place where the character's name was mentioned, and the
  254. 19:57characters age was mentioned. I had to go in and manually change it. Now, this is fine
  255. 20:02with a story that only has four lines. But imagine if this story had hundreds of lines.
  256. 20:07And the character's name was mentioned, like 100 times or their age was mentioned 100 times,
  257. 20:12imagine then if you wanted to change the character's name, or imagine even if you just wanted to
  258. 20:17keep track of and manage the characters name, and it would be extremely difficult. So the
  259. 20:22more lines in the story that I write, the harder it becomes for me to manage the characters
  260. 20:28name and the character's age. And these are two good examples of pieces of information
  261. 20:33in our program. In order to make a situation like this a lot easier, we actually have something
  262. 20:38in C sharp, called a variable. And a variable is basically just a container where I can
  263. 20:44store a specific data value. And then when I want to use that data value, instead of
  264. 20:49having to actually like, type it out and type out what it is, I can just refer to the variables
  265. 20:55name. So I'm gonna show you guys how we can create variables, and how we can keep track
  266. 20:59of and manage the different data in our programs like the characters name and the character's
  267. 21:03age. So up here above my story, I'm actually going to start creating a variable. And the
  268. 21:10way that we can create a variable is by giving C sharp some information. So this is going
  269. 21:15to be a line of code. And the first piece of information that I want to give to C sharp
  270. 21:20is going to be the type of data that I want the variable to be able to store. So remember,
  271. 21:25variables are just containers that we can store values inside of. And different containers
  272. 21:30can store different types of information. Some containers can store text, others can
  273. 21:35store numbers, others can store like other values, like true or false values, we're gonna
  274. 21:40hold go ahead and create a variable that's going to store the character's name. So I
  275. 21:45want to create a variable that can store text, because the characters name is text. And we
  276. 21:50can do that by creating a string. So a string is essentially just like a string of characters.
  277. 21:55And it's basically just plain text. So I'm going to go ahead and tell C sharp that I
  278. 21:59want to create a string variable. So I'm just going to type out string. And then what I
  279. 22:04need to do is give this variable a name. So remember, these are containers. And generally
  280. 22:09when we're naming a variable, we want to name it according to what's going to be stored
  281. 22:14inside of it. That way, we can identify what's in the variable, I'm just going to call this
  282. 22:19character name. And you'll notice here that I'm separating these words. So the second
  283. 22:24word has a capital letter. Alright, so I created my variable, and it's going to store a string,
  284. 22:30which is just plain text. And then I'm going to set it equal to something. So I'm going
  285. 22:34to say character name is equal to, and now I'm going to give this a value. So I'm going
  286. 22:39to give this a value, that's plain text, I'm going to make an open and closed quotation
  287. 22:43mark. And then whenever I write a line of code, I always want to end it off with a semicolon,
  288. 22:48just like that. Alright, now inside of these quotation marks, I can write out the string
  289. 22:52of characters in the string of text that I want to store inside of the character name
  290. 22:57variable. So we're just going to store the character's name, which is john. So I can
  291. 23:00just type out john, up there. In addition to creating a variable to store the characters
  292. 23:05name, I also want to create another variable to store the character's age. So let's go
  293. 23:10ahead and do that. Now the character's age is actually going to be a number, right, so
  294. 23:16the character's age is a number. It's not really plain text like this. And if we want
  295. 23:20to create a variable to store a number, we can create a variable called an integer variable.
  296. 23:25So I N T stands for integer. And an integer is basically just a whole number. So a number
  297. 23:30that doesn't have decimal points. And I'm just going to go ahead and name this character
  298. 23:34age. So again, giving it a name. And I want to show you guys another way that we can create
  299. 23:38these variables. So in addition to saying like string character name is equal to john,
  300. 23:43I can also just say, and character age, I can put a semicolon here. And then I can come
  301. 23:48down here and give this a value. So I could say, like character age is equal to, and now
  302. 23:54we can give this a value. So we said 35. And you'll notice that when I give this a number
  303. 23:59value, I don't need these quotation marks, you only need the quotation marks with a string.
  304. 24:03So these are two different ways that I can create a variable and they're doing exactly
  305. 24:07the same thing up here, I'm just saying string character name, and I'm giving it a value
  306. 24:11right away. Over here, I'm doing what's called declaring the value the variable on basically
  307. 24:16saying that we're going to have a variable called character age, I'm not giving it a
  308. 24:20value right away. And then down here, I'm assigning it a value of 35. So these two,
  309. 24:27this guy and this guy up here are doing exactly the same thing. So now that we have these
  310. 24:33variables, in other words, now that we have containers, where we can store the different
  311. 24:37values, or the different pieces of data that we're going to work with in our programs,
  312. 24:41I want to actually use those variables inside of our story. So I'm gonna show you guys how,
  313. 24:46in addition to printing out this plain text, I can actually print out the value that's
  314. 24:51stored inside of the variable. So over here, you'll notice that I'm typing out there once
  315. 24:55was a man named john, but I don't want to have to type out john anymore. I just Want
  316. 25:00to be able to refer to that variable. So what I can do is I can actually get rid of this.
  317. 25:05And I'm going to come outside of these quotation marks. And over here, I'm going to put a plus
  318. 25:10sign. And this plus sign basically means I want to like append some text, or I want to
  319. 25:15append a value onto the end of this text over here. And what I want to do is just put the
  320. 25:21name of the variable, so I'm just going to type out character name. And what this is
  321. 25:25going to do is it's going to print out there once was a man named, and then a space, and
  322. 25:30then it's going to print out the value that's stored inside of the character name variable.
  323. 25:35So it's going to print out, john. So let me demonstrate that, let's go ahead and run this
  324. 25:39program. And you'll see over here, we're still printing out there once was a man named john,
  325. 25:45except I didn't have to physically type out john down here, all I did was use this plus
  326. 25:51sign, and essentially just append on or people will call this concatenate on the value that
  327. 25:58was stored inside of the character named variable. And this is extremely useful. So I could do
  328. 26:01the same thing down here with this other occurrence of the character's name, I can just say, character
  329. 26:07name. And now over here, it's going to do the same thing, it's going to say he really
  330. 26:11liked the name. And then whatever the value that's stored inside of this character name
  331. 26:16variable, we can do the same thing for the age. So over here, you'll notice that I have
  332. 26:22the age is 35. But remember, we created this variable up here to store that. So what I
  333. 26:28can do is I can actually get rid of this. And I'm actually going to make to open and
  334. 26:32close quotation marks here. And I'm going to make two plus signs. And then in between
  335. 26:36here, I'm going to type out character age, essentially, what I'm doing is I'm telling
  336. 26:41this right mine instruction that I want to print out, he was with a space. And then I
  337. 26:47want to add in the value that's stored inside of the character age variable, which is going
  338. 26:51to be 35. And then I want to add into that years old, so basically, what we're doing
  339. 26:57here is called concatenating, a string, I'm saying I want to type this out, plus, I want
  340. 27:02to type the value that's in here, plus, I want to print out this stuff over there. And
  341. 27:07then finally, we can do the same thing down here. So I'm going to get rid of this. And
  342. 27:12I'm just gonna type in character age. So I've replaced every occurrence of the character's
  343. 27:19name and the character's age with these variables. So now let's go ahead and run our program.
  344. 27:25I'm just going to click this start button. And you'll see over here, we're printing out
  345. 27:28the same exact story as before, it says There once was a man named john, he was 35. He liked
  346. 27:33the name john and 35. So I essentially just replaced john and 35 with the variables that
  347. 27:40are storing them. Now, what's cool about these variables is if I wanted to, for example,
  348. 27:46modify the character's name or the character's age, I can just modify it up here. So let's
  349. 27:50say we wanted to change the character's name to Tom. And we wanted to make him like 25.
  350. 27:55Well, now I only have to modify those two values, I only have to update them in one
  351. 28:00spot. And it's going to update throughout my entire story. So now the character's name
  352. 28:05is Tom. And he's 25 throughout the entire story, and the variables just make it a lot
  353. 28:12easier for us to keep track of and for us to manage those pieces of information. Now,
  354. 28:18you'll notice that I only created variables for the characters name and for the characters
  355. 28:22age. And that's because those two pieces of information are occurring multiple times in
  356. 28:27the story, I didn't create a variable for the word once or for the word man, because
  357. 28:33I just didn't need to, you know what I mean, that's not like a distinct piece of information
  358. 28:37that we want to keep track of. But the characters name and the character's age are definitely
  359. 28:41pieces of information that we're going to want to keep track of and store. So that's
  360. 28:45sort of how we can use variables to make it easier to manage the data in our programs.
  361. 28:49Now I want to show you guys another cool thing we can do with this. Let's say that halfway
  362. 28:53through the story, I wanted the characters named the change, I can actually come down
  363. 28:57here and halfway through printing out the story, I can modify the value of one of these
  364. 29:02variables. So I could say, character name, and I could just give this another value.
  365. 29:07So I can say character name is equal to Mike. And again, I need this semi colon at the end
  366. 29:11here. Don't forget that. Now halfway through the story, the characters name is actually
  367. 29:16going to change. So if we run the program, you'll see over here it says Tom, and then
  368. 29:22down here, the name is Mike. So I was able to update the value that was stored inside
  369. 29:28of our variable halfway through. And that's another cool thing is these variables, you
  370. 29:32can give them one value and then later on in the program, you can change that value.
  371. 29:36So that's really the basics of working with variables, we really learn two things, we
  372. 29:39learn how to create variables, and we learn how to use them inside of like a print statement
  373. 29:44over here. Now there's a lot more to learn with this. And actually in the next tutorial,
  374. 29:48I'm going to be teaching you guys about the different types of information that we can
  375. 29:52store inside a variable. So over here, we stored a string of text and we also stored
  376. 29:56a whole number which is called an integer but there's a few Other types of data that
  377. 30:01we can use and work with. So in the next tutorial, we're going to learn about all of that stuff.
  378. 30:09In this tutorial, I'm going to talk to you guys about data types in C sharp. Now in C
  379. 30:14sharp, we're gonna be dealing with all different types of data and information. And generally,
  380. 30:18when you're writing programs, there's going to be all different types of information that
  381. 30:21you're going to be working with. So I want to talk to you guys today about the different
  382. 30:25types of information and the different types of data that we can represent and work with
  383. 30:30in C sharp. So down here, I'm basically going to be showing you guys all the different data
  384. 30:37types that we can use. And I'll be doing that by creating variables. So a variable is just
  385. 30:41a container where we can store a data value or a piece of information. And I'm going to
  386. 30:46be creating different variables that can store the different types of data that we can work
  387. 30:51with in C sharp. So the first type of data that we can work with is plaintext. So anytime
  388. 30:56you want to represent or, you know, use just regular old text inside of your programs,
  389. 31:01you want to use something called a string. So I'm just gonna say string. And I'll just
  390. 31:04call it phrase. And I'm just gonna set it equal to a string. So a string is basically
  391. 31:08just denoted using an open and closed quotation mark. And then inside of the quotation marks,
  392. 31:14you can just put whatever string you want to represent. So in here, I could put like,
  393. 31:18draft Academy, and now this is going to be a string with the text draft Academy inside
  394. 31:24of it. So that's a string, and that's going to come in handy a lot. Another type of data
  395. 31:28that we can store is instead of a string of text, just going to be one single character.
  396. 31:34So I want to create a character, I can just say char, and maybe this could be like the
  397. 31:39grade you get on a test or something. And then when I create a character, I want to
  398. 31:43use these single quotation marks, just like that. And then inside here, I can put one
  399. 31:48single character, so I could put like an A, or a B, or a C, or something like that. But
  400. 31:54the important difference is that with a character, I can only put one single character, I can't
  401. 31:58put a bunch of stuff, I can only put one, so any single character is going to work.
  402. 32:03But you need to make sure that there's only one in there, when you need more than one
  403. 32:07character, then you want to move up to the string. Alright, so that's the two ways we
  404. 32:12can store plain text. So things like characters and you know strings of characters, we can
  405. 32:17also work with numbers, and the most basic type of number we can create is called an
  406. 32:21integer. And an integer is basically just a whole number. So it's one of the counting
  407. 32:26numbers like 12345, basically, an integer doesn't have any decimals in it. So I can
  408. 32:31just say, I n t like that int. And this could be like someone's age, for example. And so
  409. 32:37maybe someone's like 30. And you'll notice that when I create a number, like any numbers,
  410. 32:42or you know whether it's an integer or another type of number, you don't need quotation marks
  411. 32:46around it like you do up here. So you can just type out the number as is. And you can
  412. 32:50do whole numbers like this, like an integer, we could also do negative numbers, and you
  413. 32:55can just put the minus sign right in front of it, and C sharp is going to be able to
  414. 32:58handle negative numbers just fine. In addition to integers, we can also represent decimal
  415. 33:03numbers. So a decimal number would be you know, like 60.3 or something like that. And
  416. 33:08instead of storing inside of an integer, we can actually store it in one of three data
  417. 33:13types. So there's actually three different data types that we can use to represent decimal
  418. 33:17numbers. And they're called, there's a float, a double, and a decimal. And these basically
  419. 33:23allow you to store decimal points more and more specifically, so These range from least
  420. 33:28accurate with the float to most accurate with the decimal data type. So if you really want
  421. 33:33to be super, super, super precise with the decimal point that you're using, you want
  422. 33:37to use a decimal point and this would be used for things like money, or you know, really
  423. 33:41anything that you need to be extremely exact with you want to use a decimal, a float is
  424. 33:46going to be less precise. So it's going to be able to be taken to, you know, a less precise
  425. 33:51amount of decimal places. And then double is kind of just like right there in the middle.
  426. 33:55For the purposes of this course, I think for the most part, we're just going to be using
  427. 33:58a double. I think for most use cases, a double is going to be just fine. But like I said,
  428. 34:03if you're doing something you know, where you need to be extremely accurate, like with
  429. 34:07money or something, then you want to use that decimal. So I'm going to say double and I
  430. 34:11can give this a name. So we can make this like a GPA, right? Someone's GPA might be
  431. 34:16like a 3.2. So this is a good example of a decimal number, you can also make these just
  432. 34:23normal numbers, so I can make it like 3.0. But anytime I'm using a decimal number, I
  433. 34:28need to include this point zero in there. So that's um, integers and doubles. And those
  434. 34:33are the two main types of like I said, numbers, we're gonna be able to represent whole numbers
  435. 34:37and then decimal numbers. And then we kind of touched on like the different types of
  436. 34:42decimal numbers. So after characters and numbers, we just have one more like core data type
  437. 34:49that we're going to be representing which is going to be called a Boolean. And a Boolean
  438. 34:53is essentially just a true or a false value. Now, a lot of times in programming, we're
  439. 34:59actually going to Want to be able to store true false information. And this might not
  440. 35:03be something that is super intuitive to you if you've never programmed before, but a lot
  441. 35:07of times storing information in the form of a true or false value is going to come in
  442. 35:11handy. So if I want to create a Boolean, I can just say b o l, and Boolean is just a,
  443. 35:17you know, as it essentially just means a true or false value. So I can say b, o l, and I
  444. 35:21can give this a name. So we can call this like, is male. And this Boolean will basically
  445. 35:26tell us whether or not someone is a male. So I could say true if we're a male. Or you
  446. 35:32could also say false. But those are the only two values that they should have, it should
  447. 35:36either be true, or it should be false. That's it. And that's basically why Boolean 's are
  448. 35:41useful because they can only have two values. So for the most part, I would say these are
  449. 35:47the core data types that you're going to be using in C sharp. Now there are a few other
  450. 35:52data types that we could get into, they're a little bit more obscure, and they're not
  451. 35:56used as commonly as these data types. For the most part, you're going to be fine with
  452. 36:01just strings, characters, numbers, and then Boolean values 99% of the stuff that you want
  453. 36:08to do, you're going to be able to do with just this data. And as a beginner, you only
  454. 36:12really want to concern yourself with these data types. Now I do want to point one more
  455. 36:16thing out is you can represent these data types outside of variable. So if I came down
  456. 36:21here and said, like console dot write line, I don't have to like store this type of data
  457. 36:28inside of a variable. If I don't want to like I could just print out like a string like
  458. 36:32hello down here, like this isn't actually stored inside of a variable, or I could print
  459. 36:36out like a number. Like if I wanted to print out 30, I could do that. And then when I run
  460. 36:41my program, you can see we printed out just fine. These are what we would call constants.
  461. 36:46And a constant is basically just like a value just like this 30. It's not stored in a variable,
  462. 36:52we're not keeping track of it anywhere, it's just kind of written out. And we can do the
  463. 36:55same thing for Booleans. So obviously, you don't have to store this information inside
  464. 37:00of variables. A lot of times it's useful to do that. But if you don't want to, you can
  465. 37:04just kind of use it like that. So that's the basics of data types in C sharp, and these
  466. 37:09are going to come in handy a lot really, with just these types of data up here, you can
  467. 37:13write a bunch of awesome programs. In this tutorial, I'm going to talk to you guys about
  468. 37:22strings in C sharp. Now strings are one of the most useful types of data that you're
  469. 37:27going to be working with in C sharp. So in this tutorial, I want to just show you all
  470. 37:31the ins and outs of working with strings, we're gonna look at some of the cool things
  471. 37:34that you can do with them, and how you can really leverage the power of strings inside
  472. 37:39of your programs. So down here, you'll notice that I just have this little console dot write
  473. 37:43line instruction. And basically, this is just going to write out some text onto the screen.
  474. 37:47So if I wanted to create a string, I can just make these open and close quotation marks.
  475. 37:52And then in here, I can type out some text. So I could type out like draft Academy. And
  476. 37:56now when I run my program, it's going to go ahead and print that out onto the screen.
  477. 38:01So anytime that I want to represent some sort of like plain text, or really any type of
  478. 38:05text in my program, I can do it with a string. And there's actually some cool things you
  479. 38:09can do with these strings. So let's say for example, that I wanted to print out draft
  480. 38:14Academy, but I wanted to print out draft on one line, and then Academy on the next line,
  481. 38:18I can use something called a new line character. So I could actually say backslash, N. And
  482. 38:23this is a special character that we can use in C sharp strings, which is basically going
  483. 38:28to specify that we want to type out draft, then a new line, then Academy. So now when
  484. 38:33I run my program, you'll see that draft and Academy are printed out on different lines.
  485. 38:37So that can come in handy when you're writing out your strings. And there's also another
  486. 38:41way, so if let's say that I wanted to write out a quotation mark, if I put a quotation
  487. 38:46mark here, you'll notice that this quotation mark effectively ends this string. And then
  488. 38:52this over here is just causing an error, I want to actually print out a literal quotation
  489. 38:57mark, that's not you know, delineating the string, I can just use that backslash again.
  490. 39:02And now we'll be able to print out that quotation mark, no problem, just like that. So those
  491. 39:06are some cool little things that we can do inside of our strings. You can also take a
  492. 39:10string and store it inside of a variable. So a variable is just a container that you
  493. 39:15can store different pieces of information. So if I wanted, I can create a variable over
  494. 39:19here to say string. And we can just call it like phrase. And I'm just going to set it
  495. 39:23equal to this text over here, which is just going to be draf Academy. And then down here,
  496. 39:29I could just print out the value stored inside of this variable. So I can print out the value
  497. 39:34stored in the phrase variable. And we're still printing out draft Academy. So that's kind
  498. 39:39of cool. Another thing you can do with strings is called concatenation and end cap and concatenation
  499. 39:44is the process of taking one string and then appending another string onto the end of it.
  500. 39:50So you're essentially just adding two strings together. So for example, over here, I could
  501. 39:54say string phrase is equal to draft Academy. And then I could use this plus sign and then
  502. 39:59this is basically going to add another string on to that. So I could say like, is cool.
  503. 40:06And now when I print out phrase it's going to be printing out draf Academy is cool. So
  504. 40:10it basically just added those two strings together, we would call that concatenation.
  505. 40:15And that can be really useful. Another thing we can do in our programs is find out information
  506. 40:19about our strings. So sometimes, you might want to figure out different pieces of information
  507. 40:23about the string, figure out some attributes about it. So let's say we wanted to figure
  508. 40:27out how many characters were inside of this string, I could just say phrase dot length,
  509. 40:33and that's with a capital L. And now what this is going to do is, it's actually going
  510. 40:37to tell me how many characters are inside of this string. So now we should get like
  511. 40:4215. Yeah, cuz there's 15 characters, inside of draft Academy. So that's a really cool
  512. 40:47way, we can find out some information about the string. In addition to finding out information,
  513. 40:52we can also use things called methods. And we're going to talk more about methods in
  514. 40:57a future tutorial. But essentially, all you need to know right now is that a method is
  515. 41:01basically a little block of code that we can call, which is going to perform a specific
  516. 41:06task. So these are basically like little predefined snippets of code that we can sort of tailed
  517. 41:13to execute, and then they'll do something. So we can use methods on our strings. And
  518. 41:17they'll either give us information about our strings, or they'll modify our strings in
  519. 41:22some way. So over here, if I want to use one of these string methods, I can just say the
  520. 41:27name of the string, or I could also type out like a literal string like that. And then
  521. 41:32I can just say.so, I'm going to say phrase, dot. And now I have access to all of these
  522. 41:38different methods. So if I want to use one, I can just type out the name of the method.
  523. 41:41And I'll show you these can kind of do little things with our string. So let's say that
  524. 41:46I wanted to take this string over here and convert it into all uppercase, well, there's
  525. 41:50actually a method that's going to be able to do that for us. So I could say phrase.to,
  526. 41:54upper, and then we can make an open and closed parentheses. And now this is actually going
  527. 42:00to take this string over here, convert it into uppercase, and then print out the uppercase
  528. 42:05string onto the console. So now when I run the program, you'll see we get draft Academy,
  529. 42:10all in caps, you can also do the same thing for lowercase. So I can say like to lower
  530. 42:15and now to do the same thing. So it should all be entirely lowercase now, which it is.
  531. 42:20So that's one way that we could like modify the value of a string, we can also find out
  532. 42:25some more information about this string. So imagine we wanted to figure out if this string
  533. 42:30of text contained a certain like substring of text, right? So maybe we want to see if
  534. 42:36this string had the word Academy in it, I could say phrase dot contains. And now what
  535. 42:42I'm actually going to do is I'm going to give this contains method, a piece of information.
  536. 42:47And this is called a parameter. So this over here, this contains method will tell us whether
  537. 42:52or not this phrase string contains like a certain set of characters or something. And
  538. 42:59in order to use it, I have to tell it, what I'm looking for. So I can say phrase dot contains.
  539. 43:04And in here, I could say, like, Academy, and this is basically going to return a true or
  540. 43:08false value telling me whether or not the phrase draft Academy contains the word Academy.
  541. 43:15And we should get a true value back because it does. So you can see over here we get true.
  542. 43:20But if I set over here, like academies, now all of a sudden, we should get false back.
  543. 43:26Because that string is not included in the phrase string, it's not included in draft
  544. 43:31Academy. And that can be kind of useful, you can like tell whether or not a certain string
  545. 43:36contains another string or another character. Another thing we can do is figure out the
  546. 43:41different characters inside of a string. So I'm going to get rid of that. Let's say that
  547. 43:45I wanted to figure out what the first character in my string was. Right? So let's say there's
  548. 43:49a situation where I have a string like phrase, and I wanted to figure out what's the first
  549. 43:53character in that string? Well, I can actually access individual characters and print them
  550. 43:59out. So I could say like phrase and I can make an open and closed square bracket. And
  551. 44:04inside of here, I can just put the index of the character that I want to print out inside
  552. 44:09of the string. So if I put a zero here, this is gonna tell me what this first character
  553. 44:14is. So phrase, square bracket zero should be printing out a capital G. And you see over
  554. 44:19here it does, if I wanted to figure out what the third character in the string was, I can
  555. 44:24say 012. So I can say two. And now this should print out that are for us. And you can see
  556. 44:33we get our so if you haven't caught on by now when we're indexing a string, we actually
  557. 44:38start at zero. So if I was going to assign index positions to each of the characters
  558. 44:43inside of this string, I would say that the G character is at index position zero. The
  559. 44:49eye character is an index position one the our character as an index position to the
  560. 44:54a character is an index position three, etc. So the first character this G is is actually
  561. 45:00an index position zero inside of the string. And that's basically just a way that C sharp
  562. 45:06and actually a way that most programming languages index strings. So when I say phrase zero,
  563. 45:13this is actually referring to this first character. So let's see if we could print out this a
  564. 45:19over here, well, it's basically going to be 012345678. So this is going to be an exhibition
  565. 45:26eight. So if I put an eight in here, now we should be able to print out that capital A,
  566. 45:33which we do over here. So that's sort of how string indexes work. And using this information,
  567. 45:39we can use another string method. So again, I'm just going to say phrase, dot and I want
  568. 45:44to show you guys another method that we can use it's called index of an index of is basically
  569. 45:50going to tell us not only if a string contains a certain value, but it'll tell us at what
  570. 45:55index position that value is located inside of the string. So for example, if I came over
  571. 46:00here and said, Academy, this is going to tell me at what index position Academy starts inside
  572. 46:08of this string. So when I run my program, you'll see that we're getting an eight over
  573. 46:13here, and we just sort of saw that, that capital A character started at index position eight.
  574. 46:19So basically, what this is saying is that Academy starts at 012345678. So it starts
  575. 46:27here, in addition to passing in a substring, like that, I could also pass in a character.
  576. 46:32And remember, a character is just an open and closed, single quotation mark. And then
  577. 46:37inside here, I could put like an F. And now this will tell me the location of the first
  578. 46:42f inside of this string, so it'll give me that index, you can see we get four. And that's
  579. 46:48because 01234, this starts at a four. And that can be pretty useful, there's also going
  580. 46:55to be the situation where the character isn't in there. So if I put a z in here, you'll
  581. 47:00notice that there's no z up here. So in this case, we're actually just going to get a negative
  582. 47:04one back and a negative one is always going to signify that the character is not inside
  583. 47:09of the string. So that can be pretty useful. So that's how we can use phrase dot index
  584. 47:14of, and that'll come in handy a lot. There's also one more of these string methods that
  585. 47:18I want to show you. And it's called substring. So I can say phrase, dot substring, I can
  586. 47:23make an open and close parentheses. And I'm actually going to need to give this a value.
  587. 47:29So basically, what this will allow me to do is it'll allow me to grab a part of this string,
  588. 47:35so I can grab like, one part of the string and then print that out onto the screen. And
  589. 47:39we can give this one parameter, we can give this one argument, in other words, one piece
  590. 47:43of information. And that's basically the index where we want to start grabbing the string
  591. 47:48at so let's say that I only wanted to grab this academy word and print this out, well,
  592. 47:53remember, Academy starts at index position eight, we already saw that. So this capital
  593. 47:57A is actually index position eight in the string. If I put an eight in here, this is
  594. 48:02now going to grab the characters starting at eight and going to the end of the string.
  595. 48:06So now, we should just print out Academy, which we do down here. You can also in addition
  596. 48:13to just passing in the starting index, you can pass in a length. And you'll notice that
  597. 48:17I'm just using a comma here, and now it's going to delineate that I want to pass in
  598. 48:21another value. So I'm passing in the starting index. And I can also pass in how many characters
  599. 48:26I want to grab. So I can say I want to start grabbing the characters at eight. And let's
  600. 48:31say that I only want to grab three characters. So over here, I can put a three. And now we're
  601. 48:36going to start at eight, and we're going to grab 123 characters, so we should get ACA
  602. 48:41printed out onto the screen. And you can see that we do so that substring method can come
  603. 48:47in handy. So that's kind of the basics of working with strings. And you know, so as
  604. 48:51soon as you guys kind of how we can work with them different things we could do like concatenation.
  605. 48:54And we can also print out things like new lines, we also looked at getting information
  606. 48:59about a string, like the length, and you know, individual characters in the string, we talked
  607. 49:03about the string indexes, we also talked about different methods that we can use to do different
  608. 49:08things with our strings. So hopefully, that kind of gets you up to speed with why strings
  609. 49:13are awesome and how they're useful. And to be honest with you, there's a lot more of
  610. 49:16these little methods that you can use. And if you want, you can just go online and Google
  611. 49:20C sharp string methods and you'll find like a huge listing of you know, probably at least
  612. 49:25you know a few dozen of these different methods that you can use to either find out information
  613. 49:30about or modify your strings. In this tutorial, I'm going to talk to you guys about numbers
  614. 49:40in C sharp a numbers are one of the most common types of data that you're going to be working
  615. 49:45with in your C sharp programs. So in this tutorial, I just want to kind of give you
  616. 49:49guys a full walkthrough of you know how to use numbers, the different types of numbers
  617. 49:53that we can use, and we're also gonna look at some cool things that we can do with numbers,
  618. 49:57things like you know, complex math operations. And we're just dealing with numbers in general.
  619. 50:02So it's gonna be pretty cool down here, I just have a basic program, it just is basically
  620. 50:07writing out a line of text. So if I wanted to, for example, like write out a number,
  621. 50:11I can just represent a number by just typing it out. So I could say, like 40, or 20. Or
  622. 50:16I could say, like negative 5.763, really any type of number I can work with. And there's
  623. 50:23really two main types of numbers, there's a whole number, which we would refer to as
  624. 50:28like an integer, and then there's a decimal number. So those two different types of numbers
  625. 50:32are going to be really useful in our C sharp programs. But it's important that you note
  626. 50:36that C sharp does distinguish between those two types of numbers. So there is a big difference
  627. 50:43between working with whole numbers and working with decimal numbers. And it's something that
  628. 50:47you just want to be aware of, as you sort of go through and write your programs. And
  629. 50:50so you know, basically, I could just type in a number here like 50, and then I'd be
  630. 50:54able to print it out onto the screen just over there like that. So numbers are pretty
  631. 50:58easy to work with. We can also do math with numbers, so I could say like five plus eight.
  632. 51:04And now instead of printing out five plus eight, like the literal text five plus eight,
  633. 51:09C sharp is actually going to go ahead and perform this math operation for us. So it's
  634. 51:14going to print out the result of adding five and eight together. So we're going to get
  635. 51:1713 over here. And we can use addition like that, we could also use subtraction, which
  636. 51:23is just that hyphen, we could use a forward slash, which would mean division. And we could
  637. 51:28also use a Asterix, which is going to represent multiplication. So any of those different
  638. 51:33operations, the four basic math operations you can use, we can also use another operator,
  639. 51:38which is called the modulus operator. So for example, if I could say, like, five, and then
  640. 51:44I can put a percent sign here, and I can say two, and we would read this as five mod two,
  641. 51:50and this modulus operator right there, essentially, what this is going to do is it's going to
  642. 51:54take five, it's going to divide it by two, and it's going to give us the remainder. So
  643. 51:59five divided by two is going to be two with a remainder of one. And that's exactly what's
  644. 52:03going to get printed out. So we should just print out one over here, which we do. So sometimes
  645. 52:08you might be interested in getting the remainder of a particular division operation. And this
  646. 52:13is going to go ahead and do that for us. We can also represent like mathematical equations.
  647. 52:18So one thing we can do is specify order of operations. So if I was to say like four,
  648. 52:24plus two, times three, actually, what's going to happen is the multiplication is going to
  649. 52:31happen first, and then this addition is going to happen. So it's going to be three times
  650. 52:35two, six, plus four, so we should get 10. And that's just sort of following normal order
  651. 52:41of operations rules. But if I wanted to change up the order of operations, I can use parentheses.
  652. 52:46So I could say like four plus two, which is going to be six, and now it'll be six times
  653. 52:52three. So we're going to get 18, instead of getting that 10. So you can use those parentheses
  654. 52:56to sort of change around the order that different calculations are getting performed. And those
  655. 53:01can be pretty useful. I also want to talk to you guys about the difference between using
  656. 53:06integers and using decimal numbers. So like I said, an integer is just a whole number,
  657. 53:10a decimal number would be, you know, a number that has a decimal on it, something like that.
  658. 53:14And actually, like I said, C sharp does distinguish between these two types of numbers. So if
  659. 53:19you're adding or subtracting or multiplying or doing some sort of mathematical operation
  660. 53:24with an integer, and a whole number, sometimes the results you get are going to be a little
  661. 53:28bit different. So if I was just to say, like 5.0 plus 8.1, if I was to perform a math operation
  662. 53:36with two decimal numbers, then I'm going to get a decimal answer back. So I'm going to
  663. 53:39get like 13.1. But if I was to perform a mathematical operation with an integer and a decimal, so
  664. 53:46five plus 8.1. And you'll notice that this is now an integer because it doesn't have
  665. 53:49a decimal point. Again, we're going to get a decimal back. So I'm going to get a decimal
  666. 53:53number back just like that. But when we do math operations with two integers, then we're
  667. 54:00always going to get an integer back. So if I said five divided by two, and you'll notice
  668. 54:05that these are both integers, I'm actually going to get an integer answer back. So really,
  669. 54:10this should be like two point something right should have some long decimal point. But when
  670. 54:14I actually run my program, you'll see that I'm getting an integer value back, I'm getting
  671. 54:18a two back. But if I was to convert this to a double, so if I said 2.0. So now this is
  672. 54:26going to be a decimal number, we're going to get a decimal answer back. So that's basically
  673. 54:31how that works. So any math operation that you do with two integers, no matter what you're
  674. 54:36going to get an integer back, but if you do a math operation with a decimal and an integer,
  675. 54:40then you'll always get a double back. And that'll kind of come in handy as you're working
  676. 54:43with these different types of numbers. Another thing we can do is we can store numbers inside
  677. 54:48of variables. So I can create a variable up here let's create one for int. So I'm just
  678. 54:52gonna say int num and I can give this a value of like six right so now this container num
  679. 54:58is storing the value of six And then I can print that out just like I normally would.
  680. 55:02So I can just say num down there. And it'll print out that six. There's another cool operation
  681. 55:07which we can do, which will basically allow us to increment this number. So down here,
  682. 55:11I can say num plus plus. And basically, what this is going to do is it's going to add one
  683. 55:17to the number. So now instead of being six, since we're adding one to the number, this
  684. 55:21is going to be seven. So when I run my program, you see we'll get seven. And you can do a
  685. 55:26similar thing with minus minus, so I could say like num, minus minus, and now instead
  686. 55:31of getting a six, we would get a five. And those are going to come in handy, just as
  687. 55:35like a shorthand as we go through our C sharp journey. So those are sort of the basics of
  688. 55:42you know, working with numbers. And you can see we can store them in variables. And we
  689. 55:45talked about, you know, interacting between integers and decimal numbers. And I also want
  690. 55:50to show you guys some other stuff we can do. So a lot of times in our programs, we're going
  691. 55:54to want to be able to perform different math operations. So there's all sorts of math operations,
  692. 55:59you know, like using exponents, and using the square root or you know, even doing like
  693. 56:04logarithmic stuff, or sine, cosine, tangent stuff, like there's all sorts of math things
  694. 56:08that we might want to be able to do inside of our programs I'm gonna show you guys, there's
  695. 56:12these things called methods. And a method is basically just a block of code that's going
  696. 56:17to perform a specific task. So I can do what's called call a method. And when I call a method,
  697. 56:23it's basically going to like perform a specific task or a specific function for me. And there's
  698. 56:29a bunch of these different math methods that we can use in our C sharp programs. And the
  699. 56:34way that we can get access to them is just by typing out math with a capital M. And then
  700. 56:38I can say, dot, and now I have access to all these different math operations. So you'll
  701. 56:43see down here, I can like scroll down through this list, and there's all these different
  702. 56:47things that I can do, I'm going to show you guys a couple of these cool math methods is
  703. 56:52what they're called. And you can kind of see what they do. So one is just math dot abs,
  704. 56:56and the stands for absolute value. So I can make an open and close parentheses and inside
  705. 57:01of here, I'm going to put a number. So if I said like negative 40, this is going to
  706. 57:05give me back of the absolute value of negative 40, which is just going to be 40. As you can
  707. 57:10see, there's another one, which is called pow, and this stands for power. So I'm actually
  708. 57:16going to be able to give this two numbers and it's going to spit out the first number
  709. 57:21taken to the second number. So if I said like three and then to over here, this is going
  710. 57:26to give me back three raised to the second power. So we should get nine. And you can
  711. 57:32see we do and that's this will also work with like decimal numbers. So I can say like 3.8
  712. 57:38raised to the second power. And now we should be able to run this as well. So 14.44. And
  713. 57:42you can also do the opposite. So we could say like Sq RT, this is going to Stanford
  714. 57:48square root. So if I put like a 36 in here, now, I'm actually going to get back to the
  715. 57:56square root of 36. So we should get six back. And we do, there's also another useful method,
  716. 58:03which is called min max. And with Max, you can actually pass two numbers into these parentheses.
  717. 58:09So I could say like four and 90, and Max is actually going to spit out whichever one is
  718. 58:14bigger. So it'll tell us which number is the biggest. And so this should give us 90, as
  719. 58:20you can see down there. And that can come in handy, there's going to be a lot of situations
  720. 58:23in C sharp, where you might have two numbers, maybe they're stored inside of variables,
  721. 58:29and you don't necessarily know which one is bigger. So you can use that math function
  722. 58:33to figure that out. There's also another one called min, and that just does the opposite.
  723. 58:37So now this will give us the smaller of the two numbers. So we're going to get four back.
  724. 58:42There's one more that I want to show you guys. It's called math dot round. And this is a
  725. 58:46really great way to round numbers. So when I say math dot round, I can pass this like
  726. 58:52a decimal number like 4.3. And this will round it just according to standard rounding rules.
  727. 58:57So you can see, we get four. But if I was to up this to like 4.6, now we're gonna go
  728. 59:03ahead and get a seven or a five back. So we get the five back there. So those are just
  729. 59:08some basic, you know, math operations that you can do. And you can see I'm using this
  730. 59:12Visual Studio text editor when I just say math, and then I click this dot, you can see
  731. 59:17all of these different options are showing up down here. So there's all these different
  732. 59:22methods that we can call, I mean, there's all sorts of stuff we can do. And you can
  733. 59:26kind of just like play around with each of these, like, see what they do. But for the
  734. 59:30most part, like any major math operation that you're going to want to be able to do, there's
  735. 59:34usually going to be a method for it, or at least a method in here to help you with it.
  736. 59:39So you definitely want to check that out. And you can also just google like C sharp
  737. 59:42math methods on line and you'll find a huge listing of them. So that's sort of the basics
  738. 59:47of you know, using math and then also working with numbers. Again, numbers are extremely
  739. 59:51important. So you want to make sure that you're, you know, up to date and sort of up to speed
  740. 59:55with how to use them and how they interact with each other and all that stuff in the
  741. 1:00:02C sharp. In this tutorial, I'm gonna show you guys how to get input from a user in C
  742. 1:00:08sharp. Now one of the cool things about C sharp is we can actually have our programs
  743. 1:00:13interact with the user. So I can run my program. And I can actually like give it some information.
  744. 1:00:18And it can use that information to do different things. And when you think about it, most
  745. 1:00:23programs that you probably use on a daily basis are going to allow you to input information
  746. 1:00:27into them. And then depending on the information that you input, they're going to do different
  747. 1:00:31things. So today, we're going to look at the most basic form of that, which is just, you
  748. 1:00:36know, allowing the user to basically like type in a value, and then we can use that
  749. 1:00:39value to do something. So down here, I have a simple program set up, you'll see I'm just
  750. 1:00:44saying console dot write line, and I'm writing out Hello. And then down here throughout this
  751. 1:00:49core. So far, I've always included this line of code in our little programs. And basically
  752. 1:00:54what this line of code has been doing is it's been allowing us to see the output of our
  753. 1:01:00programs, right. So if I include this read line, when I run my program, that I can see
  754. 1:01:06what gets output so I can see Hello, right sort of stays on the screen. But if I was
  755. 1:01:11to get rid of this line, like if I remove this, when I click the start button, you'll
  756. 1:01:15see that that window, like pops up really quick and then disappears. And that's because
  757. 1:01:21we're essentially executing this line of code. And then once that line of code has been executed,
  758. 1:01:26there's nothing else to execute, right, there's no other instructions for the computer to
  759. 1:01:30like, carry out. So essentially, the program just terminates, and then that window disappears.
  760. 1:01:36We've been using this console dot read line instruction in order to keep that black window
  761. 1:01:42up on the screen in order to keep the console on the screen. Because what this line does,
  762. 1:01:47is it actually waits for the user to enter in a line of text. So this line essentially
  763. 1:01:53pauses the execution of our program, and waits for the user to enter something. So if the
  764. 1:01:59user hasn't entered something, then our program is essentially just paused. So actually, if
  765. 1:02:03you guys watch over here, my program is essentially paused. But if I was to enter something, and
  766. 1:02:09then click enter, the program terminates, right, because now this line of code down
  767. 1:02:14here has finished executing. But what's cool about this line is, like I said, it pauses
  768. 1:02:19the program. So we can sort of see what's happening. Another thing that this line of
  769. 1:02:23code does though, is it allows us to enter in a line of text. And we can actually use
  770. 1:02:28this instruction over here to get input from the user. So I'm going to show you guys how
  771. 1:02:33we can do that. In this tutorial, I want to basically design a little application, that's
  772. 1:02:38going to ask the user for their name, it's going to get their name, and then it's going
  773. 1:02:42to say hi to them. So I'll basically be able to type in my name, and the computer will
  774. 1:02:46be like, Hey, Mike, what's up. So the way we can do that is with a couple steps, the
  775. 1:02:51first thing we want to do is prompt the user for information. So I'm basically just going
  776. 1:02:55to write out a line and I'm just gonna say console dot write. And you'll notice before
  777. 1:03:02we were using console dot write line. Now essentially, the difference between write
  778. 1:03:06and write line is that when we say write line, this is going to print out a line of text,
  779. 1:03:12and then it's going to go on to a new line. So it's basically going to print out and then
  780. 1:03:15we'll go on to a new line, when I just say console dot write, it's not going to go off
  781. 1:03:20to a new line, it's just going to kind of print out on the same line. And you guys will
  782. 1:03:24see what I mean in a second. So I'm going to use console dot write because I'm basically
  783. 1:03:29going to be prompting the user. So I can just say, enter your name, and then I'm going to
  784. 1:03:34make a colon. So this is going to be our prompt for the user. Now the next thing that we want
  785. 1:03:41to do is we want to be able to allow the user to enter in a line of text. So I can do that,
  786. 1:03:46like I said, with console dot read line, just like that. And this is going to pause the
  787. 1:03:52program and wait for the user to enter in a line of text. But here's the thing, I don't
  788. 1:03:57just want to let them enter in the line of text, I want to be able to take the text that
  789. 1:04:01they enter in store it inside of a variable, right? Like if they just enter text, it's
  790. 1:04:07no use to me if I don't know what they entered. So I want to be able to store the value that
  791. 1:04:11they entered somewhere. And we can do that with a variable. So whenever we use this console
  792. 1:04:15dot read line, it's basically going to get the line of text that they enter as a string.
  793. 1:04:20So it's going to store the line of text that the user enters as a string of characters.
  794. 1:04:24So I'm going to create a variable. And we're just going to call this name and I'm going
  795. 1:04:29to set this equal to console dot read line. So I'm saying string name is equal to console
  796. 1:04:36dot read line just like that. Now the last thing I want to do is I'm actually going to
  797. 1:04:41write something out to the screen. So I'm just going to say write line. And over here,
  798. 1:04:47we're going to print out Hello, and then we're going to print out the name that the user
  799. 1:04:51entered. So I'm basically just going to print out Hello, plus name. So basically, the user
  800. 1:04:59is going to enter in Name. And then we're going to say hi to the user. And you'll notice
  801. 1:05:03I'm keeping this relined down there, just so we can see what the output is. So let's
  802. 1:05:09go ahead and run this program. And we'll see what happens. So over here, it says, enter
  803. 1:05:13your name. And you'll notice over here, it's letting me enter this on the same line as
  804. 1:05:18this prompt. And that's why we use that console dot write instead of console dot write line.
  805. 1:05:24So over here, I'm just gonna type in my name, my name is Mike. And now when I click enter
  806. 1:05:28the value, Mike is going to get stored inside of that name variable. And then we're going
  807. 1:05:33to be able to print it out on the screen. So you can see down here, we're printing out
  808. 1:05:36Hello, Mike. And that's essentially all we needed to do to get this program to work.
  809. 1:05:42So we use console dot read line, which pause the program and let the user enter in a line
  810. 1:05:47of text. When they hit enter, the value that they entered gets stored inside of this name
  811. 1:05:53variable, and then we printed it out onto the screen. So that's pretty cool. And we
  812. 1:05:58can actually do this as many times as we want. And so if I wanted, I could get another piece
  813. 1:06:01of information from the user. So I'm going to do that, I'm going to copy this, and we'll
  814. 1:06:05come down here. And this time, I'm going to ask them to enter in their age. So over here,
  815. 1:06:11we're going to create another string, and we're just going to call it age. And again,
  816. 1:06:15I'm just gonna say console dot read line. So now in addition to having their name, we
  817. 1:06:20also have their age. So I can say hello, name. And then I can basically tell them how old
  818. 1:06:25they are. So I can say you are, and then we'll print out their age. So now we're saying hello,
  819. 1:06:31whatever the name is, you are, whatever their ages. So let's go ahead and run this program.
  820. 1:06:36Now we're getting two pieces of information from the user. So it says, enter your name.
  821. 1:06:40So we can enter in like john, and let's say john is 40 years old. So now when I click
  822. 1:06:46enter, we're going to have two values. And we'll be able to print them out. So it says,
  823. 1:06:51Hello, john, you are 14. So that's sort of the basics of getting input from the user.
  824. 1:06:58And again, remember what I said, when I use console dot read line, it's taking whatever
  825. 1:07:03the user enters, and it's storing it as a variable. So this is always going to be a
  826. 1:07:07variable, it's always going to get the information that they enter as a variable. And that's
  827. 1:07:12why we had to store both of those pieces of information inside of strings. But there's
  828. 1:07:17actually a way that we can get other types of information like numbers from the user.
  829. 1:07:22And in the next tutorial, we're going to build a little calculator game. And I'm going to
  830. 1:07:25show you guys how we can actually get numbers from the user. But for now, that is sort of
  831. 1:07:31the basics of doing this. And you probably just want to practice with this and maybe
  832. 1:07:35make it a little application of your own. In this tutorial, I'm going to show you guys
  833. 1:07:42how to build a basic calculator in C sharp. So we're basically going to build a little
  834. 1:07:47program where the user can enter in two numbers. And then our program will add them together
  835. 1:07:52and spit out the answer onto the screen. So should be kind of cool. And we're also going
  836. 1:07:56to talk about getting numbers as input from the user. So this is going to be a pretty
  837. 1:08:01cool tutorial. So down here, before we start building this, I actually want to show you
  838. 1:08:06guys how we can convert a string into a number. And this is a technique that's going to come
  839. 1:08:12in handy for this application. Essentially, like if I had a string that had a number in
  840. 1:08:18it, so like 45, or like 45 point 80, what I could do is I can actually use a special
  841. 1:08:24method in C sharp that would be able to convert this into an actual number. And the reason
  842. 1:08:30that's useful is because like if I had console dot write line, like if I had two strings
  843. 1:08:38that had numbers inside of them, like 43, plus 56. If I tried to add these two together,
  844. 1:08:46this is just going to concatenate the strings. So you see over here, we get 4356. So it's
  845. 1:08:52not adding the two numbers that are in here together, it's just like concatenating the
  846. 1:08:57strings together. So in certain cases, you'll have numbers that are kind of trapped inside
  847. 1:09:01of strings. And actually, we're going to have that situation in this tutorial. And so you
  848. 1:09:06want to be able to get the numbers out of there so that you can like do math with them.
  849. 1:09:09So I'm gonna show you guys how we can convert a string into a number. So actually over here,
  850. 1:09:15why don't we come down here and we're going to make a number variable. So I'm just going
  851. 1:09:20to make an integer. And we'll just call this num and I'm going to set it equal to a string.
  852. 1:09:26So I'm going to set equal to a string that has a number in it like 45. You'll see over
  853. 1:09:31here, C sharp is yelling at us because this number is actually a string so it's not going
  854. 1:09:36to be able to be stored inside of an integer variable. So we need to convert this into
  855. 1:09:43a integer. And the way that we can do that is just by typing out, convert like that not
  856. 1:09:48end with a capital letter, and then dot and now we need to specify what we want to convert.
  857. 1:09:54So you'll see over here there's a bunch of options like to in 32, two and 16 to double
  858. 1:09:59to decimal to string, this little convert function is basically going to allow us to
  859. 1:10:05convert different values to other values. In our case, we want to convert this to an
  860. 1:10:08integer. So I'm just going to say convert.to, int 32. And that was one of the options that
  861. 1:10:14we had over here. And then I'm going to make an open closed parentheses around the actual
  862. 1:10:20string. So when I do this, this is going to go ahead and convert the string inside of
  863. 1:10:25here into an integer. And just to note, the string that we put in here has to have a number
  864. 1:10:31inside of it. Like if this was just something like that, this isn't going to work, we're
  865. 1:10:33actually gonna get an error. But if I have a number in here, like 45, then this is going
  866. 1:10:38to work just fine. So if I was to come down here, now I could actually use this as an
  867. 1:10:43integer. So I could say num, plus six. And now we'll actually be able to do this math
  868. 1:10:49operation. So you can see we get 51. And that's essentially how we can convert a string into
  869. 1:10:56an integer. And just so I can point this out, like if this didn't have a number inside of
  870. 1:11:00it. Or even if it did have a number inside of it, but the number had like text next to
  871. 1:11:05it, this isn't going to work. So we're actually going to get an error. And you can see the
  872. 1:11:09program wasn't able to run, because it says down here input string was not in correct
  873. 1:11:16format. So that's just something to keep in mind. And later in the course, we're actually
  874. 1:11:20going to talk about how we can fix something like this and mitigate it. But for now, we're
  875. 1:11:24just going to assume that the string is going to be formatted correctly. And I'm just going
  876. 1:11:27to exit out of this exception. Alright, so that's kind of an explanation of how we can
  877. 1:11:31convert a string into an integer. And we're actually going to be able to use this in our
  878. 1:11:35program. So like I said, we're going to build a calculator program that's going to allow
  879. 1:11:39the user to enter in two numbers. And then we'll take those numbers, add them together
  880. 1:11:43and print it out. So the first thing I'm going to do is just print out a prompt, so I'm going
  881. 1:11:47to say console dot write. And I'm actually just going to use right, and then over here,
  882. 1:11:52I'm just gonna say enter a number. And now we're prompting the user to enter a number.
  883. 1:11:57And what I want to do now is I want to be able to get the number that they enter. So
  884. 1:12:00I'm just going to say console dot read line. And this is going to go ahead and wait, it's
  885. 1:12:07going to pause the program until they enter in a line of text. And so what I actually
  886. 1:12:12want to do is I want to store the number that they enter inside of a variable. So I'm going
  887. 1:12:17to create a variable over here, and and we're just going to call it num one. And I'm going
  888. 1:12:21to set it equal to console dot read line. But you'll notice over here, we're getting
  889. 1:12:25an error. And the problem is that console dot read line is going to give us a string.
  890. 1:12:31So console dot readline takes whatever the user enters and turns it into a string. And
  891. 1:12:35that's sort of the value that we have. If we want to eventually add these numbers together,
  892. 1:12:40we're not going to be able to use a string like I showed you before. So we're actually
  893. 1:12:44going to have to convert this into an integer, which now we know how to do. So I can just
  894. 1:12:48say convert.to, and 32. And I'm going to surround console dot read line, this whole thing with
  895. 1:12:56parentheses. So now this whole thing is surrounded with these parentheses, and it's going to
  896. 1:13:01get converted into an integer. And that's basically all we need to do for that. So now
  897. 1:13:05I'm going to copy this and we'll be able to get the second number. So I'm gonna come down
  898. 1:13:08here, and I'll just say enter another number. And we're going to call this one, two. So
  899. 1:13:15now we're prompting the user for a number, we're getting that number and turning it into
  900. 1:13:20an integer. And we're doing the same thing down here. So the last thing we want to do
  901. 1:13:23now is just print out the answer. So I'm going to go ahead and say console dot print line
  902. 1:13:29or write line. And I'm just going to write out number one plus number two, because we
  903. 1:13:36converted these guys into integers, we're going to be able to add them together. So
  904. 1:13:40let's go ahead and run this program. And we'll see how we did. It says enter a number. So
  905. 1:13:44let's enter in a 10 and insert another number. So why don't we insert a three. And so now
  906. 1:13:50it's going to convert both of those into integers, and add them together and we get 13. So looks
  907. 1:13:55like the program's working out pretty well. There's actually a problem with this program,
  908. 1:13:59though, if I was trying to add a integer number with a decimal number. So if I said like four,
  909. 1:14:05and over here, I put in like a 2.6. This is actually not going to work. So you'll see
  910. 1:14:11the program isn't actually printing anything out. That's because the program is stalling,
  911. 1:14:16we didn't enter in a correct value, we inserted a decimal value and this can't be converted
  912. 1:14:22into an integer. So you'll notice that the program over here is stalling. And basically,
  913. 1:14:28that's because we entered in a decimal number. So if I wanted to allow this program to be
  914. 1:14:33able to handle decimal numbers, instead of converting these two integers, I'd want to
  915. 1:14:37convert them to doubles. So I could change both of these two doubles like this double
  916. 1:14:43double. And instead of saying convert dot two and 32 I could say convert.to double and
  917. 1:14:49same thing down here. And now we should be able to work with decimal numbers in our program.
  918. 1:14:57So let's see if we can do that. I want to add two And 3.6. And now we're able to add
  919. 1:15:04those two together. So in certain circumstances, you might want to have a situation where the
  920. 1:15:07user can only answer an integer numbers. But I think in most cases, you'd want to use doubles,
  921. 1:15:12just like I did down there. And you can see how easy it is to convert them into doubles
  922. 1:15:16instead of integers. So that's a very basic calculator. And that kind of gives you an
  923. 1:15:20idea of how we can get numbers as input from a user. In this tutorial, I'm going to show
  924. 1:15:30you guys how to build a little mad libs game in C sharp. Now, if you're not familiar with
  925. 1:15:35Mad Libs, it's basically a game where you can enter in a bunch of like random words
  926. 1:15:40and maybe like a couple nouns and adjective and adverb, and you'll take all those random
  927. 1:15:45words, and you'll kind of sprinkle them in throughout a story. And generally, because
  928. 1:15:48you're entering in random words, the story ends up being like pretty funny. So I actually
  929. 1:15:53have an example of a Madlib over here on my web browser, you'll see it's basically just
  930. 1:15:56a little story and you kind of sprinkle in different words like this one that's asking
  931. 1:16:01for an adjective or a plural noun or a celebrity name. So these are pretty fun. And I actually
  932. 1:16:06want to show you guys how we can build a Mad Libs for ourself in C sharp, it's actually
  933. 1:16:11really easy. So down here, I have a little program set up. It's basically just writing
  934. 1:16:16out this story. So it's writing out Roses are red, violets are blue, I love you. And
  935. 1:16:21this is sort of like a classic little poem. But I think this poem would be a lot better
  936. 1:16:25if we turned it into a Mad Libs. So how about instead of saying Roses are red, we would
  937. 1:16:31say roses are and we'll let the user choose a color. And instead of saying violets are
  938. 1:16:36blue, we'll let the user choose a plural noun. And instead of saying, I love you, we'll let
  939. 1:16:42the user choose a celebrity that they're going to love. And so will basically allow the user
  940. 1:16:48to enter in three random words, and then we'll sprinkle them in through our story. And hopefully,
  941. 1:16:54it'll be pretty funny. So let's think about how we can build a Mad Libs, essentially,
  942. 1:16:59what we're going to do is we're going to have to prompt the user for input, we're going
  943. 1:17:02to have to take that input and store it inside of variables. So let's go ahead and do that.
  944. 1:17:08And, essentially, what we're going to need is we're going to need three variables to
  945. 1:17:12store the color, the plural noun and the celebrity. So I'm actually going to show you guys one
  946. 1:17:18way that we can do that. And we're actually going to declare these variables first. And
  947. 1:17:23then we're going to give them value. So up here, I'm just going to say string. And I'm
  948. 1:17:26basically just going to tell C sharp that I'm going to want to use three variables in
  949. 1:17:30my program. So I'm going to want to use one called color, I'm going to want to use one
  950. 1:17:34called plural noun. And I'm going to want to use one called celebrity. And I'm just
  951. 1:17:40gonna put a semicolon after this. And if you've been following along with this course, we
  952. 1:17:44haven't necessarily been doing you know, exactly this, every time we were creating variables.
  953. 1:17:50But this is one way that we can create a variable. And we're basically just declaring the variables
  954. 1:17:55up here. And now down here, we're going to go ahead and give them values, and actually
  955. 1:18:00want to let the user give these variables their values, because the user is going to
  956. 1:18:04be entering in the color, the plural noun and the celebrity. So let's go ahead and write
  957. 1:18:09out some prompts. So I'm just going to say console dot write. And in here, I'm going
  958. 1:18:14to ask the user to enter a color. So I'm just going to say, enter a color. And after I prompt
  959. 1:18:21them, I want to wait for them to enter in the color. So I'm just gonna say, console
  960. 1:18:26dot read line. And this is basically going to pause the program and wait for them to
  961. 1:18:32enter in a line of text. But I want to take the line of text as they enter in store it
  962. 1:18:37somewhere. So I'm going to store it inside of this color variable. So I'm just going
  963. 1:18:41to say color, which we declared up here, and down here, I'm going to give it a value. So
  964. 1:18:45I'm gonna say color is equal to whatever the user enters. And we can basically do the same
  965. 1:18:50thing for the other two. So I'm going to go ahead and copy this. And down here, I'm just
  966. 1:18:55going to paste it. So I'm actually gonna make some new lines. And so now, instead of a color,
  967. 1:19:01we're going to want to get a plural noun. And then down here, instead of setting the
  968. 1:19:05color variable equal to whatever they enter, now, we're going to store this value inside
  969. 1:19:10of the plural noun variable. And then finally, down here, instead of asking for the color,
  970. 1:19:15we're going to ask for a celebrity. And we're going to store that inside of the celebrity
  971. 1:19:20variable. So now we've prompted the user to enter in those three things, we've stored
  972. 1:19:26all of their responses inside of variables. So the last step is to put those guys into
  973. 1:19:31our story. So over here, I'm gonna say roses are, and I'm gonna use the color. And then
  974. 1:19:39instead of just saying plural noun, I'm gonna say, plural noun, plus our blue. And then
  975. 1:19:46finally, we'll do the same for the celebrity. So I'm taking the variables and I'm sprinkling
  976. 1:19:53them in through this story. So depending on what the user enters, this story is actually
  977. 1:19:58going to be a little bit different. So Let's go ahead and we'll run this program and we'll
  978. 1:20:02see how we did. So over here it says enter a color. So why don't we enter in like magenta?
  979. 1:20:10Enter a plural. Now let's do microwaves and enter a celebrity, why don't we do like Tom
  980. 1:20:17Hanks. So all of these values that I entered are getting stored in the corresponding variables.
  981. 1:20:24And now when I click enter here, it should print all of them out inside of our Mad Libs.
  982. 1:20:30So I click Enter, and it says roses are magenta, microwaves are blue, I love Tom Hanks. So
  983. 1:20:36we actually built a fully functioning madlibs, right, I can go in, I can enter in any values
  984. 1:20:41that I would want, and those are gonna get sprinkled into the Madlib. Now over here,
  985. 1:20:46we built a very simple madlibs. Right, this was, you know, essentially three inputs, and
  986. 1:20:51then we put them into a very simple poem. But you can see how you can use this same
  987. 1:20:56technique in order to ask the user to enter in like maybe 10 or 20 different words, and
  988. 1:21:01you can make a way more complex story down there. So essentially, with very few lines
  989. 1:21:07of code, I mean, this is probably, you know, 10 lines of code, we were able to write out
  990. 1:21:11an entire Madlib in C sharp. In this tutorial, I'm going to talk to you guys about arrays
  991. 1:21:21in C sharp, a lot of times when we're running programs in C sharp, we're going to be dealing
  992. 1:21:25with all types of data, a lot of times we're going to be dealing with large amounts of
  993. 1:21:30data. And one way that we can manage and keep track of the data in our programs is by using
  994. 1:21:36variables. But the one problem with variables is that they can only store a single value.
  995. 1:21:42So I can create a variable like an integer variable or a string variable. But I could
  996. 1:21:46only store one integer or one string inside of that variable container. And a lot of times,
  997. 1:21:53if we're dealing with huge amounts of information, we're not going to want to have to create
  998. 1:21:57like hundreds or 1000s of variables. So it's actually another container, another type of
  999. 1:22:03data structure that we can use in C sharp, we'll just call it an array. And an array
  1000. 1:22:08is basically a structure that allows us to hold multiple pieces of information in the
  1001. 1:22:12same container. So unlike a variable that can only hold one value, an array can hold
  1002. 1:22:17multiple values. So you can hold 10 or 100, or 1000, or even a million values inside of
  1003. 1:22:22an array. So a good way to think of an array is just a collection of individual variables,
  1004. 1:22:27right, so an array can hold like 20 different values that you would normally have to put
  1005. 1:22:31inside of a variable. So I'm gonna show you guys how we can create arrays, we'll talk
  1006. 1:22:35about sort of the basics of using them. And I'll kind of get you guys up to speed with
  1007. 1:22:39using arrays in C sharp. So down here in my program, I'm going to go ahead and create
  1008. 1:22:45an array. And remember, an array is just a container that can hold a bunch of pieces
  1009. 1:22:49of information. So we create an array very similar to how we create a variable, the first
  1010. 1:22:54thing we have to do is specify the type of information or the type of data that the array
  1011. 1:22:59is going to hold. So we can use all the basic data types string in double Boolean, we can
  1012. 1:23:04use all those things. In our case, let's make an array of integers. So I'm just going to
  1013. 1:23:08say int, and then I'm going to type a space. Now whenever we're creating an array in C
  1014. 1:23:14sharp, we always need to let C sharp know that. So the way that we can tell C sharp
  1015. 1:23:18that we want to make a variable instead of an array is by making an open and closed square
  1016. 1:23:22bracket. And that's going to signify that this is going to be an array. And then over
  1017. 1:23:26here, I can type the name of the array that I want to create. So I can give this array
  1018. 1:23:30a name. So why don't we just call this like lucky numbers, we'll create an array that's
  1019. 1:23:35going to store a list of lucky numbers. In order to give this array some information.
  1020. 1:23:41There's actually a couple of different ways we can assign values to arrays, I'm going
  1021. 1:23:44to show you guys all the ways in this tutorial. But the most basic way is to just make an
  1022. 1:23:49open and closed curly bracket. And then inside of here, we can start typing out the values
  1023. 1:23:54that we want to put inside of our array. So I can just start typing out some numbers,
  1024. 1:23:59and we'll be able to store them all in the same container. So over here, I'm just going
  1025. 1:24:03to tell you like for a 15 1620 3042. So I'm just typing out a bunch of different integers.
  1026. 1:24:11And you'll notice that I have one value here, and then I separate it with a comma, and then
  1027. 1:24:17I can put another value. So all of these would be considered elements inside of the array.
  1028. 1:24:22So I would say like, four is the first element in the array, eight is the second element
  1029. 1:24:26in the array. And we can delineate the elements using these commas. And essentially, now we
  1030. 1:24:32have a container, we have this lucky numbers container, which is able to store all of these
  1031. 1:24:38values. And this makes it really easy for us to manage and maintain large amounts of
  1032. 1:24:43information. So I mean, over here, I only have like maybe six numbers, but if I wanted
  1033. 1:24:47I could put hundreds or 1000s or even millions of values inside of one of these arrays, and
  1034. 1:24:53they're extremely useful. So now that we looked at how we can create an array, let's talk
  1035. 1:24:57about how we can access the individual element inside of an array, right, because this array
  1036. 1:25:02is going to be no use to us unless we can actually get access to each of these values.
  1037. 1:25:07So I'm going to show you guys how we can do that. And I'm just going to do a console dot
  1038. 1:25:11write line. So we'll just print out particular value onto the screen. So if I want to access
  1039. 1:25:16a particular value, I can just type out the name of the array. So in our case, lucky numbers,
  1040. 1:25:21and then I want to make an open and closed square bracket. And inside of this square
  1041. 1:25:25bracket, I want to give a number so I'm basically going to specify the index of the element
  1042. 1:25:31that I want to print out or the index of the element that I want to access. So let's say
  1043. 1:25:35I wanted to access this element four right here, it's the first element in the array,
  1044. 1:25:40I'm going to put a zero here. So this four element is at the zeroeth position, it's at
  1045. 1:25:45the zero index inside of the array. So now when I run my program, you'll see we're printing
  1046. 1:25:50out four, so we're printing out that first element in the array. And so the way that
  1047. 1:25:56we index these array elements is starting with zero. So if you've been following along
  1048. 1:26:01with this course, you'll know that this is the same way we index strings. So I would
  1049. 1:26:05say that four is at index position, zero in the array, eight is at index position. 115
  1050. 1:26:11is an index position to 16 at three, etc. So we start counting. In other words, we started
  1051. 1:26:17the indexing at zero. So over here, I could say like lucky numbers, two, and now we're
  1052. 1:26:22going to get 012, we're going to get this 15 back. So we'll be printing out that 15
  1053. 1:26:28you can see over there. So essentially, when we access an element in the array like this,
  1054. 1:26:32it's a lot like we're accessing a variable. And it's actually you can think of each one
  1055. 1:26:36of these elements in here as their own kind of separate variables. And you can do everything
  1056. 1:26:41with them that you could do with normal variables. So another thing we could do would be to update
  1057. 1:26:45one of these elements. So for example, if I wanted, I could go ahead and change one
  1058. 1:26:50of the values over here. So let's say we wanted to change this element, we wanted to change
  1059. 1:26:54eight, we could actually just say, lucky numbers, and I'll make an open and close square bracket,
  1060. 1:27:00I'm going to put the index of the element that I want to change. So this eight is going
  1061. 1:27:04to be at index position one, and I can give us a new value. So we can give this a value
  1062. 1:27:08of like 900. Now if I print out lucky numbers, one, you'll see that the value will have updated.
  1063. 1:27:16So now we get the value of 900, instead of just getting the value of eight. So I'm able
  1064. 1:27:21to update these, I can really, you know, do anything that I could do with a normal variable
  1065. 1:27:26with each one of these individual values. And that's pretty cool. So in addition to
  1066. 1:27:30creating a, an array of integers, I could also create like an array of strings. So why
  1067. 1:27:36don't we do that I'm gonna make an array of strings. And I'm just going to call this friends.
  1068. 1:27:40So this will be like a list of maybe my friends or something. And I'm going to show you guys
  1069. 1:27:44another way that we can create an array like this. So you'll see up here right away, I
  1070. 1:27:49was giving this array A bunch of value. So right off the bat, when I created it, I was
  1071. 1:27:54populating this array with a bunch of information. But a lot of times when you create arrays
  1072. 1:27:59in C sharp, you're not going to know exactly what elements you want to put inside of them.
  1073. 1:28:05So if you don't know exactly what elements need to go in here, upfront, we can just create
  1074. 1:28:09an empty array. So I can actually just say, a string friends, and I'm just going to set
  1075. 1:28:14this equal to new string. And then I'm going to type in open and close square bracket.
  1076. 1:28:19And inside of these square brackets, I need to put a number and this number is going to
  1077. 1:28:24tell a C sharp, how many elements we want this array to be able to hold. So if we create
  1078. 1:28:29an array like this, and we don't give it elements like we did up here, we still need to tell
  1079. 1:28:34C sharp how many elements this array can hold, we need to basically tell C sharp, how big
  1080. 1:28:40we want to make the array. So over here, if I put 10 like that, that means that this friends
  1081. 1:28:45array is going to be able to hold 10 values. If I put three, that means the friends array
  1082. 1:28:50is going to be able to hold three values. So why don't we put five and down here, what
  1083. 1:28:55I can do is I can populate this array with information. So I could say like friends,
  1084. 1:29:00zero is equal to and then I could type out the name of a friend. So I could say like,
  1085. 1:29:05Jim, and then I could do the same thing for the other one. So I could say like friends,
  1086. 1:29:10one is equal to Kelly, etc. So I could do that for each element inside of this array
  1087. 1:29:15and individually populate it. And this is actually going to come in handy in a lot of
  1088. 1:29:19situations. And as we go forward in the course, I'm going to show you guys how we can do something
  1089. 1:29:23like that in a future tutorial. But for now, just know that this is another way that we
  1090. 1:29:27can create an array. Alright, so really, that is the basics of using arrays. Arrays are
  1091. 1:29:31extremely simple, but they're extremely powerful. It's essentially just a type of container
  1092. 1:29:36much like a variable. But unlike a variable, it can store multiple pieces of information.
  1093. 1:29:42And again, all of those pieces of information need to be of the same type, just like we
  1094. 1:29:46have up here. In this tutorial, I'm going to talk to you guys about methods in C sharp.
  1095. 1:29:56Now a method is basically just a little block of code where we can perform a specific task.
  1096. 1:30:02So a lot of times when you're writing programs in C sharp, you'll have like a few lines of
  1097. 1:30:06code, maybe like five or 10 lines, which are performing a specific task, like the whole
  1098. 1:30:11idea of having those lines in the program is that they're going to do a certain thing.
  1099. 1:30:16So what we can actually do is we can create something called a method, which will allow
  1100. 1:30:19us to take all that code and put it into its own little container. And then we want to
  1101. 1:30:25execute it and we want to perform the task, that code is going to carry out, we can just
  1102. 1:30:29call that method, and we'll be able to reuse it in different spots in our program. So I'm
  1103. 1:30:35gonna show you guys exactly how to create a method. And we're going to talk exactly
  1104. 1:30:38about what it does and how they're useful. So what I want to do in this tutorial is I
  1105. 1:30:43want to create a method, which is going to say hi to the user. So the task of this method
  1106. 1:30:49is going to be to say hi to whoever is using the program. So what I want to do when I create
  1107. 1:30:56a new method is I actually want to come down out here outside of these curly brackets.
  1108. 1:31:02So believe it or not this line of code up here where it says static void Main, this
  1109. 1:31:06is actually a method inside of our program. So like I said, a method is just a container,
  1110. 1:31:12or it's sort of like a block where we can put code that's going to perform a specific
  1111. 1:31:17task. And this main method is very special, because this is the method that gets executed
  1112. 1:31:24when we run our program. So any code that I typed down here in this main method gets
  1113. 1:31:29executed when I run my program. But in addition to writing this main method, we can also define
  1114. 1:31:35other methods that are going to work a little bit differently. But it's the same idea, we're
  1115. 1:31:40encapsulating a bunch of code inside of its own little container. So in order to create
  1116. 1:31:45a new method, I want to first go outside of this main method. So this main method is all
  1117. 1:31:49the code that I have highlighted, you can see it's delineated with this open and closed
  1118. 1:31:54curly bracket. So I'm going to come down here below this. And we can actually create a new
  1119. 1:31:58method. So when I create my method, I need to tell C sharp a couple of different things.
  1120. 1:32:04And the first thing we need to do is, say a keyword static. And I'm actually going to
  1121. 1:32:10talk a lot more about what static means in later tutorials. And as of right now, it's
  1122. 1:32:16really not super important as to what this is doing. Again, I will explain what this
  1123. 1:32:20is. But for now just know that we have to put static here, if we're going to put our
  1124. 1:32:24method alongside this main method. After I say static, I need to specify something called
  1125. 1:32:30a return type. Now, these methods are awesome, because they can perform specific tasks for
  1126. 1:32:35us. And one of the things they can do is they can actually give us back information. So
  1127. 1:32:40if I was to run one of these methods, it can actually give information back to me. And
  1128. 1:32:45we'll see how to do that. We're going to talk more about return types in the next tutorial.
  1129. 1:32:49But for now, I'm just going to say void, and that basically means that this method isn't
  1130. 1:32:53going to return any information. So this is kind of like the simplest method that we can
  1131. 1:32:57create. Alright, so after I say void, I want to give this method a name. Now remember,
  1132. 1:33:02methods are generally going to be performing a specific task. So you want to name the method
  1133. 1:33:08based off of the task that it's performing. Our method in this tutorial is basically just
  1134. 1:33:13going to say hi to the user. So I'm just going to call this Say hi. And generally in C sharp,
  1135. 1:33:20when we're creating a method, we're going to create it using a capital letter for the
  1136. 1:33:23name. So you can see I named it just like that you can also see up here, main was named
  1137. 1:33:28with a capital letter. And that's just a naming convention, that's not necessary. But a lot
  1138. 1:33:33of people are going to do that, then what we want to put in here is an open and close
  1139. 1:33:36parentheses, and then an open and closed curly bracket over there. And I'm just going to
  1140. 1:33:40go ahead and indent in for into this curly bracket. Any code that we put inside of these
  1141. 1:33:46two curly brackets is going to be considered part of the method. So any code that we put
  1142. 1:33:51in here will be run when we call our method. So I can essentially write all the code in
  1143. 1:33:56here to perform the task that the method is designed to carry out. Remember, this method
  1144. 1:34:01is just going to say hi to the user. So I can basically just print something onto the
  1145. 1:34:04screen, I'll say console dot write line. And why don't we just come over here and say,
  1146. 1:34:10Hello, user, our method down here has one line of code and methods can have as many
  1147. 1:34:16lines of code as you want. I mean, you can have, I think most methods have anywhere from
  1148. 1:34:19five to 10 lines of code. But you can have more or less than that, it doesn't really
  1149. 1:34:23matter. In my case, though, I just have a very simple method. So it's going to be one
  1150. 1:34:26line of code. Alright, so now what I want to do is go ahead and run my program, and
  1151. 1:34:32we'll see what happens. So I'm just going to click this start button. And you'll see
  1152. 1:34:37over here, nothing got printed out. So even though we created our new method, and we put
  1153. 1:34:42a line of code inside this method, it didn't execute this line of code. In other words,
  1154. 1:34:47this line of code never got executed by C sharp. Here's the reason. When we create a
  1155. 1:34:53method, this code isn't going to get run automatically, only the code that's inside of this main method
  1156. 1:35:00is going to get run when we run our program. So because this code wasn't inside of this
  1157. 1:35:05main method, it didn't get run. Now, if I want to execute all of the code inside of
  1158. 1:35:11this, say hi function, I can actually do something called the calling it. So I can call this
  1159. 1:35:17method. Basically, what that means is C sharp is going to go over to the method execute
  1160. 1:35:22all the code inside of it. So the way I can call the method is just by typing on its name,
  1161. 1:35:26I can just say, say hi. And I can make an open and close parentheses. And then now we
  1162. 1:35:31should be able to execute this code. So when I run my program, now, you'll see we're printing
  1163. 1:35:36out Hello, user, so the code inside the method got executed. Now, essentially, what's happening
  1164. 1:35:42is, whenever C sharp sees this line of code, what it does is, it jumps over, it goes over
  1165. 1:35:49to this, say hi method. And it's going to go through and execute all of the code inside
  1166. 1:35:54of there, then when it's done executing the code inside of here, it's going to come back
  1167. 1:35:58and it's going to move on to the next line of code in the main method. That's essentially
  1168. 1:36:03what's happening. So this is extremely useful. And there's going to be a lot of circumstances
  1169. 1:36:08where you're going to want to have methods that are doing different things. And you're
  1170. 1:36:11going to want to have all of that code inside of its own single container. But this is really
  1171. 1:36:17just scratching the surface of what we can do with these methods. And I want to show
  1172. 1:36:20you guys another thing we can do, we can actually give these methods information inside of this
  1173. 1:36:25Say hi function, I could actually specify that this Say hi function should take in some
  1174. 1:36:30information. And this is what we would call parameters or arguments. And a parameter is
  1175. 1:36:35basically just a value that gets passed into a method. So it's the value that we would
  1176. 1:36:40give to this method. And then it can use that value to perform its task better or do something.
  1177. 1:36:46And the way that we can specify a parameter is just by coming in here and enter the open
  1178. 1:36:49and close parentheses. And I can just basically specify what type of information I want to
  1179. 1:36:54accept. So let's say that instead of saying hello, user, we wanted this method to say
  1180. 1:36:59hi to a particular person, well, over here, I could specify string name. And now when
  1181. 1:37:06I write out this line, string name, I'm basically saying that the Say hi method needs to take
  1182. 1:37:12as a parameter as an argument, one value, which is going to be a name, then what I can
  1183. 1:37:17do is I can come down here and I can print out Hello, name, you'll see we're getting
  1184. 1:37:22an error up here, when we're calling this method. That's because I didn't include a
  1185. 1:37:28name when I called it. So now whenever I call this method, I have to pass it a string. So
  1186. 1:37:34I'm going to pass it a string Mike. And essentially, what's going to happen is this string Mike,
  1187. 1:37:39that I passed, this method is going to get stored inside of this string variable inside
  1188. 1:37:45of this name variable. And then I'm just going to print it out. So now, we should be printing
  1189. 1:37:49out Hello, Mike. And you can see we get that right there. So that's one cool way that we
  1190. 1:37:57can run these methods. And the other cool thing about methods is that I can call this
  1191. 1:38:02code as many times as I want. And so I can call it again and again, and each time pass
  1192. 1:38:06in a different name. So we'll pass in Mike, john, and Tom. And now each time we call this,
  1193. 1:38:13the method is going to do something slightly different. So it's going to say hi to someone
  1194. 1:38:18different. So now it's saying, whoops. Now it's saying hello, Mike. Hello, john. Hello,
  1195. 1:38:24Tom. So really, what we're doing is we're able to write this code in one place, and
  1196. 1:38:30then we're able to reuse it continually inside of our program. And that's another reason
  1197. 1:38:35why methods are awesome. In addition to just passing in one parameter, we could also specify
  1198. 1:38:40that this should take two parameters. So instead of just a name, why don't we also pass in
  1199. 1:38:44an integer age. And then down here, we can add the agent so I can say hello, name you
  1200. 1:38:51are and then we'll just print out the age. So now when I call these methods up here,
  1201. 1:38:58you'll see we're getting more errors, because I have to give these values. So I can say
  1202. 1:39:02like 3356 and 12. And now when I run my program, it's going to be able to handle that. So this
  1203. 1:39:12method is now printing out both the name and the age that we passed in. And really, you
  1204. 1:39:17can pass anything you want into these methods, you can pass strings and doubles. You can
  1205. 1:39:22even pass things like arrays, it doesn't really matter. You know, any value that you want
  1206. 1:39:27to work with, you can pass into here, as long as you can specify it over there. So that's
  1207. 1:39:32kind of the basics of working with methods. Now I will say you'll hear most of the time
  1208. 1:39:37people are going to call these methods you'll also hear people refer to these as functions.
  1209. 1:39:42methods and functions are essentially the same thing. There is a small difference, just
  1210. 1:39:46in the definition, but generally you can use the word method and the word function interchangeably.
  1211. 1:39:52And you'll hear a lot of people call these functions. You also hear a lot of people call
  1212. 1:39:56them methods, but generally in C sharp like the official term would be A method. So that's
  1213. 1:40:01really why methods are useful, you can kind of see, you know, we were essentially able
  1214. 1:40:05to reuse all the code that we wrote down here. So I can reuse it and give it different information
  1215. 1:40:11and it'll do different things. Now, in the next video, I'm going to talk to you guys
  1216. 1:40:16about this value over here, which is going to be the return value. So in addition to
  1217. 1:40:20passing information into methods, methods can also pass information back to the callers,
  1218. 1:40:26and we're going to talk about that in the next tutorial. In this tutorial, I'm going
  1219. 1:40:33to show you guys how to use return statements in C sharp methods. So in C sharp a method
  1220. 1:40:39is basically just a little block of code, which is designed to perform a specific task.
  1221. 1:40:46And in the last tutorial, I showed you guys how we could create a very simple method that
  1222. 1:40:49would say hi to the user. In this tutorial, I want to show you guys how we can use another
  1223. 1:40:54feature of C sharp methods, which is the return keyword. And basically, what we can do is
  1224. 1:41:00inside of these methods, we can return values back to the callers. So I'm gonna show you
  1225. 1:41:06guys how this works. And we'll kind of get an idea of what this is and what it does.
  1226. 1:41:10So down here in my program, you'll notice I have my main method over here. And I'm actually
  1227. 1:41:14going to create another method. So I'm going to come down here below the main method. And
  1228. 1:41:18I want to create a method which is going to cube a number. So when you cube a number,
  1229. 1:41:23you basically take it to the third power. So two raised to the third power is the same
  1230. 1:41:27as two times two times two, right, so we would say that this is two cubed. So what I want
  1231. 1:41:33to do is actually create a method that will take as a parameter, so take as an argument,
  1232. 1:41:39one number, and then it'll cube it. And it'll actually return that number back to the caller.
  1233. 1:41:44So this is going to be kind of cool. So remember, whenever we're creating a method, we need
  1234. 1:41:49to say, static. And like I said, I'm going to explain what static is and what it does
  1235. 1:41:55later in the tutorial. But for now, just know that whenever you're creating a method, here
  1236. 1:42:00next to this main method, because main is such a special method, I'm also going to use
  1237. 1:42:05this static keyword for the new method that I create. Then the next thing I need to tell
  1238. 1:42:10C sharp is going to be the return type. And in the last tutorial, we use this void return
  1239. 1:42:15type, which basically meant we weren't going to return any information. But now what I
  1240. 1:42:20want to do is I want to actually specify a particular data type that we're going to return.
  1241. 1:42:26So in our case, why don't we just say we're going to return an integer. So now what I
  1242. 1:42:31want to do is give this method a name. So I'm just going to say cube, so it's going
  1243. 1:42:35to be called cube. Because it's cubing a number, I want to make an open and close parentheses
  1244. 1:42:39and an open and close curly bracket. inside of this parentheses, what I want to do is
  1245. 1:42:45specify a parameter. So this cube method is going to take one parameter, and it's going
  1246. 1:42:50to be an integer, and we're just going to call it num. So num is going to be the number
  1247. 1:42:55that they pass in. And eventually, what we're going to want to do is we're going to want
  1248. 1:42:59to return to them the value of num cubed. So what I can do down here is I can make an
  1249. 1:43:05integer. And I'm just going to say and we'll call it result, and I want to set result equal
  1250. 1:43:10to the cube value of num. In order to get that we can just say num times num times num.
  1251. 1:43:17And so now this variable result is going to have num times num times num stored in it.
  1252. 1:43:24In other words, it's going to have the num cubed stored inside of it. Now what I want
  1253. 1:43:29to do is use a special keyword in C sharp called return. And whenever we put this keyword
  1254. 1:43:35inside of a method, this is basically going to tell the method that we want to return
  1255. 1:43:39an piece of information back to the caller. And also this return keyword is going to break
  1256. 1:43:46us out of the method. So over here, what I can do is I can just say return results. So
  1257. 1:43:51I'm going to go ahead and return this result variable that we just created back to the
  1258. 1:43:56caller. Now let's go up here in our main method and actually call this method. So what I want
  1259. 1:44:01to do is, I'm just going to call it so I'm going to say cube, and I'll pass in a number
  1260. 1:44:05let's pass in five. So now I'm going to go ahead and run my program and we'll see what
  1261. 1:44:11happens. So you'll see my program ran. But I mean, nothing really happened, right? There
  1262. 1:44:15was no output there really, you know, there was no errors, but really nothing happened.
  1263. 1:44:19Here's the thing, though, if I was to come over here and say console dot write line,
  1264. 1:44:24if I printed out this method, in other words, if I printed out calling this method, you'll
  1265. 1:44:31see we're actually going to print out result we're actually going to print out the value
  1266. 1:44:35of cubing these numbers. So over here, I just said console dot write line. And inside of
  1267. 1:44:40here, I'm printing out cube five. Now when I run my program, you'll see we're printing
  1268. 1:44:46out 125. Basically what happened was when I use this return keyword down here, that
  1269. 1:44:53returned the value back over to this method. So then when I called the method it was actually
  1270. 1:45:00Given the value of 125, and that is extremely useful. And that's basically a way that we
  1271. 1:45:07can get a piece of information back from a method. Just to further illustrate this point,
  1272. 1:45:11I want to create another example. So down here, I'm going to make an integer. And I'm
  1273. 1:45:16just going to call it cube number, and I'm going to set it equal to cube five. So I'm
  1274. 1:45:22actually setting this variable equal to cube five. Now I can print out cube number, and
  1275. 1:45:32you'll see we're going to get that value 125 back. And so that's essentially what we're
  1276. 1:45:37able to do, we're able to return a value back from this method, in addition to returning
  1277. 1:45:43an integer, we could return anything. So if I wanted, I could return a string, or a double.
  1278. 1:45:50Or I could even return like an array of doubles, right, something like that. So you can really
  1279. 1:45:55return any value that you want back from these methods. But returning information can be
  1280. 1:46:00extremely useful. And you're definitely going to want to use it inside of your C sharp methods.
  1281. 1:46:09In this tutorial, I'm going to talk to you guys about if statements in C sharp, an if
  1282. 1:46:13statement is basically a special structure in our program, which will allow our program
  1283. 1:46:19to respond to different situations. So essentially, with if statements, our programs can become
  1284. 1:46:25smarter. And I want to show you guys some examples of if statements, and we're going
  1285. 1:46:28to kind of look at how we can create and use if statements in our programs. Now over here,
  1286. 1:46:33I have this little text file open. And one of the cool things about if statements is
  1287. 1:46:37that as human beings, we encounter them every single day. So over here, I have some examples
  1288. 1:46:42of if statements that you might see on a daily basis. So up here, it says, I wake up, if
  1289. 1:46:47I'm hungry, I eat breakfast. So this is an example of an if statement, essentially, an
  1290. 1:46:52if statement has two parts, it has a condition, which is right here. And then it has an action
  1291. 1:46:58that gets executed when the condition is true. So our condition up here is if I'm hungry.
  1292. 1:47:05Now this condition is either true or false. It can only have two possible values true
  1293. 1:47:10or false. You're either hungry, or you're not. If you are hungry. In other words, if
  1294. 1:47:16this condition is true up here, then you're going to eat breakfast. But if you're not,
  1295. 1:47:20if this condition is false, then you're just going to move on. And that is the essence
  1296. 1:47:24of an if statement, we check a condition. If the condition is true, then we'll perform
  1297. 1:47:28an action. If the condition is false, then we'll move on and we won't perform that action.
  1298. 1:47:35So just like you're able to make a decision about whether or not you're hungry, and whether
  1299. 1:47:39or not you should eat breakfast, we can use these in our programs to do the same thing
  1300. 1:47:43to make decisions. And there's another one down here it says I look at my phone, if it's
  1301. 1:47:47about to die, I charge it. So again, this is a condition, it's either true or false.
  1302. 1:47:53If it's true, if your phone is about to die, then you charge it. Otherwise, we're just
  1303. 1:47:57going to move on. Here's a little bit of a more complex if statement, it says I leave
  1304. 1:48:02my house. If it's cloudy, I bring an umbrella. So again, here's our condition. If it's true,
  1305. 1:48:07then we're going to bring the umbrella. If it's false, though, now we're going to do
  1306. 1:48:11something else. So up here, when the condition was false, we just moved on. But now, if this
  1307. 1:48:17condition is false, otherwise, we're going to bring our sunglasses, so we're going to
  1308. 1:48:21do something else. And these are just some basic if statements. But I just kind of wanted
  1309. 1:48:25to give you guys an idea of what these were before we implement them in our programs.
  1310. 1:48:29So we can do essentially the same thing. But we can allow our programs to make decisions.
  1311. 1:48:34So when certain conditions are true, we can do certain things. And if they're false, then
  1312. 1:48:40we can do other things. So let's go back over to our program dot C sharp. And you'll see
  1313. 1:48:45down here I'm just in my main method, and I'm going to show you guys how we can use
  1314. 1:48:49some if statements. So before we do any f statements, I'm actually going to create a
  1315. 1:48:54variable. So this is going to be a boolean variable, and I'm just going to call it is
  1316. 1:48:58male. And this variable is going to tell us whether or not somebody is a male. So I'm
  1317. 1:49:04a male. So I'm going to set this equal to true. So now we have this Boolean variable
  1318. 1:49:09is male, and it has a value of true. Now what I want to do is I want to create an if statement.
  1319. 1:49:15And we can actually create an if statement and based off of this is male variable. So
  1320. 1:49:20let's say that we have this variable in our program, we're working with it. And we want
  1321. 1:49:24to do something when the person is male. So inside of our program when the person's male,
  1322. 1:49:29when this variable is true, we want to be able to do something, I can use an if statement
  1323. 1:49:34to check that. I can come down here and say if and I'm gonna make an open and close parentheses,
  1324. 1:49:39and an open and closed curly bracket. And this is sort of the general outline of an
  1325. 1:49:44if statement in our program. Now, inside of this parentheses, I want to specify a condition.
  1326. 1:49:49So just like over in that text file, we had some conditions like if I'm hungry, or if
  1327. 1:49:54my phone's about to die. Inside of these parentheses, we want to specify a condition. Now this condition
  1328. 1:50:00It needs to be a true or a false value, right? If I'm hungry, that was our condition before
  1329. 1:50:06that's true or false, right, it can only have one of two values. If my phone's about to
  1330. 1:50:11die is true or false, it can only have one of two values. So inside of this parentheses,
  1331. 1:50:16we need to put a condition which is basically just going to be a true or a false value.
  1332. 1:50:21So in my case, I want to check to see if the person is male. So I can say, if is male,
  1333. 1:50:28and if this variable is true, then we're going to be able to execute the code that we put
  1334. 1:50:32down here. So why don't we just write out console dot write line, and we'll just write
  1335. 1:50:40out you are male. So we're writing out console dot write line, if the variable is male is
  1336. 1:50:46equal to true. So let's go ahead and run this program. And you'll see over here, it's printing
  1337. 1:50:50out you are male. So our program was smart enough to determine whether or not someone
  1338. 1:50:55was male. If I was to set this equal to false, and now I went to run my program, when I run
  1339. 1:51:01the program, you'll notice that nothing is gonna get printed out because the variable
  1340. 1:51:06was false. So this line of code down here only gets executed when this condition up
  1341. 1:51:12here in these parentheses, is true. So that's how we can use a basic if statement. But let's
  1342. 1:51:18say that when is male was false, we wanted to do something else. So let's say even if
  1343. 1:51:24this variable was false, we wanted to try and do something else. So I can actually use
  1344. 1:51:28another keyword in C sharp, which is called else. So I can say else and open and close
  1345. 1:51:33curly bracket. And then down here, we can write out another line of code. So I'm actually
  1346. 1:51:38just going to copy this guy and down here, and we're going to write out a you are not
  1347. 1:51:45male. So when they are male, we'll tell them that and when they're not mail when his mail
  1348. 1:51:50is false, we'll also tell them that so his mail is false right now. And it should tell
  1349. 1:51:55us that so it should tell us you are not male. Essentially, what we've done now is we've
  1350. 1:52:01created a program that is able to respond to the value of a variable. So this program
  1351. 1:52:07can respond to this variable, right? If his mail is true, then we're going to print out
  1352. 1:52:14your mail. If his mail is false, then we're going to print out you are not mail. So our
  1353. 1:52:19program is smart, our program can adapt and adjust what it's doing, depending on the value
  1354. 1:52:26of a variable. And therein lies the essence of if statements, if statements allow our
  1355. 1:52:32programs to make decisions and to do different things, when different conditions are met.
  1356. 1:52:38But this is a very simple if statement. In fact, this is probably like the simplest if
  1357. 1:52:41else statement that we can write, I'm going to make this a little bit more complex. And
  1358. 1:52:45we're gonna go ahead and we're going to create another variable. So I'm gonna come up here
  1359. 1:52:50and make another Boolean, and I'm just going to call it is tall. And this is tall variable,
  1360. 1:52:55again, is going to store a true or false value. So why don't we set both of these equal to
  1361. 1:52:59true, just to start off, so this is tall variable is going to tell us whether or not someone
  1362. 1:53:04is tall. So this will determine if they're like tall or short. And down here, let's say
  1363. 1:53:11that instead of just checking to see if they are male, we also wanted to check to see if
  1364. 1:53:16they're tall. So I can say if is male. And I can actually use another operator in C sharp,
  1365. 1:53:24and it's called the AND operator. So I can say, two ampersands, like this. And this stands
  1366. 1:53:29for and, and basically, when we use this and operator, it allows me to write two conditions
  1367. 1:53:35in the same condition block. So it allows me to not only check to see if they're male,
  1368. 1:53:40but I could also check to see if they're tall. So now this is saying if is male, and is tall.
  1369. 1:53:49So now, not only does izmail have to be true, but is tall also has to be true for this whole
  1370. 1:53:55thing to be true. So if one of these guys was false, then this whole thing would be
  1371. 1:54:00false. So now I'm down here, I could say you are a tall male, right, and then down here,
  1372. 1:54:06instead of saying you are not male, I can instead say you are either not male or not
  1373. 1:54:13tall or both, right, because if this code gets executed, it means one or both of the
  1374. 1:54:20conditions up here is false. So let me demonstrate this. You'll see over here, both of these
  1375. 1:54:25are set to true. And when I run my program, it's going to be able to respond to that.
  1376. 1:54:29So you'll see it says you are a tall male, because both of those were true. But let's
  1377. 1:54:34say I set one of these to false, so I set his male equal to false. Well now because
  1378. 1:54:40one of these guys is false, this whole thing is going to be false. So we're going to print
  1379. 1:54:45this out down here. So now when I run the program, you will see it's printing out you
  1380. 1:54:49are either not male or not tall or both. So it's able to respond to those two variables
  1381. 1:54:55in our program. In addition to using this and operator we can also use another operator
  1382. 1:55:00Which is called for, and or does the same thing as and except it's a little bit different
  1383. 1:55:04so or will allow us to check two conditions. But this time, only one of these conditions
  1384. 1:55:09has to be true for this whole thing to be true. So we would read this as if is male,
  1385. 1:55:15or is tall. So they either have to be male, or they have to be tall, or they can be both.
  1386. 1:55:22And then we'll be able to execute this code right here. But if both of these guys are
  1387. 1:55:27false, then we're going to go down here and execute this code. So let me just demonstrate
  1388. 1:55:31this really quick. And I'm not going to bother changing the text here, you guys kind of hopefully
  1389. 1:55:35get what's going on. So even though false and true, are both up here. So even though
  1390. 1:55:39one of these guys is false, we're still going to be able to print out You are a tall male.
  1391. 1:55:46But if I was to make both of these false, now we're going to end up printing out you
  1392. 1:55:51are either not male or not tall, or both, which again, I realize isn't the right text
  1393. 1:55:56for that, but you can just kind of understand what's going on. So it says you're either
  1394. 1:55:59not male or not tall, or both. So that's the difference between and and or when we use.
  1395. 1:56:05And here, both of these conditions have to be true. So both of these variables would
  1396. 1:56:09have to be true for this whole thing to be true. And for us to execute that code, when
  1397. 1:56:14we use or only one of them has to be true. So this could be false, or this could be false,
  1398. 1:56:21but one of them has to be true for the whole thing to be true. And that's really the difference.
  1399. 1:56:24So I'm going to turn this back to and and I want to show you guys some other stuff we
  1400. 1:56:28can do. So right now we're able to check the condition where they're male, and they're
  1401. 1:56:33tall. But there's also a couple of different scenarios. In addition to that, for example,
  1402. 1:56:38let's say that they're male, but they're not tall. Well, what if we wanted to do something
  1403. 1:56:42when they were male, but they were not tall. So if they're like a short male, I can actually
  1404. 1:56:47use another keyword or another reserved word in C sharp, which is called else F. And I
  1405. 1:56:53can just type out alsef, like this, I can make an open and close parentheses and an
  1406. 1:56:56open and close curly bracket. And then I'm just going to make a new line. So essentially,
  1407. 1:57:01what I did here was I added in elsif. And then you can see this else basically goes
  1408. 1:57:06after it. When I use LS if it allows me to check another condition. So if this condition
  1409. 1:57:11up here is false, I can actually come down here and check another condition. So we wanted
  1410. 1:57:18to check to see if they were male, and they were not tall. So I could say else if is male.
  1411. 1:57:24And now I need to figure out how I can specify and not tall. So remember, this variable is
  1412. 1:57:29tall will tell us if they're tall or not. If I wanted to find out if they're not tall,
  1413. 1:57:34I could just say exclamation point is tall. And this is going to read as not tall, not
  1414. 1:57:42is tall. This exclamation point here is what's known as the negation operator. And this will
  1415. 1:57:47basically negate the condition. So over here I have is tall. If this was true, then when
  1416. 1:57:55I put this exclamation point here, it's going to make it false. If this was false, when
  1417. 1:57:59I put this exclamation point here, it's going to make it true. So this whole thing is going
  1418. 1:58:04to be true when the person is not tall. And so now this is essentially checking to see
  1419. 1:58:10if they're male, and they're not tall. So down here, we can write another message. So
  1420. 1:58:16I can basically say like you are a short male, because they're male, but they're not tall.
  1421. 1:58:23And finally, I'm going to make one more elsif. So I'm just going to come down here. And there's
  1422. 1:58:27one more thing that we want to check. So I also want to check to see if they're tall,
  1423. 1:58:32but they're not male. Right. So over here, we're checking if they're male, and they're
  1424. 1:58:36tall. Over here, we're checking to see if they're male, and they're not tall, down here,
  1425. 1:58:41I want to check to see if they're not male. And they are tall. So this is saying else
  1426. 1:58:48if not male, and is tall. So down here, we can put another little message. And I'm just
  1427. 1:58:54going to basically say like you are not a male, but you are tall. So this is essentially
  1428. 1:59:03just saying exactly what it is you're not male, but you are tall. And then down here
  1429. 1:59:08for this else. Remember, this code now is only going to get executed when none of these
  1430. 1:59:13conditions up here are true. So now down here, we can just say you are not male and not tall.
  1431. 1:59:19Alright, so let me walk you guys through this one more time. Up here, I'm saying if is male
  1432. 1:59:27and is tall. So this condition is only going to be true when both of these are true. And
  1433. 1:59:33then if this is true, we're going to come down here and execute this code, we're going
  1434. 1:59:36to say you are a tall male. But if this is false, we're going to come down here to this
  1435. 1:59:41elsif. And we're going to check another condition. So only when this is false, or we're going
  1436. 1:59:46to check this condition down here. And we're basically going to check to see if they're
  1437. 1:59:50male, and they're not tall, and I'm using this negation operator here. And if that's
  1438. 1:59:55true, then we're going to tell them that we're going to print that out. Then we're going
  1439. 1:59:58to check to see if they're not missing. And they're tall. And we'll print out a message
  1440. 2:00:02for that. And then finally, we'll say you are not male and not tall. So let's go ahead
  1441. 2:00:08and run our program. And you'll see both of these guys, we're going to be set to true
  1442. 2:00:12initially. And we'll try all these different combinations. So both of them are set to true,
  1443. 2:00:16I'm going to run the program, and it says you are a tall male. So our program was smart
  1444. 2:00:21enough to be able to respond to the value of these two variables. So now let's set his
  1445. 2:00:26male equal to false. So I'm going to come down here, and we'll just say false. And now,
  1446. 2:00:31essentially, this is going to be able to respond, so I can run my program now. And you'll see
  1447. 2:00:36it's printing out, you are not male, but you are tall. So it was able to respond to that
  1448. 2:00:41different value. Now we'll set his male back equal to true, and we'll set his tall equal
  1449. 2:00:46to false. And now we should get a appropriate response. So it says you are a short male.
  1450. 2:00:53And finally, if these are both equal to false, then again, the program's going to be able
  1451. 2:00:57to respond to that. So it's gonna say, you are not male, and not tall, awesome. So essentially,
  1452. 2:01:04what we did with these if statements was we allowed our program to respond to different
  1453. 2:01:10values, right, our program was able to make decisions based off the information that it
  1454. 2:01:16was given. And this is really just scratching the surface of if statements. You'll see over
  1455. 2:01:20here, we're using these Boolean variables, right. But there's actually another way that
  1456. 2:01:24we can specify these conditions, and it's using something called comparisons. And in
  1457. 2:01:29the next tutorial, I'm going to talk to you guys all about comparisons about how to use
  1458. 2:01:32them. And it's basically just another way that these if statements can come in handy.
  1459. 2:01:41In this tutorial, I'm going to talk to you guys some more about if statements, more specifically,
  1460. 2:01:45we're gonna be talking about comparisons. So inside of an if statement, we can actually
  1461. 2:01:49compare two values. And depending on you know, the result of the comparison, we can do different
  1462. 2:01:55things, this is going to be kind of cool. And essentially, what I want to do in this
  1463. 2:01:59tutorial is I want to build a method. And this method is basically going to be a max
  1464. 2:02:06method. So the max method, if you're not familiar, is basically a method where we can give it
  1465. 2:02:11two numbers, and it'll tell us which one is bigger. So we're going to write a method that
  1466. 2:02:14takes two parameters as input, number one, and number two, and then we'll basically be
  1467. 2:02:20able to return whichever is bigger. So I'm going to go down here, and I'm going to go
  1468. 2:02:25below this main method, and we're going to create another method. So I'm just going to
  1469. 2:02:29say static. And I'm going to want this to return an integer. So why don't we just say
  1470. 2:02:33that it's going to return an integer, and I'm going to call it get max. And then I'm
  1471. 2:02:38going to make an open and close parentheses and open and close curly bracket. So get Max
  1472. 2:02:43is going to take one or two parameters. So we're going to take in an integer num one,
  1473. 2:02:48and then we're also going to take another integer, num, two, basically, the job of this
  1474. 2:02:53method is to figure out which of these two numbers is bigger, and then return it to the
  1475. 2:02:58caller. So let's go ahead and set this up, I'm actually going to create a variable. So
  1476. 2:03:05I'm just going to make an integer I'll call it result. And I'm not actually going to give
  1477. 2:03:09this a value yet. So we're going to end up giving this value inside of the method. And
  1478. 2:03:13then down here, I'm going to return results, I'm just going to return this variable. And
  1479. 2:03:20inside of this method, now our goal is to figure out which number is bigger, right.
  1480. 2:03:25So inside of this method, we don't know if num one is bigger or num two is bigger, we
  1481. 2:03:29have no idea. But all we know is that we have two numbers here that we can work with. So
  1482. 2:03:34what we want to do is use an if statement and an if statement will allow us to do something
  1483. 2:03:40when a certain condition is true, and do something else when a certain condition is false. So
  1484. 2:03:45I'm going to make an if statement, I'm just going to say if and I'm gonna make an open
  1485. 2:03:48and close parentheses and open and close curly bracket. So this is like the sort of outline
  1486. 2:03:53the structure for our if statement. Now inside of these parentheses, I need to put a condition
  1487. 2:04:00and remember a condition is a true or false value. In the previous tutorial, we were using
  1488. 2:04:07Booleans as our condition. So I had a Boolean like is male and another one is tall. And
  1489. 2:04:12we use that as the conditions for if statements. But here's the thing, we don't necessarily
  1490. 2:04:16have any Booleans to work with, right? All we can work with here are these two integers.
  1491. 2:04:21So what I can do is I can actually use something called a comparison. So I can compare the
  1492. 2:04:25two numbers. So I can actually say like if num one is greater than num, two. And essentially
  1493. 2:04:34what I'm doing here is I'm comparing the two values. But remember, inside of these parentheses,
  1494. 2:04:39we need a condition we need a true or a false value. But actually the cool thing is that
  1495. 2:04:45when when we do this comparison, this is going to resolve down to a true or false value.
  1496. 2:04:50Right? So num one is either going to be greater than num two or it's not. There's only two
  1497. 2:04:56possible scenarios with this comparison. Right either it's true And num one is greater than
  1498. 2:05:01num two or it's false. And num two is greater than or equal to num one, right. So really,
  1499. 2:05:08this gets resolved down to a true or a false value anyway, so these comparisons can be
  1500. 2:05:15super useful inside of our parentheses. Now inside of this actual f block, if num one
  1501. 2:05:21is indeed greater than num two, then we can just set result equal to num one. And then
  1502. 2:05:27I'm going to make an else statement down here. And I'll just say else. And I'm just going
  1503. 2:05:33to say, result is equal to num two. So if num, one's greater than num, two, then we'll
  1504. 2:05:41make result num one. Otherwise, we'll make it num two. And then down here, we're just
  1505. 2:05:45returning result. So this method is basically done, we have everything that we need, we
  1506. 2:05:51should be able to figure out which number is bigger. So let's go back up here to our
  1507. 2:05:55main method. And I'm going to call this and I'm actually going to print out the answer.
  1508. 2:05:59So I'm just going to say, console dot write line. And then inside of these parentheses,
  1509. 2:06:04we're going to call it so I'll just say get max. And why don't we pass this to and 10.
  1510. 2:06:12So ideally, we're going to get a 10 back, because 10 is the bigger number. So let's
  1511. 2:06:17go ahead and run this program. And you'll see we're getting that 10 back, no problem.
  1512. 2:06:23So let's now see if it's gonna work when the first numbers the bigger one, and hopefully
  1513. 2:06:28it does, so we should get 20, which we do. So this method is working out. And you can
  1514. 2:06:32see, we're able to use this comparison in order to figure it out. So that's awesome.
  1515. 2:06:39And this is pretty cool. But I'm going to get rid of this, let's up the ante a little
  1516. 2:06:43bit, let's make this a little bit more difficult. Let's say instead of two numbers, we wanted
  1517. 2:06:48to be able to compare the three numbers. So we wanted this max function to be able to
  1518. 2:06:53tell us the maximum of three separate numbers. Well, in here, I can just specify another
  1519. 2:06:59parameter. So I can say num three. And now down here, not only do we have to figure out
  1520. 2:07:04two of them, we have to figure out three of them. So of these three numbers, which one
  1521. 2:07:08is bigger. Now we're going to want to use another if statement. So I'm going to go ahead
  1522. 2:07:13and this time, I'll make that same if statement, essentially. And now we need a condition.
  1523. 2:07:18So not only do we need to check to see if num one is greater than num two, but we also
  1524. 2:07:23have to check to see if it's greater than num three. So I'm going to say if num one
  1525. 2:07:28is greater than or equal to num, two. And so I'm using this and operator num one is
  1526. 2:07:35greater than or equal to num three, then down here, we can set result equal to num one because
  1527. 2:07:44if no one's bigger than num, two, and it's also bigger than num three, then we know it's
  1528. 2:07:48the biggest one, right, so we can just go ahead and return that. But let's say that
  1529. 2:07:53this isn't the case, I want to do another one. So I'm going to say else if and we'll
  1530. 2:07:57make a another condition down here. So I'm basically just going to say else if num two
  1531. 2:08:03is greater than or equal to num one, and number two is greater than or equal to num three.
  1532. 2:08:10Right. So if num two is bigger than num one, and it's bigger than num three, then num twos,
  1533. 2:08:16the biggest. So we'll say result is equal to number two. Finally, then we can just say
  1534. 2:08:21ELSE, AND down here we can specify the else. So I'm just going to go ahead and set result
  1535. 2:08:29equal to num three. Because if num one's not the biggest, and remember, we check that up
  1536. 2:08:34here and num twos, not the biggest, which we checked down here, then num three is going
  1537. 2:08:40to be the biggest. So let's go ahead and test this out. So now we need to include another
  1538. 2:08:45number. So I'm going to put like a four in here. So we should get back this 20 because
  1539. 2:08:49that's the biggest number. And you can see we do, let's try it with the other places.
  1540. 2:08:54So let's make this last one here, we'll make this a 40. So this is going to be the biggest.
  1541. 2:08:59And we'll see if this works, which it does. So this get max method seems to be working
  1542. 2:09:04out pretty well. Now down here, you'll notice when I compare to the two numbers, I'm using
  1543. 2:09:10these like greater than or equal to zero before I just use a greater than these are called
  1544. 2:09:14comparison operators. And these comparison operators can be used to compare different
  1545. 2:09:19values. So I can use greater than, less than, greater than or equal to, less than or equal
  1546. 2:09:25to, I can also use equal to, and this double Eagles is special because it's going to allow
  1547. 2:09:30us to compare these two numbers. So this isn't used for assignment like down here we use
  1548. 2:09:35a single equals for assignment so I assigned the value of num one to result up here I'm
  1549. 2:09:41comparing the value so I'm basically checking to see if the value of num one is equivalent
  1550. 2:09:47or equal to the value of num two. And then we can also use one more which is going to
  1551. 2:09:53be not equals and this is going to be the opposite. So this will say num one not equal
  1552. 2:09:57to num two. So that's essentially how we can use comparisons. And you can also, in addition
  1553. 2:10:03to numbers, you could also compare like characters. So I can compare like two characters, you
  1554. 2:10:09know, basically like that. And that's going to work out just fine. So these comparisons
  1555. 2:10:13are extremely useful. And I would say that a lot of times when you're using if statements,
  1556. 2:10:17you're going to be using comparisons just like that. In this tutorial, I'm going to
  1557. 2:10:25show you guys how to build a four function calculator in C sharp. Now, if you've been
  1558. 2:10:30following along with this course, you'll know that in the beginning of the course, we actually
  1559. 2:10:33created a very simple calculator. And essentially, the calculator would let the user enter in
  1560. 2:10:38two numbers. And it would take those numbers and sort of add them together and then print
  1561. 2:10:43out the answer, right, it's very simple. In this tutorial, we're going to take that calculator
  1562. 2:10:46to the next level. So we're going to actually allow the user to enter in two numbers. And
  1563. 2:10:51they're also going to be able to specify an operator. So they'll be able to specify whether
  1564. 2:10:55whether or not they want to add the numbers, subtract the numbers, multiply or divide the
  1565. 2:11:00numbers, so it's gonna be pretty cool. And let's go ahead and get into it. So the first
  1566. 2:11:04thing we want to do is we want to be able to get all the information from the user.
  1567. 2:11:09So I'm going to basically just going to be printing out prompts, and then we'll be storing
  1568. 2:11:13the values that the user enters inside of variables. So the first thing I'll do is just
  1569. 2:11:18print out a prompt, basically prompting them to enter in some information, and actually,
  1570. 2:11:23I'm just gonna use right instead of write line. So I'll say, enter a number. And they'll
  1571. 2:11:30enter their first number. And then what we want to do is want to get that number and
  1572. 2:11:34store it inside of a variable. So let's store this inside of a double. So I'm just going
  1573. 2:11:38to call this num one. And I'm just going to set it equal to console dot read line. But
  1574. 2:11:47remember, whenever we use console dot read line, this is going to give us a string back.
  1575. 2:11:51So if I want to store this value inside of a double over here, I'm actually going to
  1576. 2:11:56have to use something called convert. So I'm going to say convert.to, double, and I'm going
  1577. 2:12:03to place console dot read line inside of this methods parentheses. So I'm going to pass
  1578. 2:12:07this in as an argument as a parameter to the Convert to double method. That's essentially
  1579. 2:12:14what's happening there. And I'm basically just going to copy this line. And I'm going
  1580. 2:12:17to paste it down here, because we're going to do the same exact thing for the second
  1581. 2:12:22number. So I'm going to say num two is equal to convert to double console dot read line.
  1582. 2:12:27So we're getting the first number and we're getting the second number and we're storing
  1583. 2:12:31in them in the num, one variable in the num two variable. Now one more thing I want to
  1584. 2:12:35do is I want to get the operator. So I'm basically gonna say I'm just gonna say console dot write.
  1585. 2:12:41And over here, I'll just say, operator. So this is going to be like a plus sign a minus
  1586. 2:12:49sign, division, sign, multiplication, sign, etc. And then down here, we can just make
  1587. 2:12:54this a string. So I'm going to say string op for operator is going to be equal to console
  1588. 2:13:00dot read line. So basically, what I'm doing is I'm getting the first number, I'm converting
  1589. 2:13:07it to a double, I'm getting the operator plus minus division, whatever, I'm getting the
  1590. 2:13:12second number. So now at this point in my program, I should have all the information
  1591. 2:13:16that I need. So the question becomes how can we perform the operation? In other words,
  1592. 2:13:22we have the two numbers, great, but how can we figure out what the user entered in? Right?
  1593. 2:13:27How do we figure out what Opie is telling us to do, we can use an if statement. So I'm
  1594. 2:13:32going to use an if statement, I'm just gonna say if let me make an open and close parentheses
  1595. 2:13:36and open and closed, curly bracket. And down here, we want to check a condition. So essentially,
  1596. 2:13:42what I want to do is I want to check to see if the operator is equal to a plus sign. So
  1597. 2:13:46I can say if op is equal to plus sign, right? If the operator is equal to a plus sign, then
  1598. 2:13:55we know for a fact that they're trying to add the numbers. And I'm actually just going
  1599. 2:13:59to print that out down here. So I can say console, dot write, and we're basically just
  1600. 2:14:04print out num, one plus num two. And actually, I'm going to do write line here. Alright,
  1601. 2:14:10so if it's a plus sign, then we're going to be able to add the numbers. But what if it's
  1602. 2:14:14not a plus sign? Well, I want to check another condition here. So I'm going to say, else
  1603. 2:14:19if and we'll check another one. We basically want to check to see if the operator is equal
  1604. 2:14:25to a minus sign. If the operator is equal to a minus sign, now we're going to do the
  1605. 2:14:30same thing we did up here. It's just that we're going to subtract the number. So I'm
  1606. 2:14:35going to say num one minus num two. We can do the same thing for multiplication and division.
  1607. 2:14:40So I'm going to go ahead and copy this code and I'm just going to paste it down here.
  1608. 2:14:44And down here. Instead of checking minus we're just going to check division and then if it
  1609. 2:14:49is division, we're going to divide the numbers. And then finally, I'm going to check for multiplication.
  1610. 2:14:54So I'm going to say Asterix for multiplication and then num one times Number two. So we're
  1611. 2:15:02able to cover all four operations, right addition, subtraction, division and multiplication.
  1612. 2:15:07Each time, whatever the user enters, we're going to perform the correct operation, right?
  1613. 2:15:11This if statements, pretty simple. But let's say that the user enters in an operator that's
  1614. 2:15:16not equal to any of these guys up here, right? What if they enter in like a random character
  1615. 2:15:22like a string of text? Well, we want to be able to throw an error message. So what I
  1616. 2:15:26can do is I can come down here and just say else. And then inside of this else block,
  1617. 2:15:31I'm just gonna say, console dot write line. And I'm just going to print out like, invalid.
  1618. 2:15:40operator. So this will basically tell them like, Hey, you had an eight, you had an invalid
  1619. 2:15:44operator, like, That's not good. So now we have our whole program. And we use our little
  1620. 2:15:49if else structure here to figure out what was stored inside of that op variable. So
  1621. 2:15:56we figured out what the user wanted us to do. Let's go ahead and run this program. And
  1622. 2:16:00we'll see how we did. Over here it says, enter a number I'm gonna enter 10 and enter an operator,
  1623. 2:16:05why don't we do addition, and enter another number? Why don't we do like 5.5. So now when
  1624. 2:16:11I hit enter that if statement is going to execute, and it's going to see that plus sign,
  1625. 2:16:15and it's going to be able to add the numbers. So you see here, we get 15.5. So we successfully
  1626. 2:16:21added the numbers together. Let's try a different one, I want to do multiplication. So let's
  1627. 2:16:27do 60 times 4.23. And we get 253. Yeah, that seems about right. Alright, so let's try one
  1628. 2:16:36more. And we'll try to break it. So I'm basically just going to enter a number 40. And now I'm
  1629. 2:16:41going to enter in an invalid operator. So I'm going to enter in like an H for the operator,
  1630. 2:16:46and then we'll enter in a five. And now this should tell us invalid operator because the
  1631. 2:16:52program didn't recognize that operator. In other words, it wasn't able to handle that.
  1632. 2:16:57So we threw our little error message. And that's essentially all we need to do to build
  1633. 2:17:01this four function calculator. The magic is in the if statement. So we can use the if
  1634. 2:17:06statement to figure out what calculation or what operation the user wanted to do. And
  1635. 2:17:12we can just keep using all these different assets in order to figure that out. So this
  1636. 2:17:16is extremely useful. And hopefully, you can find a use for something like this and your
  1637. 2:17:20programs. In this tutorial, I want to show you guys how to use switch statements in C
  1638. 2:17:29sharp. Now a switch statement is basically just a special type of if statement where
  1639. 2:17:34we can check a bunch of different conditions really easily. And there's, I'll be really
  1640. 2:17:39clear in the example that I showed you guys. So let's go ahead and get started. Down here,
  1641. 2:17:43my program, I just have a very simple program set up. And in this tutorial, I want to create
  1642. 2:17:48a method. So we're actually going to create a method, which is going to convert numbers
  1643. 2:17:53into days of the week. So if I pass this method is zero, it would give me back Sunday, because
  1644. 2:18:00Sunday is the first day of the week. If I pass this a one, it would give me back a Monday,
  1645. 2:18:07right? If I pass this a two, it would give me back Tuesday, etc, right. So if I, you
  1646. 2:18:15know, it's basically going to map numbers to the specific day of the week that we're
  1647. 2:18:20going to give it. And this is going to be a pretty cool method. So I'm going to come
  1648. 2:18:24down here and we're going to go ahead and write the method, I'm just going to say static.
  1649. 2:18:28And this is going to return a string. So it's going to return the name of the day of the
  1650. 2:18:32week. And I'm just going to call this get day. And this method is going to take one
  1651. 2:18:39parameter and it's going to be the day num. So I'm just going to call this day num. So
  1652. 2:18:45de nom is going to be like 0123456, etc, right? It's going to be a number. And we're going
  1653. 2:18:50to take that number and we're going to convert it into a day of the week, we're going to
  1654. 2:18:55convert it into a string, which would be the day of the week. So I'm going to create a
  1655. 2:18:59variable over here, that's going to be a string variable. And I'm just going to call it day
  1656. 2:19:04name. And I'm not going to give it a value and then down here, eventually Our goal is
  1657. 2:19:09going to be able going to be to return day name. So inside of our little method over
  1658. 2:19:15here inside of our method block, our goal is to populate this day name variable with
  1659. 2:19:21a value right with the correct value, that's going to correspond to the day num that gets
  1660. 2:19:26passed in. Cool. So I'm going to show you guys how we can use a special structure in
  1661. 2:19:32C sharp to do this, and it's called a switch statement. So I'm going to set up my switch
  1662. 2:19:36statement, I'm just going to say switch, I'm going to make an open and closed parentheses
  1663. 2:19:39and open and close curly bracket. And down here, we're going to be able to start writing
  1664. 2:19:44our switch statement. Now, the switch, David needs one piece of information inside of this
  1665. 2:19:49parentheses, and it basically needs a value. So the whole point of the switch statement
  1666. 2:19:55is we're going to pass it a value and then we're going to check to see if that value
  1667. 2:19:59is equal to a bunch of different stuff. And depending on what it's equal to, we're going
  1668. 2:20:02to do different things. So this will be clear in a second. In here, I'm going to pass in
  1669. 2:20:06de nom. Because if daenam is zero, I want to do something if daenam is one, I want to
  1670. 2:20:11do something else. If daenam is two, I want to do something else. In other words, my goal
  1671. 2:20:15is to figure out what the de nom is. And depending on what it is, I'm going to do something in
  1672. 2:20:20other words, I'm going to give this day name variable a different value. Now down here,
  1673. 2:20:24in the switch statement, we can make what are called cases. So I can come down here
  1674. 2:20:28and I can just type out case. And over here, I can just type in a value. So I can say zero
  1675. 2:20:34here. And I'm going to make a colon down here, I'm basically just going to say day name is
  1676. 2:20:41equal to Sunday. And I'm also going to say, break down here. So let's talk about what
  1677. 2:20:48this is saying. Essentially, this line, this little block of code is saying that in the
  1678. 2:20:52case, that day, num is equal to zero, I want to do this. So in the case that de nom has
  1679. 2:21:00a value of zero, we're going to do whatever I specify down here, essentially, what I'm
  1680. 2:21:04doing is I'm saying day name is going to be equal to Sunday. And I'm also saying break,
  1681. 2:21:09and we're going to talk about break in one second. So now what I can do is I can create
  1682. 2:21:14one of these cases for each of the possible numbers, right. So now instead of just making
  1683. 2:21:19one for zero, I'll make one for one. And the case that day num is one, I'm going to say
  1684. 2:21:27day name is going to be equal to Monday, right? And then I get I'm going to say break. And
  1685. 2:21:31we'll do this one more time, I'm going to do it for two. So in the case that day, num
  1686. 2:21:39is equal to two. Now I'm going to say day name is equal to Tuesday. And you'll notice
  1687. 2:21:45here I'm using this break statement. And this is a special reserved word in C sharp, which
  1688. 2:21:51basically says that we want to break out of the structure that we're currently in. So
  1689. 2:21:56we're inside of this switch statement. And essentially, what happens is with this switch
  1690. 2:22:01statement, it's going to look through all of these different cases. So it's going to
  1691. 2:22:05say okay, is it zero? Is it one is it two, but let's say that de nom is zero, right?
  1692. 2:22:10So it says his case, zero, yes, we're going to execute this, we're going to assign a value
  1693. 2:22:15of Sunday to daenam. If I don't put this break statement in here, then C sharp is going to
  1694. 2:22:21keep checking all of these cases after that, so it's not going to stop, it's going to keep
  1695. 2:22:26checking. So if you have a scenario where you want to be able to keep checking the other
  1696. 2:22:31cases, even after one of them was true, then you can get rid of the break statement. But
  1697. 2:22:35I think in most cases, you'll probably just want this break statement in there. Alright,
  1698. 2:22:39so I've made one of these for Sunday, Monday and Tuesday, I'm going to go off and make
  1699. 2:22:43some for the rest of the days of the week, and then we'll come back and check it out.
  1700. 2:22:47Alright, so I went ahead and made one of these for every day of the week. You can see we
  1701. 2:22:51have Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday. And this is a really
  1702. 2:22:56easy way to check all these different cases, right, I can just say case six day name, Saturday
  1703. 2:23:02excetera. So we basically have our case statement set up, we have our switch statement set up.
  1704. 2:23:08But here's the problem, let's say that the user passes a value into this method, that's
  1705. 2:23:13not zero through six. Right? So this case, this switch statement can only handle values,
  1706. 2:23:19zero through six. But what happens if someone passed like a 102? there? Well, I can actually
  1707. 2:23:24account for that. So down here, I can create something called a default, I can basically
  1708. 2:23:29just type out default, I can type a colon. And now down here, I can specify what I want
  1709. 2:23:34to do if none of these cases are true. So I could come down here and I can basically
  1710. 2:23:39just say, de name, and we'll just set this equal to invalid number, or why don't we say
  1711. 2:23:45invalid day number. And this will basically act as like a error message. And then down
  1712. 2:23:51here, we're also going to need to say break. So this is essentially how we can account
  1713. 2:23:56for any values that aren't included up here as cases. So our function or our methods seems
  1714. 2:24:03to be done at this point. So let's go ahead and try it out. So remember, get day is going
  1715. 2:24:08to take one value, which is going to be a num. And let's go up here to our main method,
  1716. 2:24:13and we'll try this out. So I'm actually just going to print out whatever we get. So I'm
  1717. 2:24:16gonna say console dot write line. And I'm going to go ahead and print out get day and
  1718. 2:24:23let's pass this as zero. So I'm going to go ahead and say get day zero. So hopefully,
  1719. 2:24:28we should get Sunday back because Sunday is mapped to zero. And you'll see over here that
  1720. 2:24:35we get Sunday back so let's try another one. Why don't we try four? And I think four is
  1721. 2:24:41going to be Thursday. Yeah. So Thursday. And let's try one more. So I'm going to try an
  1722. 2:24:47invalid number. So let's put ad in here. So if I say get day ad now we should get that
  1723. 2:24:53error message which tells us that it's an invalid day number. Yeah. So over here we're
  1724. 2:24:57getting that invalid day number So that's basically what we can do is switch statements.
  1725. 2:25:02Now let me just point this out everything that you can do with switch statements you
  1726. 2:25:06could actually do with if statements. The reason that we have switch statements though
  1727. 2:25:09is because they make it a lot easier to do essentially what we're doing over here, you
  1728. 2:25:14could do with if statements, but it would be a lot messier than this, and it would be
  1729. 2:25:17a lot harder to manage. With switch statements, it's very clear, right, it's very clear what's
  1730. 2:25:21going on and what values are getting mapped to what code so switch statements are extremely
  1731. 2:25:27useful, and you should definitely consider using them in your C sharp programs. In this
  1732. 2:25:35tutorial, I'm going to show you guys how to use while loops in C sharp. Now while loop
  1733. 2:25:40is basically a programming structure that allows us to loop over a specific block of
  1734. 2:25:45code, while a certain condition is true. Basically, using while loops, we're able to keep doing
  1735. 2:25:51something repeatedly while a condition is true. And this can come in handy in a bunch
  1736. 2:25:56of situations. And loops are extremely useful in a programming language like C sharp. So
  1737. 2:26:01let's get started. I'm going to show you guys how they work and how to use them. Basically,
  1738. 2:26:05just to give you a general introduction into while loops, down here in my main method,
  1739. 2:26:10I'm actually going to create one thing, and this isn't going to be the while loop, I'm
  1740. 2:26:14just going to create a variable, it's going to be an int, and I'm just going to call it
  1741. 2:26:18index, and I'm gonna set it equal to one. And we're gonna end up using this index variable
  1742. 2:26:22inside of our loop. So now I want to create a while loop. So I'm just gonna say while
  1743. 2:26:26and I'm gonna make an open and close parentheses and an open and close curly bracket. Now here's
  1744. 2:26:31how while loops work up here in these parentheses, I'm going to specify a condition. So if you're
  1745. 2:26:37familiar with if statements, you'll know that with an if statement, we have a condition,
  1746. 2:26:40right, we check a condition. And if that condition is true, then we execute the code inside of
  1747. 2:26:45the if statement. Pretty simple. It's the same concept with this while loop up here
  1748. 2:26:49in this while loop, I'm going to specify a condition. And if this condition is true,
  1749. 2:26:54I'm going to be able to execute all of the code that goes down here in these curly brackets.
  1750. 2:26:59The only thing is with a while loop, not only are we going to execute at one time, but we're
  1751. 2:27:04going to keep continuously executing it until the condition up here in these parentheses
  1752. 2:27:09is false. So we're basically going to check this condition. If it's true, we'll execute
  1753. 2:27:14all the code inside of these curly brackets. And then we'll come up with check the condition.
  1754. 2:27:18Again, if it's still true, we'll come down and execute it again. So you're continuously
  1755. 2:27:22looping through this code. So it's essentially an if statement, but you keep going until
  1756. 2:27:27the condition is false. And over here, in these parentheses, we need to specify our
  1757. 2:27:31condition. So I'm going to make a condition I'm just going to say while index is less
  1758. 2:27:36than or equal to five. So as long as this index variable is less than or equal to five,
  1759. 2:27:43I'm going to keep executing all the code down here. And then down here, I'm basically just
  1760. 2:27:47going to write a few lines of code. So I'm going to say console dot write line, and we're
  1761. 2:27:53going to print out the value of index. And then down here, I'm going to increment the
  1762. 2:27:58value of index. So I'm going to say index plus plus. And remember, this plus plus basically
  1763. 2:28:03just adds one onto the index. So essentially, what's happening is, every time we go through
  1764. 2:28:09this loop, I'm going to print out the value of index, then I'm going to increment index,
  1765. 2:28:14and then I'm going to go back here and check the condition again. So this is our basic
  1766. 2:28:19while loop, I'm going to run the while loop, we'll see what happens. And then I'm going
  1767. 2:28:22to explain exactly what happens. So we can get a better idea of how this works. Let's
  1768. 2:28:27go ahead and run the program. And you'll see over here, I'm printing out one through five.
  1769. 2:28:35And maybe you expected that maybe you didn't. But this is what we got. And I'm going to
  1770. 2:28:39go ahead and explain exactly how this happened. Step by step. So the first thing we did up
  1771. 2:28:44here was we created this integer index and we set it equal to one right now, then we
  1772. 2:28:51created our while loop. So when C sharp goes to execute our program, it's first going to
  1773. 2:28:56create this variable index, and it's going to store one inside of there, then the next
  1774. 2:29:00thing it's going to do is check this condition. So the first thing before it executes any
  1775. 2:29:05of the code inside of the loop before it does anything else, it's going to check this condition.
  1776. 2:29:09So it's going to check to see if index is less than or equal to five, since index was
  1777. 2:29:13equal to one initially, that's going to be true. So then C sharp is going to go down,
  1778. 2:29:18it's going to execute this line. So it's going to print out index onto the console, as you
  1779. 2:29:21can see over here, then it's going to increment index. So index is now going to be equal to
  1780. 2:29:26two, then C sharp is going to go all the way back up to this condition and check it again.
  1781. 2:29:32So before C sharp is going to execute that code a second time, it needs to check the
  1782. 2:29:36condition again. So now index is equal to two and two is less than or equal to five.
  1783. 2:29:41So C sharp is going to come down here, it's going to print out two, as you can see right
  1784. 2:29:44there, and it's going to increment index, then it goes all the way back up and checks
  1785. 2:29:49the condition again, right? So every single time we execute all that code before it's
  1786. 2:29:55going to execute it again it goes back up and checks the condition. All right, eventually
  1787. 2:30:00We're going to get to a point where index is going to be equal to five, and we printed
  1788. 2:30:03out five over here, then we're going to increment it to six, C sharp is going to come up here,
  1789. 2:30:09and it's going to check is six less than or equal to five, and then the condition is going
  1790. 2:30:13to be false. So we're going to break out of the while loop just like that. And that's
  1791. 2:30:18essentially how we can use these wild loops. So it's very simple, right, you specify a
  1792. 2:30:23condition, and you're going to keep looping through the loop as long as that condition
  1793. 2:30:27is true, easy enough. Now, I do want to warn you guys of something, there is a possibility
  1794. 2:30:32of something called an infinite loop. And an infinite loop is a situation where this
  1795. 2:30:37condition up here never becomes false. So for example, if I was to get rid of this index
  1796. 2:30:42plus plus, now this condition up here, index less than or equal to five, this is always
  1797. 2:30:48going to be true. Because we're never modifying the value of index when we were incrementing.
  1798. 2:30:53It, eventually we would increment it enough to get to five. But since we're not incrementing
  1799. 2:30:59it anymore, we're just going to loop infinitely. So let me show you guys what happens when
  1800. 2:31:02I do this. When I run my program, you'll see over here that I'm just constantly printing
  1801. 2:31:07out one, I mean, this, it's literally just printing out one as fast as it can. And it's
  1802. 2:31:12going to do this forever. If I let it it'll do this infinitely, because it's never told
  1803. 2:31:18that it needs to stop. In other words, this condition up here is never false. And so that's
  1804. 2:31:24why we have an infinite loop. And that's probably going to happen to you, as you play around
  1805. 2:31:27with loops. Everyone's had an infinite loop before. And in most cases, you're going to
  1806. 2:31:32want to avoid them, although there are some special cases where you're actually going
  1807. 2:31:35to want infinite loops. So that's sort of the basics of while loops. It's very simple,
  1808. 2:31:40right? We have a condition up here, as long as that condition is true, we're gonna keep
  1809. 2:31:43executing the code down here. And every single time we go through the loop, we check that
  1810. 2:31:47condition. Again, I want to show you guys one more thing. And there's actually another
  1811. 2:31:51type of loop that we can use, which is similar to a while loop, it's called a do while loop.
  1812. 2:31:56And in order for me to kind of illustrate what this is doing, I want to show you guys
  1813. 2:32:00an example. So if I came up here and set index equal to six, let's talk about what's going
  1814. 2:32:05to happen. So if index is equal to six, right, the first thing that my program does with
  1815. 2:32:11this while loop is it checks this condition. So the first thing before it executes any
  1816. 2:32:15of this code before it's allowed to even touch that code, it checks this condition. So since
  1817. 2:32:20index is equal to six, and this condition is getting checked, before we execute the
  1818. 2:32:24code, this code is never going to execute, because six is not less than or equal to five,
  1819. 2:32:30this condition is false. So we're never going to execute this code. So if I run my program,
  1820. 2:32:35you guys will see that nothing gets printed out, right, we don't execute any of the code
  1821. 2:32:40inside that while loop. But there's actually another type of loop, which is essentially
  1822. 2:32:44the opposite of a while loop. And I'm going to show you guys that. So I'm going to basically
  1823. 2:32:48going to take this while loop declaration up here, I'm going to throw it down here.
  1824. 2:32:52And I'm going to make sure I put a semicolon after it. And then up here, instead of saying
  1825. 2:32:56while I'm going to say do. And so now we have another type of loop, which is a do while
  1826. 2:33:03loop. And the difference between a do while loop and a while loop is that a do while loop
  1827. 2:33:07will execute the code inside of the loop before it checks the condition. So now even though
  1828. 2:33:13index is equal to six, we're still going to be able to execute this loop one time before
  1829. 2:33:19we look at the condition, right, so it's basically going to execute all this code, then it's
  1830. 2:33:24going to look at the condition, it's going to figure out that it's false. And it's going
  1831. 2:33:27to go ahead and break out of this. So I'm going to show you guys how we can do this.
  1832. 2:33:32And I'm just gonna run my program. And you'll see that even though this condition was false,
  1833. 2:33:37even though six is not less than or equal to five, we were still able to print it out
  1834. 2:33:42one time, because in a do while loop, you execute the code inside the loop first, and
  1835. 2:33:48then you check the condition. So that's basically another type of loop that we can use. And
  1836. 2:33:54do wire loops definitely come in handy. And there's a lot of situations where you want
  1837. 2:33:57to use them. Although I would say that by a huge majority, while loops are more useful.
  1838. 2:34:04But do while loops are also awesome. So that's kind of the basics of while loops, and do
  1839. 2:34:07while loops. And this is really just the basics of looping. In general, there's a lot of situations
  1840. 2:34:12when we're programming, we're going to want to continually do the same thing until a certain
  1841. 2:34:16condition is false. And that kind of gives you an overview of how it works. In this tutorial,
  1842. 2:34:25I'm going to show you guys how to build a guessing game in C sharp. Now this is going
  1843. 2:34:30to be a very basic guessing game. Essentially, we're going to define a secret word. And then
  1844. 2:34:35the user is going to have to try and guess that word. And we're going to keep asking
  1845. 2:34:39the user to try and guess it until they get it right. So this is going to be kind of cool.
  1846. 2:34:43And I'm going to show you guys how we can use some of the stuff that we've learned in
  1847. 2:34:46this course. So far things like loops, and if statements and variables, we're going to
  1848. 2:34:51use all of them together to create this cool game. So let's start creating the game. Remember,
  1849. 2:34:56this is going to be a guessing game. So the first thing we want to do is Come up with
  1850. 2:35:00a word that the user is going to have to guess. So I'm going to create a variable and it's
  1851. 2:35:04just going to be a string. And I'm just going to call it secret word. And the secret word,
  1852. 2:35:09why don't we just give it a value, we'll set it equal to draft. So draft is going to be
  1853. 2:35:14the secret word. And the user's goal for our game is going to be to try and guess that
  1854. 2:35:19secret word. Now I want to create one more variable. And we're actually going to call
  1855. 2:35:23this one guests. So this is going to be another string. And I'm just going to say it's called
  1856. 2:35:29guests. And why don't we just set it equal to this empty string over here, so I'm not
  1857. 2:35:32really giving this a value yet. Alright, so now we have our secret word. And we also have
  1858. 2:35:38our guests. And what I want to do is, I want to be able to keep prompting the user to guess
  1859. 2:35:44the word until they get it correctly. So anytime in C sharp that we want to keep doing something
  1860. 2:35:50repeatedly until a certain condition is false, we're going to use a while loop. So I'm going
  1861. 2:35:56to go ahead and create a while loop. And I'm just gonna make an open and close parentheses
  1862. 2:36:00open and close curly bracket. And there's really two things to this while loop, the
  1863. 2:36:04first thing is going to be a condition. And the second thing is going to be the code that
  1864. 2:36:07we want to continually execute. So let's think this through what condition do we want to
  1865. 2:36:12put up here? Well, basically, we want to keep looping, while the guests is not equal to
  1866. 2:36:18the secret word. In other words, as long as the guest the user guesses isn't equal to
  1867. 2:36:24what the secret word is, we want to keep looping and keep asking them to guess. So down here,
  1868. 2:36:30what I want to do is keep prompting them to guess. So I'm basically just gonna say, console
  1869. 2:36:35dot write, and I'll write out to the screen a prompt, I'll say, enter guests. And then
  1870. 2:36:41now that we've prompted them to enter the guest, what I want to do is I want to take
  1871. 2:36:45whatever they entered, store it inside of this guests variable. And that way, we can
  1872. 2:36:50keep checking it every time we go through this a loop. So down here, I'm basically just
  1873. 2:36:54going to say guess, is equal to console dot read line. So I'm going to go ahead and read
  1874. 2:37:02the line of text that the user enters, basically just read whatever word they entered in and
  1875. 2:37:07store it inside of this guest variable. So essentially, what's happening now is, every
  1876. 2:37:12time we go through the loop, we're prompting the user to guess the word, and we're storing
  1877. 2:37:16whatever they guess, inside of this guest variable, then we're going to go up here,
  1878. 2:37:20and we're going to check to see if it's equal to the secret word. If it's not equal to the
  1879. 2:37:24secret word, then we're going to go through and ask them again. But if it is equal to
  1880. 2:37:29the secret word, then we're gonna go ahead and break out of the loop. And then down here,
  1881. 2:37:33we can just print out a success message. So I can just print out console dot write, and
  1882. 2:37:38I'll just print out you win. So this is our program, it's actually a pretty easy program.
  1883. 2:37:44And I do just want to point out that in addition to using a while loop, we also could have
  1884. 2:37:49used a do while loop in order to write this program. So if you want a little challenge,
  1885. 2:37:54and you want to try and challenge yourself, go ahead and try to write this with a do while
  1886. 2:37:59loop instead of a while loop. And if you need to figure out what do while loops are just
  1887. 2:38:02check out the previous tutorial, I explained what they were. But for our case, I'm just
  1888. 2:38:06doing it with a simple while loop just to keep it as straightforward as possible. But
  1889. 2:38:09like I said, if you want a challenge, try it with a do while loop. So let's see if we
  1890. 2:38:13can run our program. Believe it or not, this is actually all the code that we need for
  1891. 2:38:17this guessing game. So I'm going to go ahead and run the program. Now you'll see over here,
  1892. 2:38:23it says enter guests, so I'm just going to type in some random text. And you'll notice
  1893. 2:38:27that because this text didn't match the secret word draft, it's asking me to enter again.
  1894. 2:38:35So I'm going to keep entering and I can keep entering essentially infinitely many times.
  1895. 2:38:40And it's going to keep prompting me to guess until I get the word. But if I was to type
  1896. 2:38:44in the secret word, so if I was to type in draft here, now when I hit Enter, draft is
  1897. 2:38:50equal to the secret word. So we're going to break out of that while loop. And we're going
  1898. 2:38:54to get this you win message. So that's essentially all our game did. It allowed us to keep guessing
  1899. 2:39:01the word until we got it? Right. So this is a pretty cool game. And you know, you guys
  1900. 2:39:05can see down here, it's actually pretty easy for us to implement it. There's only you know,
  1901. 2:39:09maybe like seven or eight lines of code. But I think there's a little problem with this
  1902. 2:39:13game. And the problem is that the user gets unlimited guesses, right? So the user can
  1903. 2:39:18just keep guessing infinitely. I mean, technically, technically, they could type in, you know,
  1904. 2:39:22every possible combination of letters until they got the secret word, right. And that's
  1905. 2:39:26not a very good game, I think we'll make this game better would be imposing a guess limit.
  1906. 2:39:32So we could say like, the user can only guess three times or they can only guess four times.
  1907. 2:39:35And if they don't get it in four guesses, then they lose the game. I think that would
  1908. 2:39:40be a little bit better game. So why don't we try to implement that. Let's see if we
  1909. 2:39:44can come up with some code that will allow us to program that game. Well, I think I know
  1910. 2:39:50what we need to do. So the first thing we want to do is we need some way to keep track
  1911. 2:39:54of how many times the user guesses right. If we're going to impose a guess limit on
  1912. 2:39:59this Game, we need to be able to keep track of how many times they've guessed. So that
  1913. 2:40:04was a good place to start, why don't we just start with that I'm going to create an integer.
  1914. 2:40:07And I'm just going to call it guest count. And we'll set it equal to zero initially,
  1915. 2:40:11right? Because Initially, the user hasn't guessed at all. And what we want to do is
  1916. 2:40:15we want to increment this variable every time the user guesses. So let's go down here. And
  1917. 2:40:22you'll notice this is the code down here, that allows the user to guess right, it's
  1918. 2:40:26this code, which is essentially prompting the user to guess and then storing their guests.
  1919. 2:40:32So in addition to prompting them and storing the guests, we can also increment the guest
  1920. 2:40:36count. So I can come down here and I can say, guest count plus, plus, right, just like that.
  1921. 2:40:42So I'm incrementing, the guest count every single time they guess, awesome. But we're
  1922. 2:40:48still gonna need some more information. So another piece of information we're going to
  1923. 2:40:52need is the guest limit. So we need to essentially tell our program how many times the user can
  1924. 2:40:58guess. So I'm just going to make another and I'll just call this guest limit. And over
  1925. 2:41:03here, we can define how many times we want the user to be able to guess, well, why don't
  1926. 2:41:07we say three guesses? I think three guesses is enough, three strikes in your out. And
  1927. 2:41:12so now we're basically limiting the user to three guesses. I also want to create one more
  1928. 2:41:19variable, and this is going to be a Boolean. And this is going to be called out of guesses.
  1929. 2:41:25And I'm going to set this equal to false. And this Boolean is basically going to tell
  1930. 2:41:29us whether or not the user is out of guesses. So if the user still has guesses left, this
  1931. 2:41:36is going to be equal to false. But if the user is out of guesses, then this is going
  1932. 2:41:40to be equal to true. So we're going to set it to false initially. And then down here,
  1933. 2:41:44we need to use these two new variables inside of this while loop. So the first thing I think
  1934. 2:41:50we should do is we should only allow the user to guess when they still have guesses left,
  1935. 2:41:56right? In other words, I only want the user to be able to guess the word if they haven't
  1936. 2:42:01already guessed three times, right? If they haven't guessed the guessed limit. So what
  1937. 2:42:05we can do is we can actually create an if statement. So I can say if and down here,
  1938. 2:42:09I'm going to make open and close curly brackets. And inside of these parentheses, I need to
  1939. 2:42:14put a condition. So let's think about this. What do we want to check? Well, I want to
  1940. 2:42:18check to see if the user is out of guesses, right? So I can check to see if the guest
  1941. 2:42:24count is less than the guest limit. And if this is true, in other words, if the guest
  1942. 2:42:30count is less than the guest limit, then the user still has a guest left, so they can go
  1943. 2:42:34ahead and guests. So I'm going to take all this code for guessing. And I'm going to put
  1944. 2:42:38it inside of these curly brackets. Right. So once again, we're checking this condition,
  1945. 2:42:44if the guest count is less than the guest limit, then we're gonna go ahead and print
  1946. 2:42:49out all of this stuff. So we're going to allow them to guess the word, and then we'll increment
  1947. 2:42:53the guest count. But if this isn't the case, in other words, if guest count isn't less
  1948. 2:42:58than the guest limit, that means that they've already reached their guests limit, right?
  1949. 2:43:03That means they don't have any guesses left. So I can say else down here. And if they run
  1950. 2:43:09out of guesses, what I want to do is I want to modify this out of the guesses variable.
  1951. 2:43:14So when they run out of guesses, in other words, when this condition is false, I want
  1952. 2:43:18to say out of guesses is equal to true. So I'm going to say out of guesses is equal to
  1953. 2:43:24true. So basically, what we're doing down here is we're saying, hey, they're out of
  1954. 2:43:29guesses. And this is actually what we call like a flag. So you'll hear people sometimes
  1955. 2:43:34call these like Boolean flags. And this is basically just a value that's giving us a
  1956. 2:43:39piece of information, right? This is telling us that the users out of guesses. So there's
  1957. 2:43:43one more thing we have to do in this while loop, you'll notice that this while loop is
  1958. 2:43:47going to keep executing as long as the guest is not equal to the secret word. But now since
  1959. 2:43:53we're adding in the gas limit, there's actually another situation where we're going to want
  1960. 2:43:57to stop looping. And that situation is when the user is out of guesses. So I want to modify
  1961. 2:44:03this, I want to say that we want to keep guessing Wow, they haven't guessed the secret word.
  1962. 2:44:08And they're not out of guesses, right? Because if they haven't guessed the secret word, then
  1963. 2:44:14we want to keep asking them. But if they ran out of guesses, then we want to break out
  1964. 2:44:18of the loop, right? So I'm going to keep looping while the guest is not equal to the secret
  1965. 2:44:23word, and the user is not out of guesses. So now we have a while loop set up, which
  1966. 2:44:29is going to allow us to specify a guest limit. So there's one more thing we have to do, which
  1967. 2:44:35is down here. Now down here. We're basically just saying you win, right? So whenever we
  1968. 2:44:40break out of the loop, we're telling them that they won. Here's the problem though.
  1969. 2:44:44So there's two situations now where they can break out of the loop when the guest is equal
  1970. 2:44:48to the secret word in other words when they guess correctly, but there's also another
  1971. 2:44:52situation which is when they're run out of guesses, right? And so down here, we're not
  1972. 2:44:57going to be super sure which situation occurred. So what we can do is we can use another if
  1973. 2:45:02statement. And we can check. So I can basically say if, and I can check to see if they're
  1974. 2:45:07out of guesses. So I can say if out of guesses. If this is true, then I want to print out
  1975. 2:45:13a lose message. So I'm basically just gonna say you lose. But otherwise, if it's not true,
  1976. 2:45:20that means that they guessed the word correctly. So we can go ahead and put this win message
  1977. 2:45:26down here. Alright, so that is essentially everything we need to do to specify the guest
  1978. 2:45:32limit, I know this was a lot of code, why don't we run our program, we'll play the game,
  1979. 2:45:36make sure we got everything right, and then we can come back, and we can kind of go over
  1980. 2:45:40what we did again. So I'm gonna run the program. It says enter guests. So I'm going to go ahead
  1981. 2:45:45and lose the game. So remember, we have three guesses, so I can just type in some random
  1982. 2:45:49guests. Another one. Now we're on our third and final guests. If I don't get the word,
  1983. 2:45:54this time, we're going to lose, so I'm not going to get it. And I'm gonna click Enter,
  1984. 2:45:59and it says, you lose. So it was able to figure out that we didn't guess the secret word and
  1985. 2:46:04three tries, and it told us that we lost the game. Cool. So now let's see if we can win
  1986. 2:46:08the game. So I'm going to go ahead and run this program, and it's gonna say enter guests.
  1987. 2:46:12So let's enter two wrong guesses. Now we're on our last try. Let's say I have a stroke
  1988. 2:46:18of genius. And I type in the right word. And it says, You win. So the program is now smart
  1989. 2:46:24enough to play this game with a guest limit. Let's go over one more time, what we did,
  1990. 2:46:30we created a bunch of variables up here, the secret word is pretty obvious. The guest is
  1991. 2:46:34just storing what they guessed, the guest count is keeping track of how many times the
  1992. 2:46:39user's guest right? So every time we run the code to allow them to guess the word, we're
  1993. 2:46:44going to increment it, the guest limit is just telling us how many times they can guess
  1994. 2:46:49in this out of guesses variable is basically going to tell us whether or not the user has
  1995. 2:46:54run out of guesses. And then down here we have our loop. And this loop is basically
  1996. 2:46:58just going to keep going and keep asking the user to guess the word. And it's going to
  1997. 2:47:02keep doing that as long as the guest is not equal to the secret word. And as long as the
  1998. 2:47:08user is not out of guesses. So if either of those conditions is false, we're gonna break
  1999. 2:47:12out of the loop, the first thing we do in the loop is we basically use this if statement,
  2000. 2:47:16and we say if guest count is less than guest limit. And so if the guest count is less than
  2001. 2:47:21the guest limit, in other words, if they haven't guessed three times in our case, then we're
  2002. 2:47:27going to prompt them for another guest. But if they have guest three times, then we're
  2003. 2:47:32going to set this out of guesses variable equal to true, and that's going to trigger
  2004. 2:47:36some breaking out of the loop. So if we do break out of the loop, there's two situations,
  2005. 2:47:40right? The first situation is that we won the game. And we guessed the secret word correctly.
  2006. 2:47:44But the other is that we ran out of guesses. So down here, I'm basically checking that
  2007. 2:47:48I'm checking if we're out of guesses. If we are, that means we lost otherwise, that means
  2008. 2:47:54we won. And so that's the basics of building this program. And I do just want to say, this
  2009. 2:48:00while loop up here, we use a simple while loop to do this. But you could also run this
  2010. 2:48:06program using a do while loop. And so if you want to challenge if you really understand
  2011. 2:48:11what's going on here, and you you know, you're pretty solid on what's going on, a good challenge
  2012. 2:48:15would be to try and build this program with a do while loop instead of a while loop. I
  2013. 2:48:20mean, it's gonna be pretty similar, a lot of the code can stay the same. But there are
  2014. 2:48:23going to be some key differences that you're going to have to try to account for. So if
  2015. 2:48:27you're looking for a challenge, try to build this with a do while loop. But otherwise,
  2016. 2:48:31that's sort of how you build a guessing game. And you know, In this tutorial, we were able
  2017. 2:48:34to integrate a lot of the stuff that we've learned up to this point in the course like
  2018. 2:48:38variables, we use all different types of variables, we use the loops, we also use if statements.
  2019. 2:48:42So hopefully this gives you an idea of how you could build something like this using
  2020. 2:48:47all that stuff. In this tutorial, I'm going to show you guys how to use a for loop in
  2021. 2:48:55C sharp. a for loop is a special type of loop, which allows us to keep track of what's called
  2022. 2:49:01an iterating variable. And basically, this is just a variable that's going to change
  2023. 2:49:06every time we go through our loop. So basically, it's it's kind of a situation where we have
  2024. 2:49:11a variable and generally that variable will like keep track of how many times we've gone
  2025. 2:49:14through the loop, or it can keep track of other things. But it's sort of a special scenario
  2026. 2:49:19in our programs where we want to use a loop. But we want to have a variable that's going
  2027. 2:49:23to be changing or doing something every time we go through that loop. And this is actually
  2028. 2:49:28really easy to understand. I'm just going to show you guys real quick this example that
  2029. 2:49:31I have down here, you'll notice that I have a simple while loop. So I said this integer
  2030. 2:49:36I over here I said equal to one. And I'm looping while i is less than or equal to five, right?
  2031. 2:49:42This is a very simple while loop and every time we go through the loop, I'm basically
  2032. 2:49:45saying console dot write line I'm printing out I and then I basically saying i plus plus
  2033. 2:49:51so a very simple loop. And but you'll notice here when I run my program, I'm going to go
  2034. 2:49:56ahead and run it right now that I basically printing out this variable i every time I
  2035. 2:50:02go through the loop, and you'll notice that the first time we go through the loop is equal
  2036. 2:50:07to one, the second time we go through the loop is equal to two, the third time it goes
  2037. 2:50:11through the loop is equal to three, this variable i is constantly changing every time we go
  2038. 2:50:18through the loop. And I is actually telling us what iteration of the loop we're currently
  2039. 2:50:23on. This is pretty cool, right? This variable i is essentially keeping track of the times
  2040. 2:50:29we've gone through the loop. And we can use this variable i to you know, print that out,
  2041. 2:50:34and to know where we're at, in the execution of the wild. This is actually a very common
  2042. 2:50:40situation. So a lot of times when we're writing loops in C sharp, we're going to want to have
  2043. 2:50:46a variable just like I a variable that's going to change every time we go through the loop,
  2044. 2:50:52right? In this case, I is changing every time it's getting incremented by one. So I is essentially
  2045. 2:50:57telling us how many times we've gone through the loop, but you can essentially use this
  2046. 2:51:00variable to do anything. So instead of just incrementing it by one, I can get incremented
  2047. 2:51:05by two or I can increment incremented by another value. And basically the idea here is that
  2048. 2:51:10we have a variable that we're using constantly as we go through this loop. And it's such
  2049. 2:51:15a common situation to have a variable like this, that there's an entirely separate loop
  2050. 2:51:21called a for loop, which is designed just for this situation. So down here, I'm going
  2051. 2:51:27to go ahead and create a for loop. And I'll show you guys how this for loop is actually
  2052. 2:51:31going to be able to do exactly what this while loop is doing but a lot cleaner and a lot
  2053. 2:51:36simpler. To create a for loop, I'm just gonna say for I'm gonna make an open and close parentheses
  2054. 2:51:40and open and close curly bracket. So so far, this is exactly like this Wilding, right,
  2055. 2:51:45it's basically structured the same exact way. But you'll notice in the while loop inside
  2056. 2:51:50of the parentheses, I only put one thing right I put the loop condition, you'll hear people
  2057. 2:51:54call this the loop guard, right, it's basically like the condition for when we should loop
  2058. 2:51:59inside of this for loops, parentheses, we're going to put three different things. So instead
  2059. 2:52:04of just having one thing, we're going to put three things, the first thing we're going
  2060. 2:52:08to put in here is a variable initialization. You'll notice up here in my while looping
  2061. 2:52:14structure, I have this integer I and over here, I'm saying and i is equal to one. But
  2062. 2:52:18this is actually outside of the while loop. So I had to do that separately from the actual
  2063. 2:52:23while loop in a four loop, I can actually do this same thing inside of this parentheses.
  2064. 2:52:28So I'm going to copy this, and I'm going to paste it over here. And you'll notice I'm
  2065. 2:52:32going to leave this semi colon in. So the first thing I'm going to do inside this for
  2066. 2:52:36loop parentheses, is I'm going to initialize this variable i. Now the next thing I want
  2067. 2:52:41to do, and you'll notice I'm separating these two things with a semicolon. The next thing
  2068. 2:52:46I want to do is write out my loop condition. So in the while loop, our loop condition is
  2069. 2:52:51right here, you know what I mean? It's just i is less than or equal to five, whatever
  2070. 2:52:54it's basically like telling us how long we should loop for, I can just take this, and
  2071. 2:52:59I can put this as the second item inside of these parentheses. So now I'm able to take
  2072. 2:53:05the variable initialisation and the loop condition. And finally, I'm going to put another semicolon
  2073. 2:53:11here. And I'm going to include one more thing over here. And this is going to be a line
  2074. 2:53:15of code that's going to get executed every time we go through the for loop. Now you'll
  2075. 2:53:20notice up here in this while loop, every time we go through this loop, we're doing something
  2076. 2:53:24special to this variable, every time we go through the while loop, we're incrementing
  2077. 2:53:29i and this is exactly what I want to do over here in my parentheses. So the last spot inside
  2078. 2:53:35of my parentheses over here, I'm just going to put in i plus plus. And like I said, this
  2079. 2:53:39is just going to be a line of code that's going to get executed after every single iteration
  2080. 2:53:45of our loop. So you'll basically see that I'm doing the same thing over here, as I am
  2081. 2:53:49over here, right over here, I'm defining this variable, I'm initializing this variable with
  2082. 2:53:54a value. I'm doing that down here in the parentheses. Over here, I'm specifying a looping condition.
  2083. 2:54:00I'm doing that down here in the parentheses. And then finally down here after every iteration
  2084. 2:54:05of the loop, I'm incrementing I. And I'm able to do that over here in this third section.
  2085. 2:54:11So basically, the for loop is just taking this situation where we want to have a variable
  2086. 2:54:16that's going to, you know, be used inside the loop. And it's compacting it into this
  2087. 2:54:21one single structure. And so now I can basically copy this line of code over here, paste it
  2088. 2:54:27into the for loop. And now these loops are identical. So these loops are actually going
  2089. 2:54:32to be doing the same exact thing. And I'm going to prove that to you guys. So I'm going
  2090. 2:54:36to go ahead and get rid of this while loop. And we only have the for loop now and let's
  2091. 2:54:40run our program and see what output we get. So I'm going to run the program and you'll
  2092. 2:54:44see we're getting the same exact output that we got before we're getting 12345 printed
  2093. 2:54:50out. So this for loop is doing exactly what that wildly was doing. It's just, it's a lot
  2094. 2:54:56more compact. It's a lot easier to manage and it's a lot better for us. So We walk you
  2095. 2:55:00guys real quick through how this gets executed. Essentially, what happens is, the first thing
  2096. 2:55:05we do is we create this variable and we give it an initial value. The second thing we do
  2097. 2:55:09is we check this condition. If the condition is true, then we're going to execute all of
  2098. 2:55:14the code inside of here. Once we're done executing all that code, then we're going to go up here
  2099. 2:55:19and increment AI, then we're going to check the condition. Again, if it's true, we'll
  2100. 2:55:23come back down here, and we'll execute this code, then again, we're coming back up here,
  2101. 2:55:28and we're going to do that and then we check the condition again. So it's continuously
  2102. 2:55:31looping through just like that. And this is the same as that while loop. So you could
  2103. 2:55:36do what we're doing here with a while loop, it's just that it's a lot more convenient,
  2104. 2:55:40and a lot easier to do it with a for loop. So that's kind of the basics of a for loop.
  2105. 2:55:44And before I end this tutorial, I want to show you guys one cool thing we can do with
  2106. 2:55:47these. Up here, I have this array that I created, and I just kind of commented it out. But I'm
  2107. 2:55:54going to copy it and bring it down here. And you'll notice this is just an array of lucky
  2108. 2:55:59numbers. So it's just an integer array, and I have these numbers in here, there's like
  2109. 2:56:02four different numbers, I'm gonna show you guys how we can use a for loop in order to
  2110. 2:56:07loop through and print out all of the elements inside of this array. So I just want to remind
  2111. 2:56:11you, the way that we can access an individual element in an array is by saying the arrays
  2112. 2:56:17name and then referring to the index of that value. So if I said lucky numbers zero, this
  2113. 2:56:21is referring to this for if I said lucky numbers two, this is referring to this 15. Right,
  2114. 2:56:27basically, that's how we can access an element inside of an array. For loops are really useful.
  2115. 2:56:32Remember, we have this indexing variable. So I can use a for loop to actually loop through
  2116. 2:56:37and print out all the elements inside of an array. So down here, so actually want to modify
  2117. 2:56:41a couple of values, I'm going to say int i, instead of setting it equal to one, I'm going
  2118. 2:56:45to set it equal to zero, because array indexes start at zero, so this element is element
  2119. 2:56:51zero inside of the array, then I'm going to change this condition. So instead of saying
  2120. 2:56:56i less than or equal to five, I want to say i less than or equal to lucky numbers dot
  2121. 2:57:03length. So this is going to allow me to access the length of the array. So when I say lucky
  2122. 2:57:08numbers dot length, this is going to tell me how many elements are inside of the array.
  2123. 2:57:13Then down here, I'm going to say console dot write line, and I want to print out lucky
  2124. 2:57:19numbers I. And the way this is going to work is the first time we go through this loop,
  2125. 2:57:24we're going to be printing out lucky numbers zero, right, because I is initially going
  2126. 2:57:30to be zero. So the first time we go through here, we're basically printing out lucky number
  2127. 2:57:34zero, the second time we go through, I gets incremented. So we're going to be printing
  2128. 2:57:38out lucky numbers one, the third time, we're going to be printing out lucky numbers two,
  2129. 2:57:42etc. So we can use this for loop in order to loop through this array. Let me show you
  2130. 2:57:47guys when I run this program, and actually, you know, I forgot to modify one more thing.
  2131. 2:57:51So instead of saying less than or equal to here, I'm just gonna say less than. And basically,
  2132. 2:57:56since we're starting at zero, I want to say i is less than lucky numbers dot link, that's
  2133. 2:58:03because the length of the array is going to be 123456. But the last element in the array
  2134. 2:58:10is an index position five. So if I say i is less than or equal to lucky numbers dot length,
  2135. 2:58:15then we're going to be trying to access lucky numbers six at one point. But the last element
  2136. 2:58:22is it lucky numbers five, so we have to say less than so I'm going to change this back
  2137. 2:58:26to an eye. And let's go ahead and run this. And you'll see that we'll be printing out
  2138. 2:58:30all the elements inside of that array. So we're printing out for 815 1623 42. So that's
  2139. 2:58:37one very common use of these four loops is you can use them to loop through all the elements
  2140. 2:58:43inside of an array. In this tutorial, I'm gonna show you guys how to build an exponent
  2141. 2:58:51function in C sharp, an exponent function is basically a function where we can pass
  2142. 2:58:56in two numbers, a base number and a power number. And it'll take the base number to
  2143. 2:59:01the power number. So essentially, it's gonna like if I passed in a two and a three, it
  2144. 2:59:07would give me two raised to the third power, right, or if I passed in a four and seven,
  2145. 2:59:13it would give me four raise to the seventh power. That's essentially what where you want
  2146. 2:59:18to write in this tutorial. So I'm gonna come down here below my main method, and I'm actually
  2147. 2:59:23going to create this function. So I'm just going to say static. And we're actually going
  2148. 2:59:28to return a integer. So why don't we just have this except integers, we'll just keep
  2149. 2:59:32it simple. And I'm just going to call this get POW. So this will basically like get the
  2150. 2:59:38power. And I'm going to make some open and close curly brackets. And in here we're going
  2151. 2:59:44to accept two parameters. So the first is going to be an integer base num, and the second
  2152. 2:59:50is going to be an integer power num. So we'll basically take the base num and raise it to
  2153. 2:59:59the pound right So a base number two and pound on three, then we'll do two cubed, right?
  2154. 3:00:03It kind of makes sense. So this is going to be a cool little method. And it kind of poses
  2155. 3:00:08an interesting challenge, because we don't know what base num and power num is. Right?
  2156. 3:00:15If we knew what power number was, for example, if we knew power number was two, then I could
  2157. 3:00:19just say, base num times base not right, I could just square base num, and we'd be good
  2158. 3:00:24to go. But I don't know what power num is. So I don't know how many times I need to multiply
  2159. 3:00:30base num by itself, in order to get the answers. So this kind of poses an interesting challenge.
  2160. 3:00:35And I'm gonna show you guys how we can use a for loop to solve this challenge. So the
  2161. 3:00:40first thing I want to do is just create an integer. And I'm just going to call this result.
  2162. 3:00:45And I'm just going to set result equal to one. And then eventually, what we want to
  2163. 3:00:49do is return result. So we want to modify result enough where it represents bass num,
  2164. 3:00:56taken to power numb, okay. And what we need to do is we need to keep multiplying result
  2165. 3:01:02times bass num, power num times. So let me show you guys, we can create a for loop. And
  2166. 3:01:07I'm just going to create a simple for loop. And remember, when we're creating for loops,
  2167. 3:01:12the first thing we want to do is create a variable. So I'm just going to say int i is
  2168. 3:01:17equal to zero, and I'm going to start this off at zero, then what I want to do is create
  2169. 3:01:23a looping guard. So what condition in other words, we're saying we want to keep looping
  2170. 3:01:28as long as this condition is true. So I want to keep looping as long as i is less than
  2171. 3:01:32a pound. And when I say this, this is basically telling C sharp that I want to keep looping
  2172. 3:01:39power num times. So if power num is equal to three, we're going to execute this loop
  2173. 3:01:44three times if count is equal to five, or executed five times etc. And the last thing
  2174. 3:01:48we want to put inside of this for loop is going to be something want to do after each
  2175. 3:01:52iteration of the loop. So I'm just going to increment i. And this is a very, very, very
  2176. 3:01:57simple for loop. So we're looping through here, power num times. So like I said, If
  2177. 3:02:03count is two, we'll go here through here two times. And every time I go through this loop,
  2178. 3:02:07I basically just want to multiply result, times base nom, so I can say result is equal
  2179. 3:02:13to result times base num, just like that. So the first time we go through this loop
  2180. 3:02:21result is going to be equal to one, right, so the first time we go through result is
  2181. 3:02:25going to be equal to one. So now result will be equal to one times based on right, the
  2182. 3:02:30first time through results gonna essentially become equal to based on the second time we
  2183. 3:02:35go through results equal to base num, right, so we're essentially multiplying base num
  2184. 3:02:40by base num, in other words, based on m squared, and storing it inside of this result variable.
  2185. 3:02:46And we're going to keep doing that through each iteration. So if we go through this loop
  2186. 3:02:50three times, then result is going to have the value of base num cube, if we go through
  2187. 3:02:55this loop, five times result will have the value of base num taken to the power of five,
  2188. 3:03:00right? And then down here, we can just return result. So by using this for loop, we're actually
  2189. 3:03:06able to figure out what the answer is. So up here inside my main method, let's go ahead
  2190. 3:03:11and run this. And I'm actually going to print out the answer. So I'll just say, console
  2191. 3:03:15dot write line, and we're going to write the result of get POW. So why don't we try to
  2192. 3:03:23do like three squared, right, so pass in three and two. And I'm going to go ahead and run
  2193. 3:03:29this program. So we should get the result of three squared, which is going to be nine.
  2194. 3:03:35And you can see over here, we get nine. Now I just want to point out this, this get power
  2195. 3:03:41method is only going to work for positive number powers. So we're not going to be able
  2196. 3:03:46to do like three and negative two. That's not going to work down here. But we can do
  2197. 3:03:52positive number exponent so I could say like three two, why don't we try like four and
  2198. 3:03:58three so this is going to be four cubed. So we should get 64 and yeah, we get 64 Alright,
  2199. 3:04:04so it looks like this methods working so once again I'll just walk you through what we did.
  2200. 3:04:10This get power method accepted two parameters bass num, and power num. So the goal was to
  2201. 3:04:16take bass num to power them. We created this for loop over here and essentially all I did
  2202. 3:04:22was I said I equal to zero. And we're going to keep looping as long as i is less than
  2203. 3:04:26power num and what that means is we're going to go through this loop power num times. So
  2204. 3:04:31if lb M was five that means we went through this for loop five times. And every time we
  2205. 3:04:36went through this for loop, we multiplied result which was initially equal to one, we
  2206. 3:04:42said result is going to be equal to result times base num. So the first time through
  2207. 3:04:46the loop, result was equal to one so we ended up getting result is equal to one times based
  2208. 3:04:51on so we got based on second time through the loop result was equal to base num. So
  2209. 3:04:56we said result is equal to base num times base. Not Essentially, the third time through
  2210. 3:05:01the loop result was equal to base num squared. So essentially, we were saying result is equal
  2211. 3:05:05to base num squared times base node based on cube. And that's essentially what we did.
  2212. 3:05:11And that was actually enough for us to get base num taken to pound them. So it's a pretty
  2213. 3:05:17simple method, although it is a little bit complex, because we use this for loop. So
  2214. 3:05:23what you should do is play around with this method, and you know, write it on your own
  2215. 3:05:27and see if you can really wrap your head around what's going on here. In this tutorial, I'm
  2216. 3:05:35going to show you guys how to use two dimensional arrays in C sharp, a two dimensional array
  2217. 3:05:40is basically a situation where we have an array, and every element inside of that array
  2218. 3:05:45is itself an array. So essentially, we're building like a little matrix structure inside
  2219. 3:05:50of our program. And I'm gonna show you guys how we can do that, it's actually pretty simple.
  2220. 3:05:54So if we want to create a two dimensional array, we can do it just like we would a normal
  2221. 3:05:58array. So I'm going to create an array of integers. So we'll just say ends. And now
  2222. 3:06:02what I want to do is I want to make an open and close square bracket and inside of this
  2223. 3:06:06open close square bracket, I'm just going to put a comma here. And basically, this is
  2224. 3:06:09telling C sharp that we want to have a two dimensional array. And I'm going to give this
  2225. 3:06:14a name. So I'm just going to call this number grid. And I'm going to go ahead and set this
  2226. 3:06:19equal to something. So now what we can do is we can actually fill this array with a
  2227. 3:06:24bunch of different elements. So I'm going to make an open and closed curly bracket,
  2228. 3:06:29and then I'm going to make a new line and down here, we're actually going to be able
  2229. 3:06:32to populate this array with elements. So if I was making a normal array, I would usually
  2230. 3:06:37just put in like a bunch of numbers, so I'd say like 1234, etc. But when we're making
  2231. 3:06:42a two dimensional array, all of the elements inside of this array need to be arrays, so
  2232. 3:06:49themselves, they have to be array, so the first element has to be an array, and the
  2233. 3:06:53second element would have to be an array, and the third element would have to be an
  2234. 3:06:56array excetera. All of these elements have to be arrays, and then I could give these
  2235. 3:07:01values. So I could say like 112. And then down here, we could say like three, four,
  2236. 3:07:09and we could say five, six. So basically, now we have a little matrix of numbers, right?
  2237. 3:07:15Not only does the number grid have three elements, but each of the elements inside of the number
  2238. 3:07:19grid has two elements inside of it. And this is a two dimensional array. So let's look
  2239. 3:07:24at how we can actually access some of these elements, what I can do is I can come down
  2240. 3:07:28here, and I'm just gonna say, console dot write line. And if I wanted to, like for example,
  2241. 3:07:34let's say I wanted to print out this element over here, I can just refer to the name of
  2242. 3:07:38the array. So I can say number grid, and then I'm going to make an open and close square
  2243. 3:07:42bracket. And in here, I'm going to put two numbers. So the first number I'm going to
  2244. 3:07:46put is going to be the element inside of the array. So it's going to be like this is the
  2245. 3:07:51first element, this is the second, this is the third, or this is element at index position
  2246. 3:07:55zero, this is element at index position one index position two, etc. So I'm gonna put
  2247. 3:08:00a zero because we're trying to access this one over here. And then I'm going to put another
  2248. 3:08:05zero, so I'm just going to put 00, and that's referring to row zero, column zero. Now when
  2249. 3:08:12I run this program, it's going to be able to print that out. So you'll see we're printing
  2250. 3:08:16out one, we're printing out that element over there. Let's say that I want to access this
  2251. 3:08:22four over here, well, this is going to be row 01. So I'm gonna put a one right here.
  2252. 3:08:28And then it's going to be 01. So it's going to be one, one. And now when we print this
  2253. 3:08:34out, we should get a four, which we do. So that's essentially how you access those elements
  2254. 3:08:40inside the array. And this works exactly like it does with a normal array. So if I wanted,
  2255. 3:08:45like I can modify one of these values, I can do everything that I would do in a normal
  2256. 3:08:50array. And that's kind of the basics. This is a structure that you might not use all
  2257. 3:08:53the time. But when you need it, it's extremely useful. And in addition to two dimensions,
  2258. 3:08:58you could also have like three, or four or five dimensions, all you have to do is just
  2259. 3:09:01put more like commas over here. There's also a situation where you might not know how many
  2260. 3:09:06elements you want to go in there initially. And you could actually just like declare one
  2261. 3:09:11of these, so I can say like and, and I'll make a two dimensional array, and we'll just
  2262. 3:09:15call this like my array. And if I don't know what elements I want to put in here, initially,
  2263. 3:09:21I can actually just say equals new int. And then over here, I need to give this some information.
  2264. 3:09:26So I'd have to say like two, three. And this is basically telling C sharp how many rows
  2265. 3:09:33and how many columns we want to have. So this would mean there's two elements and then each
  2266. 3:09:38of those elements has three elements inside of it. So that's another way that you could
  2267. 3:09:42do that. And then you could just manually like insert the elements. In this tutorial,
  2268. 3:09:49I'm gonna show you guys how to use comments in C sharp. Now comment is basically just
  2269. 3:09:54a special line or block of text in our program that's going to get ignored when we run program.
  2270. 3:10:00So comments are really useful for leaving little notes or comments inside of your program.
  2271. 3:10:05And a comment is essentially just a line of code that's meant for humans, and not meant
  2272. 3:10:11for the computer. And these can come in handy. So I'm going to show you guys basically how
  2273. 3:10:15these work and what we can do with them. If I wanted to create a comment, I can just come
  2274. 3:10:19down here and type two forward slashes just like that. And now you'll see that the text
  2275. 3:10:24in my text editor has changed color, it's now this green color. And that's basically
  2276. 3:10:28signifying that we have a comment here. So like I said, a comment is a little line or
  2277. 3:10:35a little block of code, that's actually going to get ignored by C sharp. So when I run my
  2278. 3:10:40program, anything that I have inside of this comment is just gonna get ignored. The comment
  2279. 3:10:45is for the programmer, so it's for me, or it's for another developer who comes in and
  2280. 3:10:50is reading my file. So I could leave like a little note here, I could say like, a to
  2281. 3:10:56do, like, you know, maybe this block of code needs to be done. Or I can even come in here
  2282. 3:11:01and like describe a line of code. So I could describe this line of code over here, I could
  2283. 3:11:04say like, this prints something out, etc. You could also take these comments and put
  2284. 3:11:11them after a line of code. So I can come over here and say, two forward slashes, and you
  2285. 3:11:16could type out, you know, a comment, whatever. And this is what we call a single line comment.
  2286. 3:11:23So a single line comment is a comment that can only be on one line. So if I was to make
  2287. 3:11:27a new line you see down here, this is no longer a comment. And you'll see it's actually throwing
  2288. 3:11:32an error because the program is trying to render it as actual code. But in addition
  2289. 3:11:37to single line comments like this, a lot of times, you're going to want to have comments
  2290. 3:11:41that are span multiple lines, one thing you can do is just make multiple lines like that.
  2291. 3:11:47And then you'll see a lot of developers do stuff like that, you can also use a starting
  2292. 3:11:50and an ending tag. So you can type in a forward slash, and a star. And now everything that
  2293. 3:11:55comes after this forward slash and a star in our file is going to be considered a comment.
  2294. 3:12:00That is until we end it off with another star and a forward slash. So I can say star forward
  2295. 3:12:07slash, and now you'll see only the stuff that's in between these two little tags, is going
  2296. 3:12:13to be considered a comment. And these can go on multiple lines. So those are basically
  2297. 3:12:17the two types of comments that we can use. And really a comment is extremely open ended.
  2298. 3:12:22I mean, there's no wrong way to use a comment. It's really however you want to use it. And
  2299. 3:12:27I do want to show you guys one other way these can be useful. So let's say that I'm writing
  2300. 3:12:31code, and you know, I want to try and run my code without a certain line in it. So maybe
  2301. 3:12:37I'm like, you know, have a program. And I think one line might be causing it to break,
  2302. 3:12:41or I just wanted to see what it would look like without a single line of code. Well,
  2303. 3:12:45one option would be to remove that line of code. So for example, I can take this line
  2304. 3:12:49of code, maybe this is a line of code that I think is causing my program problems, I
  2305. 3:12:54could delete it, and then I can go ahead and run the program, right, and then nothing gets
  2306. 3:12:58printed out. But here's the problem, when I do that, I have to actually like physically
  2307. 3:13:02remove the line of code. Instead, what I can do is comment it out. So I could just put
  2308. 3:13:07a comment in front of this. And now this whole thing is basically going to get ignored by
  2309. 3:13:12C sharp, so I can run the program. And we're still going to be running it without that
  2310. 3:13:16line of code. But I don't have to physically delete it from the file. And that just allows
  2311. 3:13:21me to keep track of it a little bit better. And you know, I can still have it there, it's
  2312. 3:13:25still taking up space in the file, it's just going to get ignored. So that can be a really
  2313. 3:13:30useful way. So really, I'd say comments, you know, are basically used for two things. The
  2314. 3:13:34first is like writing out little messages or notes, either for yourself or for other
  2315. 3:13:39programmers, or for commenting out lines of code. And you'll see people do both of those.
  2316. 3:13:44And they're both really awesome. So like I said, comments are pretty much open ended,
  2317. 3:13:47you can do whatever you want with them. But I'd say those are the two most common use
  2318. 3:13:51cases. In this tutorial, I'm going to show you guys how to use Exception Handling in
  2319. 3:14:00C sharp. A lot of times when your C sharp programs are running, there's gonna be certain
  2320. 3:14:04things that can go wrong. And generally, when those situations occur, C sharp is gonna throw
  2321. 3:14:10what's called an exception. An exception basically just tells you that something happened in
  2322. 3:14:14your code that C sharp couldn't handle. And whenever an exception gets thrown, generally
  2323. 3:14:19the program crashes. So there's a lot of situations where something might happen in the execution
  2324. 3:14:25of the program, and exceptions gonna get thrown and then the program is going to terminate.
  2325. 3:14:29And sometimes you're going to want that to happen. Sometimes when an exception gets thrown,
  2326. 3:14:33you're going to want the program to terminate because you're going to want to know what
  2327. 3:14:36happened. But a lot of times, especially if you're writing a program that's being designed
  2328. 3:14:40to run for, you know, months and months at a time, you're not going to want an exception
  2329. 3:14:45to crash the program. In other words, you're going to want the program to be able to keep
  2330. 3:14:48going when something wrong happens and an exception gets thrown. So I'm going to show
  2331. 3:14:54you guys the basics of working with exceptions. We're also going to talk about handling exceptions
  2332. 3:14:58and the different scenarios. where an exception is gonna get thrown down here I have a basic
  2333. 3:15:03program written out. And this is a very simple program, essentially all it does is it gets
  2334. 3:15:07two numbers from the user. And it takes those numbers, and it divides them. So you see over
  2335. 3:15:12here, I have console dot write, I'm asking them to enter a number. And then I'm taking
  2336. 3:15:17whatever they entered, and I'm converting it into an integer. And I'm storing it inside
  2337. 3:15:22of this integer variable. And I'm doing that for both of these numbers, then down here,
  2338. 3:15:26I'm just writing out the value of dividing both numbers onto the screen. So there's two
  2339. 3:15:31major things that we're doing here. The first is we're converting this string into an integer
  2340. 3:15:37using this convert instruction. And then down here, we're dividing the two numbers, and
  2341. 3:15:41I'm gonna show you guys how we could actually enter in information to this program that
  2342. 3:15:46will cause exceptions. So let's go ahead and run this. And I'm just gonna click Start.
  2343. 3:15:51So over here, I'm going to answer a number. So why don't we enter in a five. And now I'm
  2344. 3:15:55going to show you guys one way that an exception could get thrown. So remember, we're dividing
  2345. 3:16:00these two numbers, and you can't actually divide a number by zero. So just by basic
  2346. 3:16:05math rules, if we have a whole number like this, it's impossible for me to divide it
  2347. 3:16:10by zero. So essentially, what I'm doing here is I'm asking the computer to do something
  2348. 3:16:15impossible, right? I'm asking the computer to do something that it's not going to be
  2349. 3:16:19able to do, and it's not going to be able to handle when I hit Enter, and I asked the
  2350. 3:16:23computer to do this, it's gonna throw an exception. In other words, the computer is going to be
  2351. 3:16:27like, Hey, I can't do this, like I can't divide these two numbers. So the program is going
  2352. 3:16:32to terminate. So you'll see when I hit Enter, that this little screen goes away, and this
  2353. 3:16:36exception pops up. So it says exception, unhandled. System dot divide by zero exception attempted
  2354. 3:16:43to divide by zero. It's basically telling us what went wrong, right? saying, hey, you
  2355. 3:16:48tried to divide by zero, that's illegal, you can't do that. And we can't handle that. So
  2356. 3:16:53basically, it's throwing this exception. So you can just come up here and click stop debugging.
  2357. 3:16:57And that'll make that go away. I'm gonna show you guys one other way we can break this program.
  2358. 3:17:01So if I put a number in here, like a four, that's all well and good, right? But remember,
  2359. 3:17:07we're converting whatever is up there into a number. So if I was to type in like a G,
  2360. 3:17:12this isn't a number, right? G is not a number, it has no numerical value. So the program
  2361. 3:17:17isn't going to be able to convert g into an integer, even though I asked it to. So when
  2362. 3:17:22I hit Enter, another exception is going to get thrown This time, we're getting a format
  2363. 3:17:27exception, it says input string was not incorrect format. In other words, we asked you to enter
  2364. 3:17:32a number and you entered in a character, the program can handle that. So it breaks. That's
  2365. 3:17:36another example of an exception. So the question becomes, what do we do about this? Right?
  2366. 3:17:42Our program keeps throwing these exceptions. So how can we handle that like, if the user
  2367. 3:17:47enters in a letter, instead of a number, I don't want the program to terminate, I want
  2368. 3:17:50to be able to do something else, like I want to be able to yell at them and be like, hey,
  2369. 3:17:53you entered in the wrong thing, I don't want the program to terminate. Or if the user tries
  2370. 3:17:57to divide by zero, you don't want the program to terminate, you want to tell them that they
  2371. 3:18:01entered in an invalid number. So if we want to do that, we can actually do something called
  2372. 3:18:06Exception Handling exception handling is basically the process of catching these exceptions.
  2373. 3:18:11So we're basically going to tell C sharp like, hey, there's some code over here that might
  2374. 3:18:16throw an exception. If it does, we want you to catch it. And instead of crashing, we want
  2375. 3:18:20to be able to handle it. I'm going to show you guys how we can do that. So we want to
  2376. 3:18:23use something called try catch blocks. So I'm just going to type out try and open and
  2377. 3:18:28close parentheses, curly bracket. And then down here, I'm going to type out catch, and
  2378. 3:18:36again, an open and closed curly bracket. So try catch blocks work like this, any code
  2379. 3:18:42that we think is going to break the program, for example, the code down here, we can put
  2380. 3:18:47inside of these try blocks. And if that code does indeed break the program, then instead
  2381. 3:18:52of the program terminating, the program is going to go down here into this catch block.
  2382. 3:18:56And it's going to execute all the code down there. So what I want to do is take all of
  2383. 3:19:00my risky code, in other words, all of this stuff, and I can place it inside of these
  2384. 3:19:04try blocks. So I'm just going to paste it inside of here. So now all of this code that
  2385. 3:19:09potentially would break the program is inside of this try block. So what I want to show
  2386. 3:19:13you guys, now that I have this catch block over here, I can just do like console dot
  2387. 3:19:18write line, and over here, I'm just gonna print out error. So essentially, what's going
  2388. 3:19:25to happen now is if any of this code causes an exception, we're going to come down here
  2389. 3:19:29and print out there. So let me show you guys, I'm going to go ahead and run this program,
  2390. 3:19:33and it says enter a number. So let's do the 01. Again. So now I'm going to hit zero, this
  2391. 3:19:38should break the program. But now instead of breaking the program and causing an exception,
  2392. 3:19:43we just print out error. So it's, it's kind of like a backup system. And what we can also
  2393. 3:19:48do is we can get more information about the error that occurred so I can say catch, I
  2394. 3:19:52can make an open and close parentheses and then I can say, exception, and I can just
  2395. 3:19:57call this E. So basically saying that this catch block is going to take a an exception
  2396. 3:20:02parameter and we're going to call it E. If I came over here now I can basically print
  2397. 3:20:06out E. And I can say e dot message. And this is going to tell me what went wrong. So now
  2398. 3:20:13when I run my program, and when we can do that zero division, I'll say five divided
  2399. 3:20:18by zero, and it's going to tell me what went wrong, it's gonna tell me you attempted to
  2400. 3:20:22divide by zero, if I did the other error, so for example, if I entered in a letter like
  2401. 3:20:28GE, now it's going to tell me input string was not in correct format. So it's able to
  2402. 3:20:34catch the error that happened. And by saying exception II appear in these parentheses,
  2403. 3:20:39we can actually get the error message just like that. So this is a really great way to
  2404. 3:20:44kind of stop our programs from terminating. But here's the thing, there's all different
  2405. 3:20:49types of exceptions that can get thrown inside of our programs, right. And all I have down
  2406. 3:20:53here is one catch block. So no matter what exception gets thrown, we're always executing
  2407. 3:20:58this same code down here. But let's say there was a situation where when the zero division
  2408. 3:21:03error occurred, you wanted to do one thing. And when the input error occurred, you wanted
  2409. 3:21:08to do a different thing, we can't really do that, because we only have one catch block.
  2410. 3:21:13Well, what we can do is we can actually define specific exceptions that we want to catch.
  2411. 3:21:18So down here, instead of saying exception, I can actually specify a specific type of
  2412. 3:21:23exception that I want this catch block to catch. So before we were getting a divide
  2413. 3:21:29by zero exception, so I can put this in here. And now this is only going to catch a divide
  2414. 3:21:35by zero exception. So if I was to run my program, and we did the divide by zero, I'll say five
  2415. 3:21:41zero, you'll see it's able to catch it. But if I was to do that invalid format, but I
  2416. 3:21:46came over here, and I said, like six, and then I said, Gee, it's not going to be able
  2417. 3:21:50to catch that. So down here, we're getting this format exception, right. And basically,
  2418. 3:21:56it's not being caught because we didn't specify it down here, this catch block is only going
  2419. 3:22:02to catch divide by zero exceptions. But what we can actually do is we could make another
  2420. 3:22:07one. So I could say, catch, and in here, I'm just gonna type in format exception. And now
  2421. 3:22:12this is going to be able to catch any format exceptions that occur. So now I can say console
  2422. 3:22:16dot write line. And again, we can just print out like e dot message. And so the idea is
  2423. 3:22:22that instead of just printing out a message, you would do something different depending
  2424. 3:22:26on the error that gets thrown. In our case, though, we're just gonna say e dot message.
  2425. 3:22:30So now when I get that format exception, if I say like five, and then G, it's gonna say
  2426. 3:22:36input string was not in correct format. So it's able to catch both of those. And that's
  2427. 3:22:41essentially what you want to do. So what we were doing before is we just had, like, catch,
  2428. 3:22:46and we were just kind of catching any exception under the sun. But now we're catching individual
  2429. 3:22:51exceptions. And this is actually a better way to do it. Because we're able to account
  2430. 3:22:56for each of these situations, right? When the user divides by zero, we could print out
  2431. 3:23:00like, Hey, you divided by zero, like watch it. If they have a format exception, then
  2432. 3:23:05we could do something else, like maybe we could prompt them to enter that input again,
  2433. 3:23:08or, you know, we could do something else. And so being able to have these multiple catch
  2434. 3:23:13blocks, is extremely useful. There's also one more thing I want to show you guys, which
  2435. 3:23:17is called finally So down here, I can just type up. Finally, and I'm gonna make an open
  2436. 3:23:22and closed curly bracket. And any code that we put inside of this, finally, block is always
  2437. 3:23:29going to get executed no matter what, right no matter what it's going to get executed.
  2438. 3:23:33So for example, up here, this code, like if the user enters in an invalid number, like
  2439. 3:23:40if they enter in a letter here, instead of a number, this code won't get executed, right?
  2440. 3:23:45Because we threw the exception, and we broke and we came down here. But sometimes in your
  2441. 3:23:49programs, even if something throws an exception, you're always going to want to execute something,
  2442. 3:23:53and that code can go down here in the finally block, but you don't always need finally blocked
  2443. 3:23:59off kind of optional. So really, this is just how we can handle different errors in our
  2444. 3:24:04program. And there's a bunch of different exceptions. If you just go online and you
  2445. 3:24:07know, search for like C sharp exception list, you'll find a bunch of these different exceptions.
  2446. 3:24:12And this really just makes your programs more durable. Like before, this was a very fragile
  2447. 3:24:16program. If I entered in a letter instead of a number, like he would just crash and
  2448. 3:24:19burn. But now we're actually able to handle all those different situations using these
  2449. 3:24:23try catch blocks. So these are extremely useful. And anytime you have code that's a little
  2450. 3:24:28dicey, especially if you're like getting input from a user and stuff like that, these can
  2451. 3:24:32come in handy. In this tutorial, I'm going to show you guys how to use classes and objects
  2452. 3:24:41in C sharp classes and objects is actually a big subject. And there's a lot for me to
  2453. 3:24:46talk about in this particular topic, but this video is just going to be a very basic introduction
  2454. 3:24:52into what classes and objects are and how we can start using them in our C sharp programs.
  2455. 3:24:58Now in C sharp, we're going to be dealing With all types of data, and a lot of times
  2456. 3:25:02when you're writing your programs, you're going to want to work with and represent different
  2457. 3:25:06things in your programs. And by default, C sharp gives us a bunch of different data types
  2458. 3:25:12that we can work with. So in C sharp, we can represent things like text. So we can use
  2459. 3:25:17strings to represent text, we can represent characters, we can represent whole numbers
  2460. 3:25:21with integers. And we can represent decimal numbers with things like doubles and floats,
  2461. 3:25:25right, we can also represent true and false values with Boolean variables. But here's
  2462. 3:25:30the problem is with just those limited data types, we're not going to be able to represent
  2463. 3:25:36or model everything that we'd want in our programs. In other words, there's a lot of
  2464. 3:25:41things in the real world that can't just be represented using a string or a number or
  2465. 3:25:46a Boolean, right. I mean, think of something like a phone, for example, like imagine I
  2466. 3:25:50wanted to represent a phone inside of my program, well, I can't represent a phone with like
  2467. 3:25:56a string, you know, I can't represent a phone with just a number, like a phone is a complex
  2468. 3:26:01entity in the real world. And I can't sort of break it down into just one single, like
  2469. 3:26:08string or number, right, I can't represent it using just one of those data types. And
  2470. 3:26:13that's kind of one of the limitations that you're going to encounter is like, you can't
  2471. 3:26:17represent everything just using one of these data types. So what I want to talk to you
  2472. 3:26:21guys about today is something called classes and objects. And classes. And objects essentially
  2473. 3:26:26allow you to create your own custom data types. By default, were given things like strings,
  2474. 3:26:33numbers, and Booleans. But I could actually create my own data type that would allow me
  2475. 3:26:37to represent something that I wanted inside of my program. So generally, what we'll do
  2476. 3:26:42when we want to create a data type in C sharp is we'll create something called a class.
  2477. 3:26:47And a class is basically just a specification for a new data type. So I could create a class
  2478. 3:26:53for a phone or I could create a class for like a water bottle, or I could create a class
  2479. 3:26:58for a keyboard or a computer. And then I could represent all those different things inside
  2480. 3:27:04my programs. So classes are basically use to model real world entities inside of our
  2481. 3:27:10program. So I'm going to show you guys how we can build a class. In order to do that.
  2482. 3:27:15The first step when you want to build a class is you want to open up your solution explorer
  2483. 3:27:18over here, and my case is just over here on the right side of my screen, and I'm going
  2484. 3:27:21to go down to my project. So I'm over here in my draft project. And when I want to create
  2485. 3:27:26a new class, I can just right click, and I'm going to go down to add, and I'm just going
  2486. 3:27:29to click New Item. And then over here, we should get this little menu that pops up and
  2487. 3:27:34it says, class, this is the first one right up here. So I'm going to select that. And
  2488. 3:27:39then I can actually come down here to the bottom and give this a name. So I'm going
  2489. 3:27:42to go ahead and name my class. And generally when we're creating a class in C sharp, you
  2490. 3:27:46want to name it with a capital letter. so in this situation, I think we should create
  2491. 3:27:51a class for a book. So let's say that I was writing a program, maybe this was a program
  2492. 3:27:56for like a library or a school bookstore. And I wanted to be able to represent a book
  2493. 3:28:02inside of my program. So I want to create a book data type. So just like I create a
  2494. 3:28:07string, or I create an integer, or I create a Boolean, I want to be able to create an
  2495. 3:28:11actual book inside of my program. So I'm gonna create a book class. And I'm just going to
  2496. 3:28:15call this book.cs. And I'll hit Enter. And you guys will see over here that another file
  2497. 3:28:20has been added to our solution explorer. So now we have this book.cs file. And you'll
  2498. 3:28:26notice that this is pretty much laid out exactly like the other one. So exactly like this program
  2499. 3:28:33class that we were using over here. And it's basically just like an empty class. So remember
  2500. 3:28:38what I said a class is essentially just a specification for or really, it's like a blueprint
  2501. 3:28:43for a new data type in our program. And one of the cool things about C sharp is it actually
  2502. 3:28:48allows us to, you know, specify a custom data type. And essentially, what we want to do
  2503. 3:28:54is we're going to be creating a complex datatype. So a string, for example, is a data type,
  2504. 3:29:00right. And all it is is just plain text. A number is a data type. And all it is is just,
  2505. 3:29:05you know, a number, right, we have integers, we have floats that are whole numbers and
  2506. 3:29:09decimal numbers. But when we create this book data type, it's actually going to be composed
  2507. 3:29:15of all of those other data types. So inside of here, we're going to define a series of
  2508. 3:29:19attributes. And these attributes are basically going to describe what a book is inside of
  2509. 3:29:25our program. And that's generally what you want to do. When you create a class, we're
  2510. 3:29:28going to take a class, and you're going to give it a series of attributes. And like I
  2511. 3:29:33said, those attributes will kind of define what it is. So I have this book class. And
  2512. 3:29:39I want to think about what are the different attributes that are going to allow me to represent
  2513. 3:29:43a book inside of my program. And what I want to do is I want to actually create variables
  2514. 3:29:48for each of those. So let's think about this. What are the different attributes of a book
  2515. 3:29:52Well, I can think of the first one is going to be title. So over here, I'm gonna say string
  2516. 3:29:57title. And before I say string title, I'm gonna say Public. So I want to say public
  2517. 3:30:02string title. And I'm actually going to make this lowercase. So that's one attribute that
  2518. 3:30:07we can store about a book. What's another one? Well, let's think we would also want
  2519. 3:30:12to store the book's author. So I can say public string author. And let's just try to think
  2520. 3:30:18of one more. So I think one more good one might be the number of pages. So I'm gonna
  2521. 3:30:22say public, int pages. And all of these different variables that we're declaring over here are
  2522. 3:30:29called class attributes. And these are essentially pieces of information that are describing
  2523. 3:30:36what a book is in our program. So I'm essentially modeling the book data type, I'm creating
  2524. 3:30:42a blueprint for the book datatype, I'm saying that inside of our program, a book is going
  2525. 3:30:47to have a title, an author, and a number of pages. So this is a specification, it's a
  2526. 3:30:53template. Now, what I can do is I can go off to my other program, and I can actually create
  2527. 3:30:59a book inside of my program. So just like I create a string, or I create an integer,
  2528. 3:31:04or I create a double or a Boolean, I can create a book, and I can store it inside of a variable.
  2529. 3:31:10And we can actually create something called an object, which is going to be a actual book
  2530. 3:31:15inside of our program. So I'm gonna show you guys how we can do that, I'm going to save
  2531. 3:31:18this file. And I'm going to head back over to program.cs. So this is like the file that
  2532. 3:31:23has our main method over here. And I want to show you guys how we can create a book
  2533. 3:31:28now. So if I wanted to create an represent an actual book inside of my program, since
  2534. 3:31:34I've created that class, and I define what a book is, I basically defined the book datatype,
  2535. 3:31:40I can actually do that. And I can create something called an object. And an object is an instance
  2536. 3:31:47of a class. So when we created the class, we created the template for what a book is,
  2537. 3:31:52when we create an object, we're creating an actual physical book inside of our program,
  2538. 3:31:58right? So over here, this class specified like a title, author pages, right? This is
  2539. 3:32:03a specification for what a book is, when I create a book object, I'm creating an actual
  2540. 3:32:10book with an actual title, actual number of pages and an actual author. So I'm gonna show
  2541. 3:32:14you guys how we can do that. The first thing I want to do is I'm basically going to create
  2542. 3:32:18this, like I would a normal variable. So the first thing I'll do is specify the data type.
  2543. 3:32:22And in our case, we're going to create a book using the book data type. So just like if
  2544. 3:32:26I was creating a string, I would say, like string, you know, whatever is equal to whatever.
  2545. 3:32:31Here, I'm just gonna say book. And we're gonna do the same thing. So I'm gonna say book,
  2546. 3:32:35I'm going to give this a name. So why don't we just call it book one. And now what I'm
  2547. 3:32:40going to do is set it equal to new book, and an open and close parentheses. And this is
  2548. 3:32:46how we can create what's called a book object. This is essentially an instance of a book.
  2549. 3:32:52So this is like a physical book inside of our program. So the class is a specification,
  2550. 3:32:58the class tells us what the book data type is, an object is an actual instance of that
  2551. 3:33:04class. All right, hopefully that makes sense. So now I've actually created this book called
  2552. 3:33:09book one. And we can use book one over here, just like a variable, I mean, it's essentially
  2553. 3:33:14just a variable that's storing a book object. So remember, the book has all these attributes,
  2554. 3:33:21right? So a book can have a title, a book can have an author and a book can have a number
  2555. 3:33:26of pages. So when we create our book, we want to give it all of those attributes. So what
  2556. 3:33:31I can do is I can say like book one dot, and now I can access the attributes. So I could
  2557. 3:33:36say book one dot title, and I can actually give this a value. So let's say that we wanted
  2558. 3:33:41to create an object that would represent one of the Harry Potter books. So I could say
  2559. 3:33:45book, one dot title, is equal to Harry Potter. So now the title of my book is going to be
  2560. 3:33:52Harry Potter. Let's come down here. And we'll say book, one dot author, and the author of
  2561. 3:33:57the Harry Potter books is JK Rowling. So I'm gonna say book, one dot author is equal to
  2562. 3:34:03JK Rowling. And then finally, down here, we're gonna say book one dot pages. And let's just
  2563. 3:34:08say that this Harry Potter book has 400 pages in it. So what I did was I created a book
  2564. 3:34:13object was, which is an instance of the book class. And I was able to define all of the
  2565. 3:34:19pieces of information about this book. So I define what the book's title was, I define
  2566. 3:34:24what the book's author was, and I defined how many pages the book had. And so now what
  2567. 3:34:28I can actually do is I can work with this piece of information. So I can say like console
  2568. 3:34:33dot write line, and over here, I could print out some attributes of the book. So I could
  2569. 3:34:39say like book one dot title, and now when I run my program, it's going to print out
  2570. 3:34:44the title of the book we just created. So it should print out Harry Potter. I can do
  2571. 3:34:49the same thing for the other attributes. So I could say like book one dot pages, and now
  2572. 3:34:54we're going to print out however many pages book one has, so we're going to get 400. So
  2573. 3:34:59essentially, I'm creating here a book, right? I defined the book data type in that class.
  2574. 3:35:05And now I'm creating an actual book inside of my class, which we're calling an object.
  2575. 3:35:10And what's cool about these objects is I can create as many objects as I wanted. So I could
  2576. 3:35:13come down here, and I could actually make another object. So I'm going to copy this
  2577. 3:35:17guy. And I'm actually going to paste it down here. And instead of being book one, I'm going
  2578. 3:35:22to change it to book two. So this is going to be Book Two, Book Two, Book Two, book two.
  2579. 3:35:27So let's start giving this some different information. Let's say this object is going
  2580. 3:35:31to represent one of the Lord of the Rings books. So I could say, Lord of the Rings,
  2581. 3:35:37and then the author is token and let's say the Lord of the Rings has like, I don't know,
  2582. 3:35:42700 pages in it. So now I have a book up here called book one, which is representing the
  2583. 3:35:48Harry Potter book. And then I have another book down here, Book Two, which is representing
  2584. 3:35:53the Lord of the Rings book. And I'm actually able to have an actual book in my program,
  2585. 3:35:58right, so I'm able to represent and store and work with and model a book inside my program.
  2586. 3:36:05So down here, I could do the same with Book Two, I could say like Book Two, dot author,
  2587. 3:36:11and now this is going to print out the author of book two. She'll see over here, we get
  2588. 3:36:16token. Now I want to bring up one point. So both of these objects are books right over
  2589. 3:36:24here. In this book class, I defined the book datatype, I basically told C sharp like, Hey,
  2590. 3:36:29we got a new data type. It's a book, it's awesome. All books have a title and author
  2591. 3:36:34and a number of pages. And over here, I created individual books off of that blueprint off
  2592. 3:36:40of that template. Right. So book, one is its own book. It has its own title, its own author
  2593. 3:36:46and its own pages, Book Two, down here is another book. And it has its own title, its
  2594. 3:36:53own author and its own pages. So if I was creating an application for like a library,
  2595. 3:36:58for example, let's say the library had 10,000 books. Well, I could create 10,000 objects,
  2596. 3:37:04all of which would have the title, the author and the number of pages of each one of those
  2597. 3:37:09books. And then I could work with those books, I could pass them into methods, I could use
  2598. 3:37:14them in if statements, I can do all sorts of stuff with those books, just like I would
  2599. 3:37:18with other data types, like strings, numbers, or Booleans. So classes and objects are extremely
  2600. 3:37:25powerful. And really, this is just scratching the surface. This is like lesson one. You
  2601. 3:37:29know, this is the bare basics of working with classes and objects. As we go forward. For
  2602. 3:37:33the next few videos, we're gonna learn all sorts of ways to make these a lot more powerful.
  2603. 3:37:42In this tutorial, I want to talk to you guys about using constructors in C sharp classes.
  2604. 3:37:48A constructor is basically a special method that we can put inside of a C sharp class,
  2605. 3:37:54which is going to get called whenever we create an object of that class. So anytime I create
  2606. 3:38:00an object of a specific class, the constructor method will get called. And we can do different
  2607. 3:38:05things. So I'm going to show you guys how we can leverage that constructor method in
  2608. 3:38:09order to make it a lot easier for us to create objects off of the classes that we create
  2609. 3:38:15in C sharp. So down here, if you're not following along with the course, I basically created
  2610. 3:38:20a class over here called book. And I just created this book class. And I basically said
  2611. 3:38:26that the book is going to have a title and author and a number of pages. That's kind
  2612. 3:38:30of all this book is. And then over here in my program.cs, and my main method, I created
  2613. 3:38:36two books. So I created a Harry Potter book, and I created a Lord of the Rings book. Now
  2614. 3:38:41I want to show you guys how we can use a constructor and we're actually going to be able to use
  2615. 3:38:45a constructor to make it a lot easier for us to create these objects. But before we
  2616. 3:38:50do that, I just want to give you a introduction into what constructors are, and kind of show
  2617. 3:38:54you what's happening. So over here in my class, I'm actually going to create a method down
  2618. 3:38:59here, I'm just gonna say public book, I'm gonna make an open and close parentheses and
  2619. 3:39:04open and close curly bracket. Now you'll notice that I said public and then I said the name
  2620. 3:39:09of the class. And this is what's called a constructor. So anytime I make a method like
  2621. 3:39:14this, where I'm saying public, and then the name of the class, this means that this is
  2622. 3:39:18going to be the constructor for this class. And like I told you guys before, the constructor
  2623. 3:39:23is basically a special method inside of our class, that's going to get called when we
  2624. 3:39:28create an object of this class. So let me prove that to you guys. I'm going to come
  2625. 3:39:32down here and I'm just going to type out console dot write line. And I'll make an open and
  2626. 3:39:37close parentheses and down here, I'm just going to write creating book. Okay, very simple.
  2627. 3:39:43Now over here in my program in my main method, I'm going to run this and what you're going
  2628. 3:39:49to see is every time I create a new book, this line and this line that is going to get
  2629. 3:39:54executed, so I'm going to run the program and you'll see over here it says creating
  2630. 3:39:58book and creating Book, essentially, what's happening is when I come down here and I say
  2631. 3:40:03new book, what I'm doing here is I'm actually calling that constructor method. So this right
  2632. 3:40:11here, this line where I'm saying new book, this book is directly linked to this method
  2633. 3:40:18over here. And again, I'm going to prove this to you. So over here, what I'm going to do
  2634. 3:40:23is I'm going to say that this book method, this constructor should accept one parameter.
  2635. 3:40:28So I'm gonna say, string name, that's all I'm gonna say. And then down here, I'm just
  2636. 3:40:33gonna print out name. So this method is accepting one parameter name, and then down here, we're
  2637. 3:40:39gonna print it out. Over here, you'll notice I'm already getting errors. And that's because
  2638. 3:40:44I didn't include a parameter. So I can come over here and say, like, Mike, and then down
  2639. 3:40:48here, we can pass another name like john. So now when I run my program, it's going to
  2640. 3:40:53print out Mike, and it's going to print out john, because I'm calling that book constructor.
  2641. 3:41:00Awesome. So not only does this call that constructor, but from here, we can also pass parameters
  2642. 3:41:10into that constructor. And that is extremely powerful. So I'm going to show you guys what
  2643. 3:41:15we can do with that information. Now, let's talk about the code that we have over here,
  2644. 3:41:21you'll notice that I have a bunch of code here. So when I created this Harry Potter
  2645. 3:41:25book, it took me four lines of code, right? When I created this book to this Lord of the
  2646. 3:41:30Rings book took me another four lines of code, total of eight, eight lines of code just to
  2647. 3:41:35create two objects, right, just to create two books, I had to write out eight lines
  2648. 3:41:39of code. Not only that, I had to manually type out book one dot title is equal to Harry
  2649. 3:41:44Potter book, one dot author is equal to JK wrong, like, that's a huge drag. You know,
  2650. 3:41:49having to do that every single time you want to create a book is extremely annoying. And
  2651. 3:41:54imagine if we were instead of creating two books, we were trying to create 10 or 12,
  2652. 3:41:58or 20 books, right, it would take forever, because we'd have to manually go through and,
  2653. 3:42:03you know, assign a title and an author and a number of pages. So this is a totally valid
  2654. 3:42:08way to create our object and give it some attributes. But there's actually another way,
  2655. 3:42:12and we can use that constructor to do it. So essentially, what I want to do is I want
  2656. 3:42:17to allow the caller to pass in a title and author and a number of pages into this book
  2657. 3:42:25constructor. So when they create the book, I want them to be able to tell me what the
  2658. 3:42:30title is going to be, what the author is going to be and what the number of pages is going
  2659. 3:42:34to be. That way, they can just tell me right away, and I can handle it up front. And then
  2660. 3:42:39I don't have to say like book, one, title is equal to whatever book one dot author is
  2661. 3:42:44equal to whatever. So let's see if we can wire that up over here. Again, this book constructor
  2662. 3:42:50is going to take in three parameters. And we already saw before that over here, we can
  2663. 3:42:57pass parameters into here, right, so I can pass a parameter into here, and it's going
  2664. 3:43:01to get passed into that constructor. So I'm going to say that this constructor is going
  2665. 3:43:05to take three parameters, the first is going to be a string, and I'm going to call this
  2666. 3:43:09a title. And this is going to stand for argument title. And you guys will see why I'm naming
  2667. 3:43:14this differently in a second. And then I'm going to make another string, I'm going to
  2668. 3:43:17call it a author. And then I'm going to make an int, and I'm call it a pages. Now, essentially,
  2669. 3:43:25what's happening is whenever we create a book, we need to pass in the title, the author,
  2670. 3:43:31and the number of pages. And so what I can do now is from inside of this constructor,
  2671. 3:43:37I can just come down here and I can say, title is equal to a title. And basically, what I'm
  2672. 3:43:46saying is this title up here, the title of the object is going to be equal to the title
  2673. 3:43:53that they passed in. So whenever we create a new book, now we're going to pass in a title.
  2674. 3:43:58And down here, I'm going to say that the title of the object is going to be equal to a title.
  2675. 3:44:03And so now instead of having to say like book, one dot title is equal to whatever. Now I
  2676. 3:44:09can just pass the title in over here. And I'll just assign the title from over here.
  2677. 3:44:14So essentially, doing this is the same as saying book one dot title is equal to whatever,
  2678. 3:44:18it's the same exact thing, except I can just do it straight from in here. So what we want
  2679. 3:44:24to do is do this for each of these attributes. So I can say, author is equal to A author.
  2680. 3:44:31And then we can say pages is equal to A pages. And I just want to point out, you can name
  2681. 3:44:36these whatever you want. So I can even name this title if I wanted. Or I could name this,
  2682. 3:44:41you know, whatever, it doesn't matter. I'm just using a title, because it kind of shows
  2683. 3:44:46that this is the argument that got passed in. And it kind of distinguishes it from the
  2684. 3:44:51actual attribute of the object, but you can name it whatever you want. Alright, so now
  2685. 3:44:55when we go back over to our program, you notice we're getting errors here and it's basically
  2686. 3:44:59saying There's no argument given that corresponds to the required formal parameter. In other
  2687. 3:45:04words, hey, you need to pass in all this information now. So what I can do is I can take this information
  2688. 3:45:10from down here, and I can pass it into here. So I can pass in the title Harry Potter, I
  2689. 3:45:15can pass in the author, JK Rowling. And I can pass in the number of pages 400. So instead
  2690. 3:45:23of having to do that manually, I can just get rid of all this. And now we have the same
  2691. 3:45:28thing. So I'm passing in the title, the author and the number of pages. And then over here
  2692. 3:45:34in the book, I'm taking those parameters that got passed in, and I'm assigning them to the
  2693. 3:45:39attributes of the object. And that is super cool and super useful. So let's do the same
  2694. 3:45:44thing for Lord of the Rings, we can copy this paste this bad boy up here. So I can get rid
  2695. 3:45:50of all of this stuff now. And essentially, we have the same exact thing. So we went from
  2696. 3:45:55having eight lines of code to create these two objects. So now we just have two lines
  2697. 3:46:00of code one each, and we're passing in all of these different attributes. So this should
  2698. 3:46:05actually work. And I'm going to go ahead and prove that to you. So I'm gonna say console
  2699. 3:46:08dot write line. And I'm going to go ahead and just print out like Book Two dot, I don't
  2700. 3:46:14know title. And so you'll see that this attribute got set. So now I can run my program. And
  2701. 3:46:21we get a Lord of the Rings. So it's doing exactly the same thing. Now, I also want to
  2702. 3:46:25show you if you want, you can modify any of these values. So I can technically come down
  2703. 3:46:28here and say book to title is equal to the Hobbit. And now this is going to update book
  2704. 3:46:36two's title. So now when we print it out, it's not going to be Lord of the Rings anymore,
  2705. 3:46:40it's going to be the Hobbit. So that's kind of how this constructor works. And that constructor
  2706. 3:46:45is super powerful. It's an amazing way for us to be able to create these objects really
  2707. 3:46:51quickly. And I also do want to point out that you can have more than one constructor. So
  2708. 3:46:56I have this constructor book. And it takes a title and author and a number of pages.
  2709. 3:47:01But if I wanted, I could just create another one. So I could say like public book. And
  2710. 3:47:06this could take no parameters. And I'd still be able to do this. And so now I can have
  2711. 3:47:12that. And I'll still be able to create a book just by saying book, book three is equal to
  2712. 3:47:18new book. So now I'm able to create the book like this with no parameters with no arguments.
  2713. 3:47:24Or I can create the book with a bunch of arguments. So you can create as many of these constructors
  2714. 3:47:29as you want. And yeah, that's kind of the basics of constructors, these are very powerful
  2715. 3:47:34we want to do is just play around with these, right? I mean, as long as you understand what's
  2716. 3:47:37going on, like this constructor over here is directly related to me saying book over
  2717. 3:47:44here, then you have like, have a good understanding of what these are. And this tutorial, I'm
  2718. 3:47:52going to talk to you guys about object methods in C sharp. An object method is basically
  2719. 3:47:58just a method that we can define inside of our class that the objects of that class can
  2720. 3:48:03use to either find out information or modify information about themselves. And object methods
  2721. 3:48:09can be very useful. So I'm going to show you guys just a little demonstration on how to
  2722. 3:48:13use them in this tutorial, what I've actually done is I've created another class in my program.
  2723. 3:48:19So I created this student class over here. And this student class is pretty simple. I
  2724. 3:48:24just defined three attributes that are going to be part of my student class. So every student
  2725. 3:48:29is going to have a name, a major and a GPA. And then down here, I have a constructor.
  2726. 3:48:33So I'm just sort of passing in the name, the major in the GPA, and I'm assigning them down
  2727. 3:48:37here. So this is a very simple class, a lot like the class we created in the last tutorial
  2728. 3:48:43with constructors. So now that I have this student class, I went over to my program,
  2729. 3:48:48and I actually created a couple students. So I had these variables, student one and
  2730. 3:48:52student two. And then I created these student objects. So one is his name is Jim. He's a
  2731. 3:48:58business major, and he has a 2.8 GPA. And then we have Pam, she's an art major, and
  2732. 3:49:03she has a 3.6 GPA. So these are two objects that I've created from that student class.
  2733. 3:49:10And I want to show you guys how we can put a method inside of that student class that
  2734. 3:49:16these student objects can use to find out some information, what I want to do is I want
  2735. 3:49:21to be able to figure out whether or not each of these students is on the honor roll. Right?
  2736. 3:49:25So let's say that we're, you know, modeling students for a college or university and the
  2737. 3:49:30rules that that college or that if you have a GPA of 3.5, or greater than you have honors,
  2738. 3:49:35so you're on the honor roll, I want to be able to find out whether or not Pam and Jim
  2739. 3:49:40are on the honor roll really easily. So one way I can do that is by creating an object
  2740. 3:49:45method. So what I'm going to do is I'm going to come over here to my class, and down below
  2741. 3:49:49this constructor, I'm going to create a method. So I'm actually going to create a method that
  2742. 3:49:55is going to tell us whether or not the student has honors. So I'm gonna come down here And
  2743. 3:50:00I'm just going to say public. And I'm gonna say bool. Because we're going to return a
  2744. 3:50:05boolean value. Essentially, this method is going to return a true if the student has
  2745. 3:50:11honors, or it's going to return false if the student doesn't have honors. So now I'm just
  2746. 3:50:16going to call this has honors. And I'm going to make an open and close parentheses, and
  2747. 3:50:21an open and closed curly bracket. So inside of here, we can start writing out the code
  2748. 3:50:27to tell if a student has honors. Now remember, what I said before was, if a student has a
  2749. 3:50:32GPA of 3.5 or above, that means they're on the honor roll. So what I can do is I can
  2750. 3:50:37just use a simple if statement. And I can just say if, and I'll come down here, and
  2751. 3:50:41I'll just say, if GPA is greater than or equal to 3.5, then I'm going to come over here,
  2752. 3:50:50and I'm basically going to return true. So I'm just going to return true. And if that's
  2753. 3:50:56not the case, then I'll come down here and we'll just return false. So in the case that
  2754. 3:51:01the GPA is greater than 3.5, we'll hit this line of code, which is telling us to return
  2755. 3:51:06true, and we'll exit the method. And if this is false, so if the GPA isn't greater than
  2756. 3:51:113.5, then we'll come down here and return false. So this is obviously a very simple
  2757. 3:51:16method, but it's going to come in handy with our objects. So I'm going to head back over
  2758. 3:51:20here. And basically, what I can do now is I can figure out whether student one or student
  2759. 3:51:25two is on the honor roll. So I can come down here and I can just print this out. So I'll
  2760. 3:51:30say console writeline. And what I want to do is I want to print out whether or not each
  2761. 3:51:35of these students has honors. So I'm actually going to copy this and I'll come over here
  2762. 3:51:40and I'll say, student, one dot has honors. And then we can also say, student two dot
  2763. 3:51:49has honors. Alright, so I'm printing out if whether or not student one has honors, and
  2764. 3:51:57I'm printing out whether or not student two has honors. Now, what should happen is student
  2765. 3:52:03one dot has honors should return false because Jim has a 2.8 GPA. Jim's not that smart. And
  2766. 3:52:10this line down here, student two dot has honors should return true because Pam has a 3.6 Pam's
  2767. 3:52:16pretty smart. So let's go ahead and run our program. And we'll see what happens. So you'll
  2768. 3:52:21see over here, we got exactly what we thought we would we got false and true. Now one of
  2769. 3:52:26the cool things about the method that we wrote over here in this student class is that this
  2770. 3:52:32method is actually going to use different information depending on what object is calling
  2771. 3:52:38it. So when the gym object calls it, when student one calls it, it's going to use Jim's
  2772. 3:52:44GPA. So when Jim calls this method, it's going to use Jim's GPA of a 2.8. When Pam calls
  2773. 3:52:51this method, when that Pam object calls it, it's going to use Pam's GPA of 3.6. So we
  2774. 3:52:57can specify one method here, we only need to write at one time, and the different objects
  2775. 3:53:03will be able to use it, and they'll be able to use it with their own information. So Jim
  2776. 3:53:08calls this method and uses his GPA. Pam calls this method and uses her GPA. And that's why
  2777. 3:53:14these methods can be so powerful, because we can define one method and then different
  2778. 3:53:19objects that can use it. And they can get different information back depending on the
  2779. 3:53:24value of their attributes. Another cool thing about this method is if I ever wanted to change
  2780. 3:53:30the qualification for honors, so let's say that I wanted to change honors from being
  2781. 3:53:353.5 to being 2.5. So now I'm saying any student who has a 2.5 or above can have honors, well,
  2782. 3:53:43now, I only had to change the code right here. And when I run this program, again, now they're
  2783. 3:53:49both going to have honors. So that value is going to be automatically updated for both
  2784. 3:53:55of these objects, just by changing it in that one spot. And that's what's so powerful about
  2785. 3:53:59these methods is they basically allow me to define different rules for my object. So like,
  2786. 3:54:04I can define what it means to have honors in my program. And I can change it around
  2787. 3:54:09if I want. And it's not really going to matter, because the objects are just calling this
  2788. 3:54:13method, and it's telling them whether or not they have honors. So that's why this is useful.
  2789. 3:54:18And in the next couple videos, we're going to be looking at some other ways that we can
  2790. 3:54:21use some methods just like this. But basically, all you need to know is that you can make
  2791. 3:54:26as many of these as you want, right? So I can make a method over here that tells me
  2792. 3:54:30whether or not, you know, specific rule so that I could it could tell me whether or not
  2793. 3:54:35the students have honors, I could also make a method that would modify one of these values.
  2794. 3:54:39So nothing is stopping me from coming in here and saying GPA is equal to 90.4. Right? I
  2795. 3:54:45could modify each of these students GPA, if I wanted to, you can basically do anything
  2796. 3:54:49you would do in a normal method. So play around with these, you know, I'm sure you can think
  2797. 3:54:53of a couple of different, you know, methods that you can create inside of your classes.
  2798. 3:54:57They can be really awesome. In this tutorial, I'm going to talk to you guys about using
  2799. 3:55:05getters and setters in C sharp. Now a getter and a setter are basically two types of methods
  2800. 3:55:12that we can define inside of our C sharp classes, which will essentially control the access
  2801. 3:55:19that people have to the attributes of those classes. And essentially, what this does is
  2802. 3:55:25it kind of like makes your classes more secure. So it kind of allows you to define like, what
  2803. 3:55:30data is valid for specific attributes and what data is invalid. And really getters and
  2804. 3:55:35setters are extremely useful. And there's a lot of different scenarios where you want
  2805. 3:55:39to use them. And this tutorial, I'm going to show you guys one specific scenario, we're
  2806. 3:55:43going to talk about, like how to set these up, and essentially just how they work. So
  2807. 3:55:47what I've done in preparation for this tutorial is I actually created another class, it's
  2808. 3:55:51this movie class over here. And this movie class is very simple. It's basically just
  2809. 3:55:56modeling a movie inside of our program. So this is sort of like, you know, the template
  2810. 3:56:00for the movie data type. And I said that every movie is going to have a title, a director
  2811. 3:56:05and a rating. And then down here, I created this constructor where I basically pass in
  2812. 3:56:10a title, a director and a rating. And I assigned all of those values. So this is a very simple
  2813. 3:56:17class. And you know, like I said, it's just modeling a basic movie. And then over here
  2814. 3:56:20in my program, I created two movies. So I have one called Avengers, and it's just the
  2815. 3:56:25Avengers with director, john Sweden, and it's pG 13. And then we have Shrek with director
  2816. 3:56:31Adam Adamson. And it's rated PG. So pretty simple program, right? We're basically just
  2817. 3:56:36creating a couple of different movies. But here's one of the problems that we actually
  2818. 3:56:41have with this program. And it might not be super obvious right away. But the problem
  2819. 3:56:45has to do with the movie rating. Right? Now generally, when movies have ratings, they
  2820. 3:56:51can only have specific ratings, right? So if we have a movie, like generally, it's only
  2821. 3:56:56going to be able to be G, PG, PG, 13, R, or nr, right. And maybe there's some more ratings,
  2822. 3:57:03whatever. But let's just say that these are all the valid ratings that we can have for
  2823. 3:57:07a movie. So movie can either be rated G, PG, PG, 13, R, or nr four not rated. And let's
  2824. 3:57:13say that we want to enforce these rules inside of this movie class. So we want all the movies
  2825. 3:57:19in our programs to only be able to have one of these ratings, and it can't have a rating,
  2826. 3:57:26other than the ones that I specified down here, right? Well, the problem is, how do
  2827. 3:57:30we enforce something like that? Right, because what I could do is I could easily come over
  2828. 3:57:34here and change the rating of Avengers to dog for example. And now if I was to come
  2829. 3:57:40down here and print out the Avengers rating, for example, if I came down here is that console
  2830. 3:57:45dot write line, Avengers dot rating, when I print this out, it's actually going to print
  2831. 3:57:52out the rating. So it's going to be able to print out dog. So dog was the rating that
  2832. 3:57:56we gave to it. But dog isn't one of the valid ratings down here. Another thing I could do
  2833. 3:58:02is even if I set this to pG 13, over here, so if this was like set to pG 13, I could
  2834. 3:58:08still come over here and manually set it. So I could still say like, Avengers dot rating,
  2835. 3:58:13is equal to, you know, dog over here, and it's going to be the same scenario, right?
  2836. 3:58:17I'm still able to set this value equal to dog. So what I want to show you guys is how
  2837. 3:58:22we can prevent this from happening. In other words, how can we make it so that the only
  2838. 3:58:27possible ratings for the movies are one of these five ratings that I defined down here,
  2839. 3:58:34we can actually use something called getters and setters. And in order to use these getters
  2840. 3:58:39and setters, we're gonna have to modify a couple things. So the first thing that we
  2841. 3:58:43want to do is we want to close off access to this rating property, this rating attribute
  2842. 3:58:50and what I can do is I can actually come over here into my program. And you'll see that
  2843. 3:58:54all of these attributes are public. So we have public title, public director and public
  2844. 3:59:00rating. And I haven't really talked about this too much yet. But public essentially
  2845. 3:59:05means that these can be accessed by anyone. So any other program, any other code that
  2846. 3:59:10we're dealing with can access any of these attributes. So public just means it's open
  2847. 3:59:15to everybody. It's open to the world. But there's actually another modifier that we
  2848. 3:59:19can put in front of here. And instead of saying public, I could say private. And what this
  2849. 3:59:24means when I say private, this means that only code inside of this movie class is going
  2850. 3:59:31to be able to access the rating. So only code that is contained inside of these curly brackets
  2851. 3:59:37over here in this out of this movie class is going to be able to access this and you'll
  2852. 3:59:41see now when I go over to program, I'm getting an error down here. So essentially, what this
  2853. 3:59:47is saying is we can't access the rating because it's private. So because the code over here
  2854. 3:59:53is inside of the program class, and it's not inside of the movie class. It's not going
  2855. 3:59:58to be able to access the rating. So That's the first step in being able to enforce these
  2856. 4:00:04ratings is we need to make the rating field private. So I need to cut off access from
  2857. 4:00:09anybody else. Now, the next thing I want to do is I want to create two things called getters
  2858. 4:00:16and setters. So we're going to create getters and setters for this rating attribute. And
  2859. 4:00:22basically, what these are going to allow us to do is it's going to allow outside code,
  2860. 4:00:27for example, the code inside of this program class like down here, it's going to allow
  2861. 4:00:31code like this to both access the rating, and modify and set the rating. So those getters
  2862. 4:00:39and setters are going to be really important. And we can define those through what are called
  2863. 4:00:43properties. And a property is basically just like it's kind of like a little method. But
  2864. 4:00:48it's a special method that's going to basically allow us to define a getter and setter. So
  2865. 4:00:54what I want to do is I can just say public, and I can just say string. And what I want
  2866. 4:01:00to do is name this, generally, what you're going to do is you're going to name it the
  2867. 4:01:05same as the attribute over here, but in capital letters. So I'll name this public string rating.
  2868. 4:01:10And then down here, I'm going to make an open and close curly bracket. And inside of here,
  2869. 4:01:14I need to define two things or I don't need to, but I'm going to define two things. So
  2870. 4:01:18I can just say, Get an on make an open and close curly bracket. And then I'm going to
  2871. 4:01:22say set, and I'm going to make an open and close curly bracket. And these are essentially
  2872. 4:01:27going to be getters and setters. So a getter is going to allow us to get the rating attribute.
  2873. 4:01:33And notice over here, I'm trying to get this rating attribute by saying Avengers dot rating,
  2874. 4:01:38but I'm not able to do it right, I can't actually access this rating, because it's private.
  2875. 4:01:43And so the job of the getter is to give me that. So I'm just gonna come over here and
  2876. 4:01:48inside this get, we're just going to say, return rating, and I'm going to put a semicolon
  2877. 4:01:53there. And so basically, whenever we say rating now, and we want to get it, it's going to
  2878. 4:01:59return the rating. Now this setter is going to allow us to set the rating, so it's going
  2879. 4:02:05to allow me to modify the rating. So if I came over here and said, Avengers dot rating,
  2880. 4:02:11and you can already see it's trying to make me use this capital letter is equal to dog,
  2881. 4:02:16it's gonna yell at me, because this is a private attribute. So it's not going to let me do
  2882. 4:02:22that. And that's what the that's the point of this setter, the setter is basically going
  2883. 4:02:26to allow me to do that. So what I can do in this setter, though, is I can define a specific
  2884. 4:02:32rule. So I can basically say like, Hey, you can only set this value to be a certain value.
  2885. 4:02:38And so what I want to do is I want to check to see if the value is one of the valid ratings.
  2886. 4:02:43And if it is, if the value they're trying to set rating to is one of the valid ratings,
  2887. 4:02:48then we're going to go ahead and set it otherwise, we're just going to do something else. So
  2888. 4:02:52I want to say if and I want to check to see if the value that they passed in. In other
  2889. 4:02:57words, the value that they're trying to set to rating is equal to one of the valid rating.
  2890. 4:03:01So what we can actually do is we can say if the value and value is going to represent
  2891. 4:03:06whatever got passed in, so value is going to represent whatever they're trying to set
  2892. 4:03:10the rating to. So I can say if value is equal to and we're just going to do one for all
  2893. 4:03:16of these. So if the value is equal to G, or value is equal to PG, and I'm going to do
  2894. 4:03:23this one for each of the valid ratings, so I can just say or value is equal to, and over
  2895. 4:03:29here, we can just put pG 13. So I'm gonna say pG 13. And we're just going to do two
  2896. 4:03:34more so value is equal to R. And then the last one is going to be for nr. So n over
  2897. 4:03:41here, I'll say or value is equal to an R. So if it's equal to one of these, in other
  2898. 4:03:48words, if the value that we pass in is equal to one of these, then we're going to be able
  2899. 4:03:53to just set that is the rating. So I can just say, rating is equal to value. Otherwise,
  2900. 4:04:02though, I'm just gonna set the rating equal to nr, so I'm just gonna say rating is equal
  2901. 4:04:07to nr. So they add in an invalid rating. In other words, they add in a rating, they try
  2902. 4:04:13to set this to a rating that's not G, PG, PG, 13, R or nr, then it's just going to get
  2903. 4:04:21set to not rated by default. So this getter is going to allow us to get the rating and
  2904. 4:04:27this setter is going to allow us to set the rating. But you'll notice that in order to
  2905. 4:04:32set the rating, we have to go through this if statement, right? So they can try to pass
  2906. 4:04:37in whatever rating they want. But if it's not equal to G, PG, PG, 13, etc, then it's
  2907. 4:04:42just going to get set equal to not rating. And the reason we needed these getters and
  2908. 4:04:47setters was because we made this guy up here private. So there's one more thing we have
  2909. 4:04:53to do in order to make this super secure. In other words, there's one more thing that
  2910. 4:04:56we have to do to make sure that the rating is on Sent to one of these values up here
  2911. 4:05:02in my constructor, when I set the rating right here, I'm just assigning it a normal value.
  2912. 4:05:07So technically, the user could pass in whatever rating they wanted over here. And then I'm
  2913. 4:05:12just gonna set it to the rating. But now instead, I'm just gonna say rating is equal to A rating.
  2914. 4:05:17And what this means is now we're going to set the rating through this setter down here,
  2915. 4:05:23when I use this capital R, it's essentially calling this setter down here and setting
  2916. 4:05:29it that way. So now this is completely secure. And down here, what I can do now is I can
  2917. 4:05:35set this to whatever I want, and it's going to be safe. So down here, I can say Avengers
  2918. 4:05:39dot rating. So I'm using this capital R now. And over here, if I wanted, I could pass in
  2919. 4:05:45an invalid rating. So I could pass in like dog over here. And when I print it out, you'll
  2920. 4:05:50see that we're going to get an R, so we're not going to get dog, we're actually going
  2921. 4:05:54to get n r. And that's because we had to go through that setup. And the same goes for
  2922. 4:06:00anything else. So I could say down here, like Shrek dot rating is equal to cat. And if we
  2923. 4:06:07tried to print out Shrek dot rating, then we're going to get an R because it's going
  2924. 4:06:13to get filtered out by that center. But here's the thing, if I wanted to set this to a valid
  2925. 4:06:18rating, like if I set Shrek dot rating equal to R, now it's going to be able to take that
  2926. 4:06:23rating, because it's valid. So that's kind of how we can use getters and setters. And
  2927. 4:06:28a lot of times what people will do is they'll create getters and setters for each of the
  2928. 4:06:34attributes inside of their classes, you don't have to, but a lot of times they can be useful.
  2929. 4:06:38So we want to do is just play around with that. And, you know, basically, these just
  2930. 4:06:42make your class more secure. So they make it so someone couldn't, for example, give
  2931. 4:06:46your movie in invalid rating. In this tutorial, I want to talk to you guys about static attributes
  2932. 4:06:55in classes in C sharp, a static attribute is basically a special type of attribute in
  2933. 4:07:01a class, which is shared by all of the objects and all the instances of that class. So a
  2934. 4:07:07static attribute is technically an attribute that is contained on the class itself, instead
  2935. 4:07:13of on the individual objects of that class. And I'm going to sort of explain to you how
  2936. 4:07:18this works. Over here in my program, I actually created a class, and it's called song. And
  2937. 4:07:24this class basically just models a song in our program. So it's kind of like a blueprint
  2938. 4:07:28for what a song should be in our program. songs have a title and artist and a duration.
  2939. 4:07:33And then down here, I just have this basic constructor, which allows the caller to create
  2940. 4:07:39the object right up front. So over here in my program, I created two songs. One's called
  2941. 4:07:44holiday one's called cashmere. This is holiday by Green Day, and I just had it was 200 seconds.
  2942. 4:07:50And then Kashmir by Led Zeppelin, 150 seconds. So we have our two song objects. And I want
  2943. 4:07:56to show you guys how we can actually create a static attribute on this song class. So
  2944. 4:08:03one thing I want to point out is there's these normal attributes that we have over here on
  2945. 4:08:07this song class, like the title, the artist and the duration, if I was to come down here
  2946. 4:08:13and print these out, so for example, I can come down here and I can print out holiday
  2947. 4:08:18dot title. And then right after it, I could also print out cashmere dot title. And if
  2948. 4:08:26I was to do this, you'll notice that both of these objects have different titles. So
  2949. 4:08:30the first objects title is holiday. The second is cashmere, I could do the same thing for
  2950. 4:08:36the other attributes. So if I came down here and said, holiday dot artists, and cashmere
  2951. 4:08:41dot artists, they're gonna have different artists, right. In other words, there's two
  2952. 4:08:46different values that are assigned to the attributes of those objects, right. So when
  2953. 4:08:51I create an object in my program, on basically allowing it to have its own title, its own
  2954. 4:08:58artist and its own duration. So each individual object will have their own title, their own
  2955. 4:09:04artists and their own duration, right? Those things are going to differ across all of the
  2956. 4:09:09objects. And that is what we would consider a normal attribute. But there's another type
  2957. 4:09:14of attribute which is called a static attribute. And a static attribute is an attribute that
  2958. 4:09:20isn't unique to each one of the objects. In other words, a static attribute is an attribute
  2959. 4:09:24about the actual class. So this title attribute, like I said, it's different for each of the
  2960. 4:09:31objects. So holiday dot title, and cashmere dot title, those are different values. But
  2961. 4:09:35a static attribute is going to be the same across all of the objects of a class. In fact,
  2962. 4:09:41a static attribute is basically just an attribute that we would say is about the class. So let
  2963. 4:09:46me show you guys. Basically what this is I'm going to come down here and I'm going to create
  2964. 4:09:50a static attribute. You can create it similar to these ones, I can just say public static.
  2965. 4:09:55So I'm using this static keyword here. And then I'm going to give it a type and I'm just
  2966. 4:09:59going to call it Song count. And basically what this variable is going to do is it's
  2967. 4:10:04going to tell us how many song objects have been created. So I'm just going to set this
  2968. 4:10:08equal to zero initially. So I'm giving it an initial value. Now I want to show you guys
  2969. 4:10:13what we can actually do with this static variable. Because we have this static attribute, I can
  2970. 4:10:19actually come over here. And instead of saying like holiday dot artist, I can actually say,
  2971. 4:10:26song dot song count. And I could actually print this out. So I'm going to print this
  2972. 4:10:32out. And you'll see that we should just get zero, because that's what I set it to initially.
  2973. 4:10:38So unlike the artists, the title and the duration, the song count attribute is actually on this
  2974. 4:10:45class. So I couldn't say song, title. Because title isn't an attribute that's associated
  2975. 4:10:53with song, you'll see down here we're getting this error, I couldn't say song dot artist,
  2976. 4:10:58because artist is going to apply to the objects of the class, not the class itself. But I
  2977. 4:11:04can say song dot song count, because this is a static attribute, which is, which means
  2978. 4:11:09it belongs to the class. So that's kind of the difference. And I want to show you guys
  2979. 4:11:14something that we could actually do. So over here, I have my constructor, right? This is
  2980. 4:11:19a constructor method. It's a very simple constructor. Anytime that we create a song object. So anytime
  2981. 4:11:25a song object gets created, this method gets called it's a guarantee, right? Basically,
  2982. 4:11:31when I come over here, and I say, new song, this is me calling that constructor. So anytime
  2983. 4:11:37we create one of these songs, it's going to get called. So what I'm going to do down here
  2984. 4:11:41is every time we create a song, in other words, after all this code in the constructor, I'm
  2985. 4:11:45just going to increment the song count. So I'm going to say, song count plus plus. So
  2986. 4:11:50basically, what's going to happen is, every single time a song object gets created, the
  2987. 4:11:55song count is going to get incremented. So let me show you guys how this is gonna work
  2988. 4:11:59over here, I'm actually just going to, I'm going to get rid of this guy. And I'm going
  2989. 4:12:05to copy this and I'm going to go ahead and paste it in between here. So in between hot
  2990. 4:12:11the creating holiday and creating cashmere, we're printing out the song count. So let's
  2991. 4:12:17see what happens, what we should get is one in two. So basically, when we create holiday,
  2992. 4:12:23and then we print out the song count, one song has been created, so we get a one over
  2993. 4:12:27here, then we create this cashmere object. And so that song count is going to get incremented
  2994. 4:12:33again, and here we get to. So this static attribute is telling us information not about
  2995. 4:12:39the specific objects themselves, but about the class in general, right. So I can figure
  2996. 4:12:45out how many objects have been created of the song class, right? How many instances
  2997. 4:12:50of the song class have been created in my program, but that's not a piece of information
  2998. 4:12:54that is going to be specific to the holiday object or to the cashmere object, that is
  2999. 4:13:00a piece of information that's going to be specific to the class itself. And that's basically
  3000. 4:13:04what a static class attribute is, it's an attribute about the class not about the specific
  3001. 4:13:11objects of the class. So it's basically an attribute that we store to kind of, you know,
  3002. 4:13:15give us information about that class. And you can create static attributes, you can
  3003. 4:13:20create as many as you want. And honestly, they can be really useful. And you'll notice
  3004. 4:13:24that whenever we access them, we don't say like, I wouldn't say like cashmere dot song
  3005. 4:13:30cow, like, I'm not going to be able to do that. So we have to access it through the
  3006. 4:13:34actual class. Now, one thing that we could do is we could create a method. So I could
  3007. 4:13:40come over here and I could create a method. So I could say, like, public get song count.
  3008. 4:13:46And actually, this is going to return an integer. And then over here, I could actually return
  3009. 4:13:51the song count. So I could say like, and now we're actually going to be able to access
  3010. 4:13:58the song count on each of our individual objects. So I could technically come down here and
  3011. 4:14:02say, cashmere dot get song count. And now we'll be able to get the song count off of
  3012. 4:14:11one of those objects. So you'll see now that we can get to just like we did before. So
  3013. 4:14:16you can either just create a static attribute and access it using like song dot song count
  3014. 4:14:22like I did over here, or you could create a getter method like get song count, and then
  3015. 4:14:26the individual objects could access that. And both of those can be pretty useful. Sometimes
  3016. 4:14:32you're gonna want the objects to be able to have access to those static attributes. But
  3017. 4:14:36that's sort of an overview of static attributes and really that static keyword. Actually,
  3018. 4:14:40as we go forward, I'm going to show you some other cool stuff we can do with that static
  3019. 4:14:44keyword. In this tutorial, I'm gonna show you guys how to use static methods in classes.
  3020. 4:14:53A static method is basically just a method that belongs to the class itself. So a lot
  3021. 4:14:59of times when We create methods inside of our C sharp classes. Usually those methods
  3022. 4:15:04will be used on objects. So we can have an object of a specific class and then that object
  3023. 4:15:09can use those methods to, you know, do different things. But a static method in a class is
  3024. 4:15:15a method that belongs to the actual class, which means you don't have to create an object
  3025. 4:15:20of that class in order to use it and access it. And these can be extremely useful. And
  3026. 4:15:25there's a lot of circumstances where you'd want to create classes that have static methods
  3027. 4:15:30inside of them. So I'm going to show you guys basically how that works. Down here, you'll
  3028. 4:15:34see that I have this little line, it just says console dot write line. And then over
  3029. 4:15:38here, I'm saying a math dot square root 144. Right? Essentially, what's happening here
  3030. 4:15:44is there's this class in C sharp, which is called math. And inside of that math class,
  3031. 4:15:51there's a bunch of these methods. Now one thing you'll notice here, when I went to access
  3032. 4:15:55this square root method, inside the math class, I didn't have to create an instance of the
  3033. 4:16:01math class. And normally up to this point in the course, whenever we wanted to use a
  3034. 4:16:05class, or use any of the functionality that a class described, we had to create an object.
  3035. 4:16:10So I would have to say, like math, my math is equal to new math, right, I'd have to create
  3036. 4:16:16an actual instance of the math class like that. But you'll notice that down here, I
  3037. 4:16:22didn't have to do that, right. In fact, when I try to create an instance of the math class,
  3038. 4:16:26I'm getting this error over here. So down here, I can basically just say, math dot square
  3039. 4:16:32root. And I'm able to use this method without having to create an instance of the math class.
  3040. 4:16:38And this is an example of a static method. So a static method, like I said, it's a method
  3041. 4:16:44that belongs to the class, which means we don't have to create an instance of the class
  3042. 4:16:49in order to use it. So I can go ahead and run this program, and we should get back the
  3043. 4:16:52square root of 144. So we should get back 12. I'm going to show you guys how we can
  3044. 4:16:56do this, and how we can basically create something similar to the math class. So up here, I'm
  3045. 4:17:03going to create a new class. And you can either come over here, right click, and go to add,
  3046. 4:17:07and then click New Item. Or we can also go up here, and we can click project and click
  3047. 4:17:13Add class. So I'm going to do it this way. Then over here, you can see I'm just clicking
  3048. 4:17:17on class. And why don't we just call this useful tools. So this is going to be our useful
  3049. 4:17:22tools class. And you'll see over here that I basically just have class useful tools.
  3050. 4:17:28And inside of this class, if I wanted to create static methods, all I have to do is come down
  3051. 4:17:32here and I can just say, like, public static, and I can create a method. So why don't we
  3052. 4:17:37create one called Say hi, and it's going to take one parameter, so it's going to take
  3053. 4:17:42a string name. And then we will basically just have this Say hi. So it's going to console
  3054. 4:17:49dot, write line. And we're just going to print out Hello, name. And actually, whoops, forgot
  3055. 4:17:57to put a return type here. So this should be void. So public static void, say, Hi string
  3056. 4:18:02name, and it just prints out hello to whatever name gets passed in extremely simple method
  3057. 4:18:09over there. So what I could do is I could actually come down here, and I could access
  3058. 4:18:13that method directly without creating an instance of the useful tools class. So down here, I
  3059. 4:18:18can just get rid of this. And we can just go ahead and call this directly. So I could
  3060. 4:18:21say, useful tools dot, say hi. So I didn't actually have to create an instance of the
  3061. 4:18:30useful tools class, all I all I did was just access this method directly. So I can pass
  3062. 4:18:35in my name, like Mike. And now we'll be able to run this method without having to create
  3063. 4:18:40an instance of the useful tools class, you can see over there, everything's running just
  3064. 4:18:45fine. Now, this is an extremely useful thing. So what you could actually do is you could
  3065. 4:18:49create a class over here, and it could just be contained of, you know, these static methods.
  3066. 4:18:56And that's essentially what that math classes that we looked at before, the math class is
  3067. 4:19:00basically just a class that has a bunch of these static methods inside of it, that are
  3068. 4:19:05used to do different things. So if you just have a normal class, you know, maybe a class
  3069. 4:19:10that's modeling some sort of real world entity, you can include static methods inside of there.
  3070. 4:19:15And then the callers don't have to create instances of that class in order to use them.
  3071. 4:19:20Another thing you could do is set up a class that is solely designed for this purpose.
  3072. 4:19:24So for example, that math class, that math class is actually what's called a static class.
  3073. 4:19:30So you'll notice when I tried to create a instance of that math class before I said,
  3074. 4:19:35like math, my math is equal to new math, I'm not actually able to create an instance of
  3075. 4:19:42the math class, you'll see I'm getting an error here. And this is because the math class
  3076. 4:19:47is a static class, which means you can't create an instance of it. But you'll notice with
  3077. 4:19:53these useful tools class I can create an instance of it And it's going to be no problem. So
  3078. 4:20:02like this is actually like a useful tools object. So in certain circumstances, you're
  3079. 4:20:07going to want to do that like, you're going to want to be able to have a class with static
  3080. 4:20:10methods that can have an object created of it. But in situations like with that math
  3081. 4:20:15class, you're not going to want to do that. So what you can do is you can come over here,
  3082. 4:20:19and we could just say, static class useful tools. And now we're not going to be able
  3083. 4:20:25to create an instance of the useful tools class, you'll see down here, I'm getting this
  3084. 4:20:30error, and basically says, cannot declare a variable of static type useful tools. And
  3085. 4:20:33then over here, it says, cannot create an instance of the static class useful tools.
  3086. 4:20:38So that's sort of like two different ways that you can use this. But these static methods
  3087. 4:20:43are extremely useful. And like I said, there's a lot of situations where you're going to
  3088. 4:20:48want to use them, basically, just to define functionality on the class level instead of
  3089. 4:20:52on the individual object level. In this tutorial, I'm going to talk to you guys about inheritance
  3090. 4:21:01in C sharp. Now, inheritance is basically a technique that we can use in C sharp, where
  3091. 4:21:07we can have one class inherit all of the functionality of another class. So I'm going to find something
  3092. 4:21:13called a superclass. And then I can actually create classes that are going to inherit all
  3093. 4:21:20the functionality from that superclass called subclasses. So I'm just going to give you
  3094. 4:21:24guys a very basic introduction into inheritance. This is a huge subject, and there's a lot
  3095. 4:21:29that you can kind of get into with inheritance. So I just want to show you guys like the basic
  3096. 4:21:32concept of what's happening with it. So over here, in my program, I created a class, it's
  3097. 4:21:38called chef.cs. And this is a class that basically like represents a chef in our program. So
  3098. 4:21:45you can see here, class chef, and this chef has three methods that it can use. So the
  3099. 4:21:52first one here is called make chicken. And basically all this does is it prints out,
  3100. 4:21:57the chef makes chicken Right, so the chef can make chicken, the chef can also make salad.
  3101. 4:22:01So it says the chef makes salad and the chef can also make a special dish. So it just says
  3102. 4:22:06the chef makes barbecue ribs. So this is a you know, a pretty cool class. It's obviously
  3103. 4:22:10a very simple class, but it's, you know, it can basically represent a chef in our program.
  3104. 4:22:15So down here in my program.cs, in my main method, I created a chef object, and it's
  3105. 4:22:21just called chef. And then I basically just said chef dot make chicken. So I'm telling
  3106. 4:22:25the chef to make some chicken. And when I run my program, now, it's going to say the
  3107. 4:22:31chef makes chicken, right. So hopefully that makes sense. You know, I just have this chef
  3108. 4:22:35class over here. And I defined some methods in there. And then I created an object of
  3109. 4:22:39the chef class and told it to make chicken. Now I want to show you guys how we can use
  3110. 4:22:44inheritance to make a nother type of class. So let's say that in addition to having just
  3111. 4:22:51a normal chef, like we defined over here, I also wanted to have a more specialized type
  3112. 4:22:56of chef. So I wanted to represent not just not only just a general chef in my program,
  3113. 4:23:01but also a more specialized chef. So let's say I come up here, I'm going to say project,
  3114. 4:23:06add class. And I'm going to create a class called Italian chef. So a Italian chef is
  3115. 4:23:15not just going to be a normal chef, he's going to be a Italian chef. And I'm gonna go ahead
  3116. 4:23:20and create this class. So you'll see over here we have this class Italian chef. Now,
  3117. 4:23:25let's say that this Italian chef can do everything that the normal chef could do. Right. So this
  3118. 4:23:31Italian chef is a really awesome chef. And not only can he just cook regular food, but
  3119. 4:23:36he can also cook like more specifically Italian food. So let's say the Italian chef can do
  3120. 4:23:42everything that the normal chef can do. So the Italian chef can also make chicken, the
  3121. 4:23:47Italian chef can also make salad. And the Italian chef can also make a special dish.
  3122. 4:23:53So the Italian chef is very similar to the normal chef, right, you can do everything
  3123. 4:23:58that the normal chef can do, you can also do some other stuff as well. One thing I could
  3124. 4:24:03do to give the Italian chef all of this functionality is I could just copy. And I can paste all
  3125. 4:24:08of these methods over here, right. And so now the Italian chef is able to make chicken
  3126. 4:24:13able to make salad and able to make a special dish, right. But in C sharp, there's actually
  3127. 4:24:19something that we can do in a situation like this. So anytime you have a class, like Italian
  3128. 4:24:24chef that can do everything. And that has all the same functionality and attributes
  3129. 4:24:29as another class, like this chef class, we can use something called inheritance. And
  3130. 4:24:34inheritance is basically the process of inheriting all of the functionality, all the attributes,
  3131. 4:24:39etc. From a class like chef into a class like Italian chef. So what I can do is I can come
  3132. 4:24:45over here and I can say Italian chef and I can make a colon. And I can just say chef,
  3133. 4:24:51and basically what this means is that the Italian chef is going to inherit all the functionality
  3134. 4:24:56from the chef class. So now the Italian chef can Do everything that the chef class can
  3135. 4:25:01do. And I'm gonna come over here and prove that to you guys. So I'm going to create an
  3136. 4:25:06Italian chef object. So I'm just going to copy this guy. And I'm going to paste this
  3137. 4:25:11down here. And I'm just going to change everything to Italian chef. So I went ahead and change
  3138. 4:25:18this to Italian chef, it's called Italian chef. And we said, new Italian chef. And now
  3139. 4:25:22I'm saying Italian chef dot make chicken, now, on some calling them make chicken method
  3140. 4:25:28on the Italian chef objects. But you'll notice over here in the Italian chef class declaration,
  3141. 4:25:34I don't actually have any methods specified. So I didn't specify like a make chicken method
  3142. 4:25:39or anything like that. But what you'll see is because I inherited all of the functionality
  3143. 4:25:44from the chef class, I'm actually still able to make chicken with the Italian chef. So
  3144. 4:25:50when I run my program, you'll see that it says, The chef makes chicken. And this is
  3145. 4:25:54that normal chef. And then down here, the Italian chef is also able to make chicken,
  3146. 4:26:01because the Italian chef inherited all the functionality from the normal chef. And that's
  3147. 4:26:06kind of cool. So that's basically what you can do. If you have like two classes. And
  3148. 4:26:11in C sharp, we will refer to chef as the superclass. And we would refer to Italian chef as the
  3149. 4:26:16subclass. And the subclass is always going to be inheriting functionality from the superclass.
  3150. 4:26:21So that's just a little terminology. Now we can actually take this a step further. So
  3151. 4:26:27let's say that the Italian chef, in addition to being able to make chicken salad and a
  3152. 4:26:31special dish, can also do something else. So we could add another method in here, I
  3153. 4:26:35could say like public void, and we'll just call this like make pasta. So in addition
  3154. 4:26:40to making all that other stuff, the Italian chef can also make pasta. So now this chef
  3155. 4:26:49can also make pasta and forgot to put an open and closed parentheses there. So if I was
  3156. 4:26:54to come over here to my program, I could say Italian chef dot make pasta, and the Italian
  3157. 4:27:00chef is going to be able to make pasta. So let's run that. And you'll see over here,
  3158. 4:27:05he's making pasta. But what you'll notice is this normal chef can't make pasta. So if
  3159. 4:27:12I tried to make pasta on the normal chef, it's not going to be able to do that. So that's
  3160. 4:27:15gonna throw an error. And that's because we only define make pasta in the subclass, not
  3161. 4:27:21in the superclass. So that's kind of how that's going to work out. And I want to show you
  3162. 4:27:25guys one more thing we can do. So you'll notice over here in my chef class, I have this method,
  3163. 4:27:31it's called make special dish, right. And the chef is making a special dish of barbecue
  3164. 4:27:37ribs, right. So this chef's special dish is going to be barbecue ribs. But let's say that
  3165. 4:27:44the Italian chef doesn't want to make barbecue ribs as a special dish. So let's say that
  3166. 4:27:49the Italian chef's special dish is actually like some other type of dish, and it's not
  3167. 4:27:53a barbecue dish. Well, the problem is when I come over here, and I say, make special
  3168. 4:28:00dish, and I say, make special dish, both of these chefs are going to be making the same
  3169. 4:28:06special dish. So when I run the program, they're both making barbecue ribs. But remember, I
  3170. 4:28:11don't want the Italian chef to make barbecue ribs, I want the Italian chef to make something
  3171. 4:28:16else as their special dish. Well, in a situation like this, I can do something called overriding
  3172. 4:28:22a method. overriding a method basically means that I take one of the methods that I've inherited
  3173. 4:28:27like this make special dish method, and I override it. So I basically come over here
  3174. 4:28:32into this Italian chef class. And I override the functionality of that method. Now in order
  3175. 4:28:38to do this, we're going to have to do one thing over here in the chef class. And we're
  3176. 4:28:42going to have to include this virtual keyword. So you'll notice I already have this in here.
  3177. 4:28:46So up here on these two methods, it says public void make chicken public void make salad.
  3178. 4:28:51But down here on make special dish, I've given it this special keyword, public virtual void
  3179. 4:28:58make special dish. And when we specify this as virtual, basically, that means that this
  3180. 4:29:03method can be overridden in any subclasses. So when I say virtual, it basically means
  3181. 4:29:08that subclasses can change the functionality of this method. So I can just copy this method.
  3182. 4:29:15And I'm just going to go ahead and paste it down here. And you'll see here I'm saying
  3183. 4:29:20public virtual void make special dish, but instead of saying virtual here in the subclass,
  3184. 4:29:25so I'm in my Italian chef class, I'm gonna say, override, and this is basically telling
  3185. 4:29:31C sharp that we're going to override the make special dish method from the superclass, and
  3186. 4:29:37then down here, I can change it so I could say the chef makes instead of barbecue ribs,
  3187. 4:29:41why don't we say like chicken parm. And so now when the Italian chef makes a special
  3188. 4:29:47dish, it's going to be making chicken parm instead of making barbecue ribs. So let's
  3189. 4:29:52go over here into our program. And you'll see I'm still printing out chef dot make special
  3190. 4:29:56dish and Italian chef dot make special dish when I run the pro They're printing out different
  3191. 4:30:02special dishes. So the Italian chef successfully overrode the functionality of one of the methods
  3192. 4:30:09that it inherited from the superclass. And that is extremely useful. So those are three
  3193. 4:30:15very core concepts in inheritance. So the first is just inheriting all the functionality
  3194. 4:30:19from a superclass. The second is extending that functionality. So for example, in addition
  3195. 4:30:25to being able to do everything that the normal chef could do, the Italian chef could also
  3196. 4:30:28make pasta. And then finally, we were able to override one of the functions or one of
  3197. 4:30:34the methods that we inherited using this override keyword on the subclass, and using this virtual
  3198. 4:30:41keyword on the superclass. So that is sort of the basics of inheritance. And like I said,
  3199. 4:30:46there's, you know, inheritance is a very complex subject, and there's a lot lot lot of stuff
  3200. 4:30:52that you can do with it. But this is kind of just an overview of essentially how it
  3201. 4:30:56works. So now you kind of have the concepts down in your mind and you can kind of go forward
  3202. 4:31:00and learn more specific ways to use it. Hey, thanks for watching. If you enjoyed the video,
  3203. 4:31:06please leave a like and subscribe.

About this transcript

This page contains the full transcript of C# Tutorial - Full Course for Beginners by freeCodeCamp.org, generated from the public captions YouTube serves with the video. The transcript has 55,812 words across 3,203 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.