C# Tutorial - Full Course for Beginners — Transcript
Full transcript
- 0:00Hey, welcome to draft Academy. My name is Mike in this course, I'm going to teach you
- 0:03guys everything you need to know to get started using C sharp, a C sharp is an extremely powerful
- 0:09programming language. And it's used in millions of applications around the world, there are
- 0:13literally 1000s of things you can do with C sharp, and there's tons of different applications
- 0:18for this language is extremely powerful. And I'm excited to be bringing you guys a course,
- 0:23that will teach you everything you need to know to get started. So we're going to start
- 0:26the course with the basics, we're going to install C sharp, we're going to install a
- 0:29text editor called Visual Studio, we're going to get everything set up. And we'll start
- 0:34writing our first program. I'm going to teach guys the basics of programming. So we're going
- 0:38to look at some of the core programming concepts, things like working with data variables, data
- 0:43types, getting input from users, then we're going to move on to some more advanced stuff.
- 0:47We'll talk about things like loops, and if statements in different data structures. And
- 0:52finally, we'll talk about some object oriented programming concepts, things like classes,
- 0:57and objects, inheritance, all that fun stuff. If you're new to programming, or you just
- 1:01want to try and pick up C sharp, then this course has something for you, I'm really excited
- 1:06to be bringing you guys C sharp tutorials. It's an awesome language, and there are literally
- 1:101000s of things you can do with it. So without further ado, let's get started. Let's go into
- 1:15the course and start learning some C sharp. In this tutorial, I'm going to show you guys
- 1:23how to install and get everything set up to start programming in C sharp. Now in order
- 1:28to start programming in C sharp, we're actually just going to need one thing, which is going
- 1:31to be a program called Visual Studio community. And this is basically a program that will
- 1:37allow us to write our C sharp programs. And this is what's called an integrated development
- 1:42environment. And then a lot of times you'll hear people call those IDs, it's essentially
- 1:47just a special program where we can write and work with our code. So in this case, this
- 1:53is an ID that will allow us to work with the C sharp programming language. So over here
- 1:58on this website, it's just visual studio.com, forward slash vs forward slash community.
- 2:05And this is the website where we can go to download this, you'll see over here, there's
- 2:07an option to download on Windows. And then there's also an option to download on Mac
- 2:13over here. So you can just click whichever one you need. On my case, I'm on a Windows,
- 2:17so I'm just going to go ahead and click the one for Windows. So click this and that should
- 2:21start downloading and we should get an installer that we can use to install Visual Studio.
- 2:26So once that's downloaded, I'm just gonna click it. And we'll go ahead and run this
- 2:29installer. So when the installer loads up, you'll see that we have this little window
- 2:33over here. And basically this is a little program that we can use to install what we
- 2:37need. Now here's the thing, this Visual Studio program is huge. And there's tons of stuff
- 2:43that you can actually do with it. So you can see there's all these different things we
- 2:46can install. I mean, there's probably like, over 100 gigabytes worth of stuff that we
- 2:49can install here, we basically just need this guy over here says dotnet desktop development,
- 2:55this is all we're going to need, we're going to be building something called a console
- 2:58application, which is essentially like the simplest type of application that we can build.
- 3:02And you'll see over here, it says the install size is around three gigabytes. So that's
- 3:07actually not too bad. Some of these things are like a couple, you know, maybe like dozen
- 3:11gigabytes, something like that. So yeah, click this dotnet desktop environment. And I'm just
- 3:15going to click install, and we're going to go ahead and install that hopefully doesn't
- 3:18take too long. Alright, so once that is finished installing and downloading, we can just go
- 3:22ahead and click Launch. And we can launch right into Visual Studio and see over here,
- 3:27it's asking me to sign in, you don't actually have to sign in. So I'm just gonna click Not
- 3:31now, maybe later. And then over here, we have a bunch of options. You'll see down here,
- 3:35there's this drop down, I'm just going to click general. So just make sure it's on general.
- 3:38And then we can pick whatever style we want to have, we can just go with the basics. So
- 3:42I'll just stick with blue. And then I'm going to click Start Visual Studio. Alright, so
- 3:47this should go ahead and open up this little starting page, I'm just going to click out
- 3:50of this, we don't need to have this up here. And I'm going to go over here. And we're just
- 3:54going to click File, I'm going to click new project. So we want to start a new project.
- 3:59And this is basically going to allow us to write our applications. So over here in sort
- 4:05of Visual C sharp, you can just click here. And you'll see over here, there's an option
- 4:09for console app, this is what we want. So don't get too intimidated by all this different
- 4:14stuff. Like I said before, Visual Studio is a very powerful program. And it can do a lot
- 4:18of stuff. But make sure you just click console app, and we should be good. So I'm just going
- 4:22to click OK. And actually down here, we can give this a name. So I'm just going to call
- 4:26this draft and then we could store it whatever we wanted to store it. So when this loads
- 4:31up, you'll see that we now have this file open. And this is just called program.cs and
- 4:36that.cs extension stands for C sharp. And then down here we have a basic program. So
- 4:44this is essentially a very simple program. And it's sort of like the template for a program.
- 4:48And don't worry too much about what all this stuff is. In the next video. I'm going to
- 4:51talk you guys through what all this stuff is what it means and we'll actually write
- 4:55our first program but for now, we have everything set up. So we're ready to go and right To
- 5:00get started with C sharp development. And this tutorial, I'm going to show you guys
- 5:07how to write your first C sharp program. We're also just going to talk in general about what
- 5:12a program is, we're going to look at the different instructions that we can give to the computer
- 5:17and how those instructions are going to play out and allow us to write applications. So
- 5:23over here, we basically just have our basic Project Setup. And I kind of went over how
- 5:28to do this in the last tutorial. And we're basically given this little text file, and
- 5:33this is a C sharp file. So it's called program.cs. And that CS extension stands for C sharp.
- 5:40You'll also notice over here in our solution explorer, we have a bunch of different stuff
- 5:45here. So I called my project draft. And then over here, I have like this little properties
- 5:51thing, this references thing, really all we need to worry about over here. And I know
- 5:56there's a lot of stuff over here is this guy right here, program.cs, that is the C sharp
- 6:02file that we're going to be programming and that's the file we're going to be using for
- 6:05this course. And this is really all you need to worry about over here. And you'll see that
- 6:10this is actually populated with some code with some text by default, you'll see up here
- 6:16we have all of these little commands, it just says using system using system collections,
- 6:21all this stuff, essentially what this is doing is it's just configuring the file. So you
- 6:25don't really have to worry too much about what this stuff is up here. Just make sure
- 6:29that you leave it alone, you know, don't touch it. But basically, there's some things that
- 6:33we're going to have to use with C sharp and up here, we can specify that we want to use
- 6:37them. So there's these things called system. And there's all these different things that
- 6:41we can use inside of there. But for the most part, you can just kind of leave this alone
- 6:45for now, we'll talk more about what this is later in the course. But for now, you don't
- 6:49have to worry about it too much. Down here, you'll see this as namespace draft, you'll
- 6:54notice that this is actually the same name as the project that we're using. And essentially,
- 6:59that's what this is saying. It's saying that we're in the draft project. And actually,
- 7:02this is doing some more stuff. Again, we're going to talk more about namespaces later
- 7:06in the course. So you don't really have to worry about that too much. And really, you
- 7:10know, all this stuff up here, you can can just leave alone, you don't have to worry
- 7:13about that in the beginning. And down here we have these two curly brackets, and these
- 7:18two curly brackets essentially just signify that our like program is starting. So inside
- 7:24of these curly brackets, we can actually write some code and the computer will be able to
- 7:28execute that code. Here we have this keyword class, and it's called program. Now a class
- 7:34is essentially just a container where we can store a bunch of code. So inside of a class,
- 7:39we can write all of the code for our program, and it'll be able to be executed. So again,
- 7:44we're going to talk more about classes as we go on through this course. But for now,
- 7:48just know that a class is essentially just a container where we can put code. Down here
- 7:53we have this line of code here, it says static, void main. And this is actually what's called
- 8:00a method or a function. Again, we're going to talk more about methods and functions later
- 8:05in the course. But this is sort of just like another container where we can put code. And
- 8:11this is a very important container. Because this container is where we can put all the
- 8:17code that we want our program to execute. So this main method, or this main function,
- 8:22any code that we put inside of here, in other words, any code that we put inside of these
- 8:27open and closed curly brackets, is actually going to get executed by our computer. So
- 8:32whatever code I want my program to have, I need to put it inside of this main method.
- 8:37So inside of these open and closed curly brackets. Now, I understand that like a lot of this
- 8:43stuff doesn't make sense right now. And to be perfectly honest with you, we kind of have
- 8:47to just set our program up this way in order for it to work. It doesn't matter if this
- 8:51stuff doesn't make sense. Like obviously, you're not going to understand what all this
- 8:54stuff is. But just know that that's totally fine. Like as a beginner, you're not expected
- 8:58to know any of that you're not going to know any of that through this course though, I'm
- 9:02going to be teaching you guys all of this stuff. So by the end of the course, we'll
- 9:05understand what all this stuff is doing, why it's important, why it's there. But for now,
- 9:09I just need you to trust me that we need to have everything set up like this, in order
- 9:13for our programs to run an any code that we're going to write inside of our C sharp file
- 9:18that we want to execute, I want to put in between these two curly brackets inside of
- 9:22this main block. Okay. So now that we've kind of talked about the structure a little bit,
- 9:28we've talked about, you know, some of the stuff that we needed to know, what I want
- 9:31to do is actually write our first line of code. So we're going to go ahead and write
- 9:36a line of code, which is going to print something out onto the screen. So one of the things
- 9:41that is really useful to do when writing a program is to print out information. And the
- 9:45most basic instruction that we can give the computer is basically telling it to print
- 9:49something out. So I'm going to go ahead and do that down here. We're going to write our
- 9:53first line of code. So I'm just going to say console with a capital C dot right? line.
- 10:01And then we're going to make an open and closed parentheses. And I'm going to make a semi
- 10:05colon. Now inside of these opening closed parentheses, I'm going to make an open and
- 10:10closed quotation mark. And I'm just going to type out hello world. So essentially, what
- 10:15we're doing here is we're telling C sharp that we want to write this text out onto the
- 10:21screen. So let's kind of look through this line of code. I'm saying console dot write
- 10:25line. And this is an instruction. So essentially, I'm telling C sharp that I want to write a
- 10:30line out onto the console. And as we'll see in a second, the console is basically just
- 10:36a little window where we can see the output of our program. I'm saying console dot write
- 10:41line. And inside of these parentheses, I'm specifying what I want to write out onto the
- 10:45console, or out onto the screen. And I'm basically just saying, hello world. And then over here,
- 10:51I'm putting this semi colon. Now whenever we're writing a line of code in C sharp, whenever
- 10:57you're done writing that line of code, you always need to end it with a semicolon. So
- 11:01for the most part, whenever you write an instruction, whenever you write a line of code, you're
- 11:05going to end it with a semicolon. And that's very important. And as a beginner, you're
- 11:10going to forget to put the semicolon trust me, everybody does, but eventually, we'll
- 11:12just kind of get used to putting it in there. Alright, so I'm going to go ahead and save
- 11:16this file. And now we're going to actually execute this program. So when I say execute
- 11:20the program, essentially what I'm doing is I'm telling the computer to go through and
- 11:25carry out all of the instructions that we specify. In our program, right now, we only
- 11:29really have one instruction, which is telling the computer to write a line out onto the
- 11:34console. I'm gonna come up here, and you'll see there's this start button. So this is
- 11:38going to be our best friend throughout the course, anytime I want to run my program,
- 11:42I'm going to go ahead and click this start button. So I'm just gonna click it. And when
- 11:45I click this start button, my program is actually going to run. And if you look there, you'll
- 11:50see that a blank window popped up for just a second, I'm continuing to run it, essentially
- 11:55what's happening is C sharp is printing out the line hello world onto a little console
- 12:01window that's popping up. But the problem is that our program is printing that out,
- 12:07and then the program is terminating. So that window is going away. In order to keep that
- 12:11window up. So we can see what's going on, I'm going to type out another line of code,
- 12:15I'm just going to type console dot read line. And basically what this line of code is going
- 12:21to do is it's going to keep that little console window up until I enter in a line of text.
- 12:27So now when I run my program, you guys will see that this little black window up here
- 12:33appears and it stays open. So before it was kind of flashing up and then disappearing.
- 12:39But now it's going to stay open. And you'll see we're printing out hello world onto the
- 12:43screen. Now, if I click the Enter key, so I'm just gonna click enter, this window is
- 12:48going to go away. So anytime I click enter, that basically tells our program that we're
- 12:53done executing it. So we want it to go away. And that's basically all we have to do. So
- 12:57I can write out instructions in here, I can write out lines of code, I can press the start
- 13:01button. And then I'm basically executing all of those lines of code. And then obviously,
- 13:06to get out of this, like I said, you can just like type a space and type enter, or you can
- 13:10really type anything, and just hit enter and then the program will terminate. This is sort
- 13:15of our first C sharp instruction. And like I said, console dot write line basically just
- 13:21writes a line of text out onto the screen. And then we can include this console dot read
- 13:25line instruction in order to keep that window up so we can see what we're printing out.
- 13:31Now that we know how to print out something onto the screen, I want to show you guys how
- 13:34we can draw a little shape. So I'm actually going to get rid of this. And I'm going to
- 13:40go ahead and copy this right line piece of code. And I'm going to paste it a few times
- 13:44down here. So we're actually going to use this line of code to draw out a shape onto
- 13:49the screen. So down here, I'm going to create like a forward slash. And then up here, we'll
- 13:53create one. And I'm going to keep creating them in this little pyramid structure. So
- 13:59now you can see I have like a little pyramid and then I'm going to make a vertical bars
- 14:02going all the way down. So essentially, we're drawing out like a little right triangle.
- 14:06And actually, you know, I want to put underscores here, so it's a little more obvious. So you
- 14:12can see I'm saying console dot write line. And in each one of these instructions, I'm
- 14:17telling the computer to write out a different line. So over here, we're writing out like
- 14:21the tip of the triangle. Over here, we're writing out the middle part of the triangle,
- 14:25and then the base of the triangle. So now let's go ahead and run this program. And you'll
- 14:30see over here, we're actually able to print out this entire triangle. So just by using
- 14:36that one instruction console dot write line instruction, I was actually able to draw out
- 14:41an entire shape onto the screen. So now I'm going to go ahead and exit off of this. And
- 14:46you'll see down here how easy it was for us to write this program. Now, essentially what
- 14:51happens is when I click that start button, C sharp is going to go into my program. It's
- 14:56going to find this block of code right here. Main It's gonna go inside of these curly brackets.
- 15:03And it's going to execute each instruction. In other words, each line of code, one after
- 15:08the other. So it's going to start with this first line of code. And it's basically going
- 15:12to say, Okay, what does the user want me to do, they want me to write a line out onto
- 15:16the console. And they want me to write this text. So it's going to go ahead and write
- 15:19that out onto the console, then when it's done with this instruction, it's going to
- 15:23come down here and go to this second instruction, it's going to do the same thing. It'll execute
- 15:28that instruction, and then it'll go down here, execute this one and execute this one. So
- 15:33essentially, what C sharp does is it executes all of the lines of code, all the instructions
- 15:38that we give it in order. And one of the things you're going to learn is that when we're programming,
- 15:43the order matters. So the order in which you write these lines of code actually matters
- 15:47a lot. So if I was to take this instruction over here, and I was to paste it up here on
- 15:54the top, now, this is going to change the way that the shape looks. So instead of having
- 15:58that normal triangle, you can see we have this kind of like funky looking shape, or
- 16:02the base is up at the top. And that's because we changed the order of the program. Essentially,
- 16:07all a program is is just a set of instructions specified in a particular order. That's it.
- 16:13And as we go through this course, and as you learn more about C sharp, you're going to
- 16:16find more and more complex instructions that you can use. So the more complex instructions,
- 16:23you're able to give the computer and the better, you're able to organize them and order them,
- 16:27the more powerful your programs are going to become. So obviously, we only really know
- 16:31two instructions, we know this console dot write line, which prints something out in
- 16:36console dot read line, which basically pauses the screen until we enter something. But just
- 16:41with these two lines of code, we were actually able to draw out an entire shape onto the
- 16:45screen. And that was pretty cool. The takeaways from this tutorial is that a program is essentially
- 16:50just a set of instructions that the computer is going to follow the order of those instructions
- 16:55matter. And in C sharp, the instructions that are going to get executed by the computer
- 16:59are going to be inside of these open and closed curly brackets in this main block of code.
- 17:06And that's sort of the bare basics of writing a C sharp program, we were able to print something
- 17:10out into the screen, we were able to pause that little console until we told it we were
- 17:14done. And now we can kind of go forward and start learning more and more complex instructions
- 17:19and more and more complex ways to build our programs. In this tutorial, I'm going to talk
- 17:28to you guys about variables in C sharp. Now a lot of times when we're writing programs
- 17:32in C sharp, we're going to be dealing with a bunch of different data. So there's going
- 17:37to be all sorts of information, all sorts of data values in our programs. And a lot
- 17:41of times that information, those data values can be kind of hard to keep track of and manage.
- 17:47So in C sharp, we have a special container called a variable. And a variable is basically
- 17:52just a container where we can store some of that information. So I can store a piece of
- 17:57data or a piece of information that I want to keep track of and manage in my programs
- 18:03inside of a variable. So in this tutorial, I'm gonna show you guys what variables are,
- 18:06why they're useful. And we'll look at kind of how we can work with them in our C sharp
- 18:10programs. So down here, I have written out this very basic program. It's basically just
- 18:16writing out a bunch of lines of text, it says There once was a man named George, he was
- 18:2170 years old. He really liked the name George, but didn't like being 70. So this is basically
- 18:26just a program that I wrote, which prints out a little story onto the screen. So if
- 18:31I was to run this program, you'll see that we get this story it says There once was a
- 18:35man named George who was 70, etc. So I'm able to print out this story onto the screen. So
- 18:41this is a perfectly functional program, we've actually written a program which prints out
- 18:45a story, right, but let's say that I'm looking at my story. And I'm thinking to myself, Hmm,
- 18:49I think I want to change the character's name. Well, let's say instead of having the character
- 18:55be named George, I want to change his name to john. Well, I can just come up here, I
- 18:59can search through my story, and find every place where I'm mentioning the character's
- 19:03name, and I can change it to john. Cool. So now my story is updated. I went through I
- 19:10manually changed the name to john, and everything works. But let's say I'm looking at it again.
- 19:16And I'm thinking maybe I want to try to appeal to a younger audience. So instead of having
- 19:21the character be 70, why don't we have the character be like 35. So once again, I can
- 19:26just go through I can look through and every time they mentioned the character's age, I
- 19:30can just change it to 35. Here's the next one. Cool. So I basically went through and
- 19:37I found out every place where the character's name was mentioned, and I changed it to 35.
- 19:41Here's the problem though, in order to make those changes in order to modify the character's
- 19:47name and of the characters age, I had to go through and manually change it at every place
- 19:53inside of my program. So every place where the character's name was mentioned, and the
- 19:57characters age was mentioned. I had to go in and manually change it. Now, this is fine
- 20:02with a story that only has four lines. But imagine if this story had hundreds of lines.
- 20:07And the character's name was mentioned, like 100 times or their age was mentioned 100 times,
- 20:12imagine then if you wanted to change the character's name, or imagine even if you just wanted to
- 20:17keep track of and manage the characters name, and it would be extremely difficult. So the
- 20:22more lines in the story that I write, the harder it becomes for me to manage the characters
- 20:28name and the character's age. And these are two good examples of pieces of information
- 20:33in our program. In order to make a situation like this a lot easier, we actually have something
- 20:38in C sharp, called a variable. And a variable is basically just a container where I can
- 20:44store a specific data value. And then when I want to use that data value, instead of
- 20:49having to actually like, type it out and type out what it is, I can just refer to the variables
- 20:55name. So I'm gonna show you guys how we can create variables, and how we can keep track
- 20:59of and manage the different data in our programs like the characters name and the character's
- 21:03age. So up here above my story, I'm actually going to start creating a variable. And the
- 21:10way that we can create a variable is by giving C sharp some information. So this is going
- 21:15to be a line of code. And the first piece of information that I want to give to C sharp
- 21:20is going to be the type of data that I want the variable to be able to store. So remember,
- 21:25variables are just containers that we can store values inside of. And different containers
- 21:30can store different types of information. Some containers can store text, others can
- 21:35store numbers, others can store like other values, like true or false values, we're gonna
- 21:40hold go ahead and create a variable that's going to store the character's name. So I
- 21:45want to create a variable that can store text, because the characters name is text. And we
- 21:50can do that by creating a string. So a string is essentially just like a string of characters.
- 21:55And it's basically just plain text. So I'm going to go ahead and tell C sharp that I
- 21:59want to create a string variable. So I'm just going to type out string. And then what I
- 22:04need to do is give this variable a name. So remember, these are containers. And generally
- 22:09when we're naming a variable, we want to name it according to what's going to be stored
- 22:14inside of it. That way, we can identify what's in the variable, I'm just going to call this
- 22:19character name. And you'll notice here that I'm separating these words. So the second
- 22:24word has a capital letter. Alright, so I created my variable, and it's going to store a string,
- 22:30which is just plain text. And then I'm going to set it equal to something. So I'm going
- 22:34to say character name is equal to, and now I'm going to give this a value. So I'm going
- 22:39to give this a value, that's plain text, I'm going to make an open and closed quotation
- 22:43mark. And then whenever I write a line of code, I always want to end it off with a semicolon,
- 22:48just like that. Alright, now inside of these quotation marks, I can write out the string
- 22:52of characters in the string of text that I want to store inside of the character name
- 22:57variable. So we're just going to store the character's name, which is john. So I can
- 23:00just type out john, up there. In addition to creating a variable to store the characters
- 23:05name, I also want to create another variable to store the character's age. So let's go
- 23:10ahead and do that. Now the character's age is actually going to be a number, right, so
- 23:16the character's age is a number. It's not really plain text like this. And if we want
- 23:20to create a variable to store a number, we can create a variable called an integer variable.
- 23:25So I N T stands for integer. And an integer is basically just a whole number. So a number
- 23:30that doesn't have decimal points. And I'm just going to go ahead and name this character
- 23:34age. So again, giving it a name. And I want to show you guys another way that we can create
- 23:38these variables. So in addition to saying like string character name is equal to john,
- 23:43I can also just say, and character age, I can put a semicolon here. And then I can come
- 23:48down here and give this a value. So I could say, like character age is equal to, and now
- 23:54we can give this a value. So we said 35. And you'll notice that when I give this a number
- 23:59value, I don't need these quotation marks, you only need the quotation marks with a string.
- 24:03So these are two different ways that I can create a variable and they're doing exactly
- 24:07the same thing up here, I'm just saying string character name, and I'm giving it a value
- 24:11right away. Over here, I'm doing what's called declaring the value the variable on basically
- 24:16saying that we're going to have a variable called character age, I'm not giving it a
- 24:20value right away. And then down here, I'm assigning it a value of 35. So these two,
- 24:27this guy and this guy up here are doing exactly the same thing. So now that we have these
- 24:33variables, in other words, now that we have containers, where we can store the different
- 24:37values, or the different pieces of data that we're going to work with in our programs,
- 24:41I want to actually use those variables inside of our story. So I'm gonna show you guys how,
- 24:46in addition to printing out this plain text, I can actually print out the value that's
- 24:51stored inside of the variable. So over here, you'll notice that I'm typing out there once
- 24:55was a man named john, but I don't want to have to type out john anymore. I just Want
- 25:00to be able to refer to that variable. So what I can do is I can actually get rid of this.
- 25:05And I'm going to come outside of these quotation marks. And over here, I'm going to put a plus
- 25:10sign. And this plus sign basically means I want to like append some text, or I want to
- 25:15append a value onto the end of this text over here. And what I want to do is just put the
- 25:21name of the variable, so I'm just going to type out character name. And what this is
- 25:25going to do is it's going to print out there once was a man named, and then a space, and
- 25:30then it's going to print out the value that's stored inside of the character name variable.
- 25:35So it's going to print out, john. So let me demonstrate that, let's go ahead and run this
- 25:39program. And you'll see over here, we're still printing out there once was a man named john,
- 25:45except I didn't have to physically type out john down here, all I did was use this plus
- 25:51sign, and essentially just append on or people will call this concatenate on the value that
- 25:58was stored inside of the character named variable. And this is extremely useful. So I could do
- 26:01the same thing down here with this other occurrence of the character's name, I can just say, character
- 26:07name. And now over here, it's going to do the same thing, it's going to say he really
- 26:11liked the name. And then whatever the value that's stored inside of this character name
- 26:16variable, we can do the same thing for the age. So over here, you'll notice that I have
- 26:22the age is 35. But remember, we created this variable up here to store that. So what I
- 26:28can do is I can actually get rid of this. And I'm actually going to make to open and
- 26:32close quotation marks here. And I'm going to make two plus signs. And then in between
- 26:36here, I'm going to type out character age, essentially, what I'm doing is I'm telling
- 26:41this right mine instruction that I want to print out, he was with a space. And then I
- 26:47want to add in the value that's stored inside of the character age variable, which is going
- 26:51to be 35. And then I want to add into that years old, so basically, what we're doing
- 26:57here is called concatenating, a string, I'm saying I want to type this out, plus, I want
- 27:02to type the value that's in here, plus, I want to print out this stuff over there. And
- 27:07then finally, we can do the same thing down here. So I'm going to get rid of this. And
- 27:12I'm just gonna type in character age. So I've replaced every occurrence of the character's
- 27:19name and the character's age with these variables. So now let's go ahead and run our program.
- 27:25I'm just going to click this start button. And you'll see over here, we're printing out
- 27:28the same exact story as before, it says There once was a man named john, he was 35. He liked
- 27:33the name john and 35. So I essentially just replaced john and 35 with the variables that
- 27:40are storing them. Now, what's cool about these variables is if I wanted to, for example,
- 27:46modify the character's name or the character's age, I can just modify it up here. So let's
- 27:50say we wanted to change the character's name to Tom. And we wanted to make him like 25.
- 27:55Well, now I only have to modify those two values, I only have to update them in one
- 28:00spot. And it's going to update throughout my entire story. So now the character's name
- 28:05is Tom. And he's 25 throughout the entire story, and the variables just make it a lot
- 28:12easier for us to keep track of and for us to manage those pieces of information. Now,
- 28:18you'll notice that I only created variables for the characters name and for the characters
- 28:22age. And that's because those two pieces of information are occurring multiple times in
- 28:27the story, I didn't create a variable for the word once or for the word man, because
- 28:33I just didn't need to, you know what I mean, that's not like a distinct piece of information
- 28:37that we want to keep track of. But the characters name and the character's age are definitely
- 28:41pieces of information that we're going to want to keep track of and store. So that's
- 28:45sort of how we can use variables to make it easier to manage the data in our programs.
- 28:49Now I want to show you guys another cool thing we can do with this. Let's say that halfway
- 28:53through the story, I wanted the characters named the change, I can actually come down
- 28:57here and halfway through printing out the story, I can modify the value of one of these
- 29:02variables. So I could say, character name, and I could just give this another value.
- 29:07So I can say character name is equal to Mike. And again, I need this semi colon at the end
- 29:11here. Don't forget that. Now halfway through the story, the characters name is actually
- 29:16going to change. So if we run the program, you'll see over here it says Tom, and then
- 29:22down here, the name is Mike. So I was able to update the value that was stored inside
- 29:28of our variable halfway through. And that's another cool thing is these variables, you
- 29:32can give them one value and then later on in the program, you can change that value.
- 29:36So that's really the basics of working with variables, we really learn two things, we
- 29:39learn how to create variables, and we learn how to use them inside of like a print statement
- 29:44over here. Now there's a lot more to learn with this. And actually in the next tutorial,
- 29:48I'm going to be teaching you guys about the different types of information that we can
- 29:52store inside a variable. So over here, we stored a string of text and we also stored
- 29:56a whole number which is called an integer but there's a few Other types of data that
- 30:01we can use and work with. So in the next tutorial, we're going to learn about all of that stuff.
- 30:09In this tutorial, I'm going to talk to you guys about data types in C sharp. Now in C
- 30:14sharp, we're gonna be dealing with all different types of data and information. And generally,
- 30:18when you're writing programs, there's going to be all different types of information that
- 30:21you're going to be working with. So I want to talk to you guys today about the different
- 30:25types of information and the different types of data that we can represent and work with
- 30:30in C sharp. So down here, I'm basically going to be showing you guys all the different data
- 30:37types that we can use. And I'll be doing that by creating variables. So a variable is just
- 30:41a container where we can store a data value or a piece of information. And I'm going to
- 30:46be creating different variables that can store the different types of data that we can work
- 30:51with in C sharp. So the first type of data that we can work with is plaintext. So anytime
- 30:56you want to represent or, you know, use just regular old text inside of your programs,
- 31:01you want to use something called a string. So I'm just gonna say string. And I'll just
- 31:04call it phrase. And I'm just gonna set it equal to a string. So a string is basically
- 31:08just denoted using an open and closed quotation mark. And then inside of the quotation marks,
- 31:14you can just put whatever string you want to represent. So in here, I could put like,
- 31:18draft Academy, and now this is going to be a string with the text draft Academy inside
- 31:24of it. So that's a string, and that's going to come in handy a lot. Another type of data
- 31:28that we can store is instead of a string of text, just going to be one single character.
- 31:34So I want to create a character, I can just say char, and maybe this could be like the
- 31:39grade you get on a test or something. And then when I create a character, I want to
- 31:43use these single quotation marks, just like that. And then inside here, I can put one
- 31:48single character, so I could put like an A, or a B, or a C, or something like that. But
- 31:54the important difference is that with a character, I can only put one single character, I can't
- 31:58put a bunch of stuff, I can only put one, so any single character is going to work.
- 32:03But you need to make sure that there's only one in there, when you need more than one
- 32:07character, then you want to move up to the string. Alright, so that's the two ways we
- 32:12can store plain text. So things like characters and you know strings of characters, we can
- 32:17also work with numbers, and the most basic type of number we can create is called an
- 32:21integer. And an integer is basically just a whole number. So it's one of the counting
- 32:26numbers like 12345, basically, an integer doesn't have any decimals in it. So I can
- 32:31just say, I n t like that int. And this could be like someone's age, for example. And so
- 32:37maybe someone's like 30. And you'll notice that when I create a number, like any numbers,
- 32:42or you know whether it's an integer or another type of number, you don't need quotation marks
- 32:46around it like you do up here. So you can just type out the number as is. And you can
- 32:50do whole numbers like this, like an integer, we could also do negative numbers, and you
- 32:55can just put the minus sign right in front of it, and C sharp is going to be able to
- 32:58handle negative numbers just fine. In addition to integers, we can also represent decimal
- 33:03numbers. So a decimal number would be you know, like 60.3 or something like that. And
- 33:08instead of storing inside of an integer, we can actually store it in one of three data
- 33:13types. So there's actually three different data types that we can use to represent decimal
- 33:17numbers. And they're called, there's a float, a double, and a decimal. And these basically
- 33:23allow you to store decimal points more and more specifically, so These range from least
- 33:28accurate with the float to most accurate with the decimal data type. So if you really want
- 33:33to be super, super, super precise with the decimal point that you're using, you want
- 33:37to use a decimal point and this would be used for things like money, or you know, really
- 33:41anything that you need to be extremely exact with you want to use a decimal, a float is
- 33:46going to be less precise. So it's going to be able to be taken to, you know, a less precise
- 33:51amount of decimal places. And then double is kind of just like right there in the middle.
- 33:55For the purposes of this course, I think for the most part, we're just going to be using
- 33:58a double. I think for most use cases, a double is going to be just fine. But like I said,
- 34:03if you're doing something you know, where you need to be extremely accurate, like with
- 34:07money or something, then you want to use that decimal. So I'm going to say double and I
- 34:11can give this a name. So we can make this like a GPA, right? Someone's GPA might be
- 34:16like a 3.2. So this is a good example of a decimal number, you can also make these just
- 34:23normal numbers, so I can make it like 3.0. But anytime I'm using a decimal number, I
- 34:28need to include this point zero in there. So that's um, integers and doubles. And those
- 34:33are the two main types of like I said, numbers, we're gonna be able to represent whole numbers
- 34:37and then decimal numbers. And then we kind of touched on like the different types of
- 34:42decimal numbers. So after characters and numbers, we just have one more like core data type
- 34:49that we're going to be representing which is going to be called a Boolean. And a Boolean
- 34:53is essentially just a true or a false value. Now, a lot of times in programming, we're
- 34:59actually going to Want to be able to store true false information. And this might not
- 35:03be something that is super intuitive to you if you've never programmed before, but a lot
- 35:07of times storing information in the form of a true or false value is going to come in
- 35:11handy. So if I want to create a Boolean, I can just say b o l, and Boolean is just a,
- 35:17you know, as it essentially just means a true or false value. So I can say b, o l, and I
- 35:21can give this a name. So we can call this like, is male. And this Boolean will basically
- 35:26tell us whether or not someone is a male. So I could say true if we're a male. Or you
- 35:32could also say false. But those are the only two values that they should have, it should
- 35:36either be true, or it should be false. That's it. And that's basically why Boolean 's are
- 35:41useful because they can only have two values. So for the most part, I would say these are
- 35:47the core data types that you're going to be using in C sharp. Now there are a few other
- 35:52data types that we could get into, they're a little bit more obscure, and they're not
- 35:56used as commonly as these data types. For the most part, you're going to be fine with
- 36:01just strings, characters, numbers, and then Boolean values 99% of the stuff that you want
- 36:08to do, you're going to be able to do with just this data. And as a beginner, you only
- 36:12really want to concern yourself with these data types. Now I do want to point one more
- 36:16thing out is you can represent these data types outside of variable. So if I came down
- 36:21here and said, like console dot write line, I don't have to like store this type of data
- 36:28inside of a variable. If I don't want to like I could just print out like a string like
- 36:32hello down here, like this isn't actually stored inside of a variable, or I could print
- 36:36out like a number. Like if I wanted to print out 30, I could do that. And then when I run
- 36:41my program, you can see we printed out just fine. These are what we would call constants.
- 36:46And a constant is basically just like a value just like this 30. It's not stored in a variable,
- 36:52we're not keeping track of it anywhere, it's just kind of written out. And we can do the
- 36:55same thing for Booleans. So obviously, you don't have to store this information inside
- 37:00of variables. A lot of times it's useful to do that. But if you don't want to, you can
- 37:04just kind of use it like that. So that's the basics of data types in C sharp, and these
- 37:09are going to come in handy a lot really, with just these types of data up here, you can
- 37:13write a bunch of awesome programs. In this tutorial, I'm going to talk to you guys about
- 37:22strings in C sharp. Now strings are one of the most useful types of data that you're
- 37:27going to be working with in C sharp. So in this tutorial, I want to just show you all
- 37:31the ins and outs of working with strings, we're gonna look at some of the cool things
- 37:34that you can do with them, and how you can really leverage the power of strings inside
- 37:39of your programs. So down here, you'll notice that I just have this little console dot write
- 37:43line instruction. And basically, this is just going to write out some text onto the screen.
- 37:47So if I wanted to create a string, I can just make these open and close quotation marks.
- 37:52And then in here, I can type out some text. So I could type out like draft Academy. And
- 37:56now when I run my program, it's going to go ahead and print that out onto the screen.
- 38:01So anytime that I want to represent some sort of like plain text, or really any type of
- 38:05text in my program, I can do it with a string. And there's actually some cool things you
- 38:09can do with these strings. So let's say for example, that I wanted to print out draft
- 38:14Academy, but I wanted to print out draft on one line, and then Academy on the next line,
- 38:18I can use something called a new line character. So I could actually say backslash, N. And
- 38:23this is a special character that we can use in C sharp strings, which is basically going
- 38:28to specify that we want to type out draft, then a new line, then Academy. So now when
- 38:33I run my program, you'll see that draft and Academy are printed out on different lines.
- 38:37So that can come in handy when you're writing out your strings. And there's also another
- 38:41way, so if let's say that I wanted to write out a quotation mark, if I put a quotation
- 38:46mark here, you'll notice that this quotation mark effectively ends this string. And then
- 38:52this over here is just causing an error, I want to actually print out a literal quotation
- 38:57mark, that's not you know, delineating the string, I can just use that backslash again.
- 39:02And now we'll be able to print out that quotation mark, no problem, just like that. So those
- 39:06are some cool little things that we can do inside of our strings. You can also take a
- 39:10string and store it inside of a variable. So a variable is just a container that you
- 39:15can store different pieces of information. So if I wanted, I can create a variable over
- 39:19here to say string. And we can just call it like phrase. And I'm just going to set it
- 39:23equal to this text over here, which is just going to be draf Academy. And then down here,
- 39:29I could just print out the value stored inside of this variable. So I can print out the value
- 39:34stored in the phrase variable. And we're still printing out draft Academy. So that's kind
- 39:39of cool. Another thing you can do with strings is called concatenation and end cap and concatenation
- 39:44is the process of taking one string and then appending another string onto the end of it.
- 39:50So you're essentially just adding two strings together. So for example, over here, I could
- 39:54say string phrase is equal to draft Academy. And then I could use this plus sign and then
- 39:59this is basically going to add another string on to that. So I could say like, is cool.
- 40:06And now when I print out phrase it's going to be printing out draf Academy is cool. So
- 40:10it basically just added those two strings together, we would call that concatenation.
- 40:15And that can be really useful. Another thing we can do in our programs is find out information
- 40:19about our strings. So sometimes, you might want to figure out different pieces of information
- 40:23about the string, figure out some attributes about it. So let's say we wanted to figure
- 40:27out how many characters were inside of this string, I could just say phrase dot length,
- 40:33and that's with a capital L. And now what this is going to do is, it's actually going
- 40:37to tell me how many characters are inside of this string. So now we should get like
- 40:4215. Yeah, cuz there's 15 characters, inside of draft Academy. So that's a really cool
- 40:47way, we can find out some information about the string. In addition to finding out information,
- 40:52we can also use things called methods. And we're going to talk more about methods in
- 40:57a future tutorial. But essentially, all you need to know right now is that a method is
- 41:01basically a little block of code that we can call, which is going to perform a specific
- 41:06task. So these are basically like little predefined snippets of code that we can sort of tailed
- 41:13to execute, and then they'll do something. So we can use methods on our strings. And
- 41:17they'll either give us information about our strings, or they'll modify our strings in
- 41:22some way. So over here, if I want to use one of these string methods, I can just say the
- 41:27name of the string, or I could also type out like a literal string like that. And then
- 41:32I can just say.so, I'm going to say phrase, dot. And now I have access to all of these
- 41:38different methods. So if I want to use one, I can just type out the name of the method.
- 41:41And I'll show you these can kind of do little things with our string. So let's say that
- 41:46I wanted to take this string over here and convert it into all uppercase, well, there's
- 41:50actually a method that's going to be able to do that for us. So I could say phrase.to,
- 41:54upper, and then we can make an open and closed parentheses. And now this is actually going
- 42:00to take this string over here, convert it into uppercase, and then print out the uppercase
- 42:05string onto the console. So now when I run the program, you'll see we get draft Academy,
- 42:10all in caps, you can also do the same thing for lowercase. So I can say like to lower
- 42:15and now to do the same thing. So it should all be entirely lowercase now, which it is.
- 42:20So that's one way that we could like modify the value of a string, we can also find out
- 42:25some more information about this string. So imagine we wanted to figure out if this string
- 42:30of text contained a certain like substring of text, right? So maybe we want to see if
- 42:36this string had the word Academy in it, I could say phrase dot contains. And now what
- 42:42I'm actually going to do is I'm going to give this contains method, a piece of information.
- 42:47And this is called a parameter. So this over here, this contains method will tell us whether
- 42:52or not this phrase string contains like a certain set of characters or something. And
- 42:59in order to use it, I have to tell it, what I'm looking for. So I can say phrase dot contains.
- 43:04And in here, I could say, like, Academy, and this is basically going to return a true or
- 43:08false value telling me whether or not the phrase draft Academy contains the word Academy.
- 43:15And we should get a true value back because it does. So you can see over here we get true.
- 43:20But if I set over here, like academies, now all of a sudden, we should get false back.
- 43:26Because that string is not included in the phrase string, it's not included in draft
- 43:31Academy. And that can be kind of useful, you can like tell whether or not a certain string
- 43:36contains another string or another character. Another thing we can do is figure out the
- 43:41different characters inside of a string. So I'm going to get rid of that. Let's say that
- 43:45I wanted to figure out what the first character in my string was. Right? So let's say there's
- 43:49a situation where I have a string like phrase, and I wanted to figure out what's the first
- 43:53character in that string? Well, I can actually access individual characters and print them
- 43:59out. So I could say like phrase and I can make an open and closed square bracket. And
- 44:04inside of here, I can just put the index of the character that I want to print out inside
- 44:09of the string. So if I put a zero here, this is gonna tell me what this first character
- 44:14is. So phrase, square bracket zero should be printing out a capital G. And you see over
- 44:19here it does, if I wanted to figure out what the third character in the string was, I can
- 44:24say 012. So I can say two. And now this should print out that are for us. And you can see
- 44:33we get our so if you haven't caught on by now when we're indexing a string, we actually
- 44:38start at zero. So if I was going to assign index positions to each of the characters
- 44:43inside of this string, I would say that the G character is at index position zero. The
- 44:49eye character is an index position one the our character as an index position to the
- 44:54a character is an index position three, etc. So the first character this G is is actually
- 45:00an index position zero inside of the string. And that's basically just a way that C sharp
- 45:06and actually a way that most programming languages index strings. So when I say phrase zero,
- 45:13this is actually referring to this first character. So let's see if we could print out this a
- 45:19over here, well, it's basically going to be 012345678. So this is going to be an exhibition
- 45:26eight. So if I put an eight in here, now we should be able to print out that capital A,
- 45:33which we do over here. So that's sort of how string indexes work. And using this information,
- 45:39we can use another string method. So again, I'm just going to say phrase, dot and I want
- 45:44to show you guys another method that we can use it's called index of an index of is basically
- 45:50going to tell us not only if a string contains a certain value, but it'll tell us at what
- 45:55index position that value is located inside of the string. So for example, if I came over
- 46:00here and said, Academy, this is going to tell me at what index position Academy starts inside
- 46:08of this string. So when I run my program, you'll see that we're getting an eight over
- 46:13here, and we just sort of saw that, that capital A character started at index position eight.
- 46:19So basically, what this is saying is that Academy starts at 012345678. So it starts
- 46:27here, in addition to passing in a substring, like that, I could also pass in a character.
- 46:32And remember, a character is just an open and closed, single quotation mark. And then
- 46:37inside here, I could put like an F. And now this will tell me the location of the first
- 46:42f inside of this string, so it'll give me that index, you can see we get four. And that's
- 46:48because 01234, this starts at a four. And that can be pretty useful, there's also going
- 46:55to be the situation where the character isn't in there. So if I put a z in here, you'll
- 47:00notice that there's no z up here. So in this case, we're actually just going to get a negative
- 47:04one back and a negative one is always going to signify that the character is not inside
- 47:09of the string. So that can be pretty useful. So that's how we can use phrase dot index
- 47:14of, and that'll come in handy a lot. There's also one more of these string methods that
- 47:18I want to show you. And it's called substring. So I can say phrase, dot substring, I can
- 47:23make an open and close parentheses. And I'm actually going to need to give this a value.
- 47:29So basically, what this will allow me to do is it'll allow me to grab a part of this string,
- 47:35so I can grab like, one part of the string and then print that out onto the screen. And
- 47:39we can give this one parameter, we can give this one argument, in other words, one piece
- 47:43of information. And that's basically the index where we want to start grabbing the string
- 47:48at so let's say that I only wanted to grab this academy word and print this out, well,
- 47:53remember, Academy starts at index position eight, we already saw that. So this capital
- 47:57A is actually index position eight in the string. If I put an eight in here, this is
- 48:02now going to grab the characters starting at eight and going to the end of the string.
- 48:06So now, we should just print out Academy, which we do down here. You can also in addition
- 48:13to just passing in the starting index, you can pass in a length. And you'll notice that
- 48:17I'm just using a comma here, and now it's going to delineate that I want to pass in
- 48:21another value. So I'm passing in the starting index. And I can also pass in how many characters
- 48:26I want to grab. So I can say I want to start grabbing the characters at eight. And let's
- 48:31say that I only want to grab three characters. So over here, I can put a three. And now we're
- 48:36going to start at eight, and we're going to grab 123 characters, so we should get ACA
- 48:41printed out onto the screen. And you can see that we do so that substring method can come
- 48:47in handy. So that's kind of the basics of working with strings. And you know, so as
- 48:51soon as you guys kind of how we can work with them different things we could do like concatenation.
- 48:54And we can also print out things like new lines, we also looked at getting information
- 48:59about a string, like the length, and you know, individual characters in the string, we talked
- 49:03about the string indexes, we also talked about different methods that we can use to do different
- 49:08things with our strings. So hopefully, that kind of gets you up to speed with why strings
- 49:13are awesome and how they're useful. And to be honest with you, there's a lot more of
- 49:16these little methods that you can use. And if you want, you can just go online and Google
- 49:20C sharp string methods and you'll find like a huge listing of you know, probably at least
- 49:25you know a few dozen of these different methods that you can use to either find out information
- 49:30about or modify your strings. In this tutorial, I'm going to talk to you guys about numbers
- 49:40in C sharp a numbers are one of the most common types of data that you're going to be working
- 49:45with in your C sharp programs. So in this tutorial, I just want to kind of give you
- 49:49guys a full walkthrough of you know how to use numbers, the different types of numbers
- 49:53that we can use, and we're also gonna look at some cool things that we can do with numbers,
- 49:57things like you know, complex math operations. And we're just dealing with numbers in general.
- 50:02So it's gonna be pretty cool down here, I just have a basic program, it just is basically
- 50:07writing out a line of text. So if I wanted to, for example, like write out a number,
- 50:11I can just represent a number by just typing it out. So I could say, like 40, or 20. Or
- 50:16I could say, like negative 5.763, really any type of number I can work with. And there's
- 50:23really two main types of numbers, there's a whole number, which we would refer to as
- 50:28like an integer, and then there's a decimal number. So those two different types of numbers
- 50:32are going to be really useful in our C sharp programs. But it's important that you note
- 50:36that C sharp does distinguish between those two types of numbers. So there is a big difference
- 50:43between working with whole numbers and working with decimal numbers. And it's something that
- 50:47you just want to be aware of, as you sort of go through and write your programs. And
- 50:50so you know, basically, I could just type in a number here like 50, and then I'd be
- 50:54able to print it out onto the screen just over there like that. So numbers are pretty
- 50:58easy to work with. We can also do math with numbers, so I could say like five plus eight.
- 51:04And now instead of printing out five plus eight, like the literal text five plus eight,
- 51:09C sharp is actually going to go ahead and perform this math operation for us. So it's
- 51:14going to print out the result of adding five and eight together. So we're going to get
- 51:1713 over here. And we can use addition like that, we could also use subtraction, which
- 51:23is just that hyphen, we could use a forward slash, which would mean division. And we could
- 51:28also use a Asterix, which is going to represent multiplication. So any of those different
- 51:33operations, the four basic math operations you can use, we can also use another operator,
- 51:38which is called the modulus operator. So for example, if I could say, like, five, and then
- 51:44I can put a percent sign here, and I can say two, and we would read this as five mod two,
- 51:50and this modulus operator right there, essentially, what this is going to do is it's going to
- 51:54take five, it's going to divide it by two, and it's going to give us the remainder. So
- 51:59five divided by two is going to be two with a remainder of one. And that's exactly what's
- 52:03going to get printed out. So we should just print out one over here, which we do. So sometimes
- 52:08you might be interested in getting the remainder of a particular division operation. And this
- 52:13is going to go ahead and do that for us. We can also represent like mathematical equations.
- 52:18So one thing we can do is specify order of operations. So if I was to say like four,
- 52:24plus two, times three, actually, what's going to happen is the multiplication is going to
- 52:31happen first, and then this addition is going to happen. So it's going to be three times
- 52:35two, six, plus four, so we should get 10. And that's just sort of following normal order
- 52:41of operations rules. But if I wanted to change up the order of operations, I can use parentheses.
- 52:46So I could say like four plus two, which is going to be six, and now it'll be six times
- 52:52three. So we're going to get 18, instead of getting that 10. So you can use those parentheses
- 52:56to sort of change around the order that different calculations are getting performed. And those
- 53:01can be pretty useful. I also want to talk to you guys about the difference between using
- 53:06integers and using decimal numbers. So like I said, an integer is just a whole number,
- 53:10a decimal number would be, you know, a number that has a decimal on it, something like that.
- 53:14And actually, like I said, C sharp does distinguish between these two types of numbers. So if
- 53:19you're adding or subtracting or multiplying or doing some sort of mathematical operation
- 53:24with an integer, and a whole number, sometimes the results you get are going to be a little
- 53:28bit different. So if I was just to say, like 5.0 plus 8.1, if I was to perform a math operation
- 53:36with two decimal numbers, then I'm going to get a decimal answer back. So I'm going to
- 53:39get like 13.1. But if I was to perform a mathematical operation with an integer and a decimal, so
- 53:46five plus 8.1. And you'll notice that this is now an integer because it doesn't have
- 53:49a decimal point. Again, we're going to get a decimal back. So I'm going to get a decimal
- 53:53number back just like that. But when we do math operations with two integers, then we're
- 54:00always going to get an integer back. So if I said five divided by two, and you'll notice
- 54:05that these are both integers, I'm actually going to get an integer answer back. So really,
- 54:10this should be like two point something right should have some long decimal point. But when
- 54:14I actually run my program, you'll see that I'm getting an integer value back, I'm getting
- 54:18a two back. But if I was to convert this to a double, so if I said 2.0. So now this is
- 54:26going to be a decimal number, we're going to get a decimal answer back. So that's basically
- 54:31how that works. So any math operation that you do with two integers, no matter what you're
- 54:36going to get an integer back, but if you do a math operation with a decimal and an integer,
- 54:40then you'll always get a double back. And that'll kind of come in handy as you're working
- 54:43with these different types of numbers. Another thing we can do is we can store numbers inside
- 54:48of variables. So I can create a variable up here let's create one for int. So I'm just
- 54:52gonna say int num and I can give this a value of like six right so now this container num
- 54:58is storing the value of six And then I can print that out just like I normally would.
- 55:02So I can just say num down there. And it'll print out that six. There's another cool operation
- 55:07which we can do, which will basically allow us to increment this number. So down here,
- 55:11I can say num plus plus. And basically, what this is going to do is it's going to add one
- 55:17to the number. So now instead of being six, since we're adding one to the number, this
- 55:21is going to be seven. So when I run my program, you see we'll get seven. And you can do a
- 55:26similar thing with minus minus, so I could say like num, minus minus, and now instead
- 55:31of getting a six, we would get a five. And those are going to come in handy, just as
- 55:35like a shorthand as we go through our C sharp journey. So those are sort of the basics of
- 55:42you know, working with numbers. And you can see we can store them in variables. And we
- 55:45talked about, you know, interacting between integers and decimal numbers. And I also want
- 55:50to show you guys some other stuff we can do. So a lot of times in our programs, we're going
- 55:54to want to be able to perform different math operations. So there's all sorts of math operations,
- 55:59you know, like using exponents, and using the square root or you know, even doing like
- 56:04logarithmic stuff, or sine, cosine, tangent stuff, like there's all sorts of math things
- 56:08that we might want to be able to do inside of our programs I'm gonna show you guys, there's
- 56:12these things called methods. And a method is basically just a block of code that's going
- 56:17to perform a specific task. So I can do what's called call a method. And when I call a method,
- 56:23it's basically going to like perform a specific task or a specific function for me. And there's
- 56:29a bunch of these different math methods that we can use in our C sharp programs. And the
- 56:34way that we can get access to them is just by typing out math with a capital M. And then
- 56:38I can say, dot, and now I have access to all these different math operations. So you'll
- 56:43see down here, I can like scroll down through this list, and there's all these different
- 56:47things that I can do, I'm going to show you guys a couple of these cool math methods is
- 56:52what they're called. And you can kind of see what they do. So one is just math dot abs,
- 56:56and the stands for absolute value. So I can make an open and close parentheses and inside
- 57:01of here, I'm going to put a number. So if I said like negative 40, this is going to
- 57:05give me back of the absolute value of negative 40, which is just going to be 40. As you can
- 57:10see, there's another one, which is called pow, and this stands for power. So I'm actually
- 57:16going to be able to give this two numbers and it's going to spit out the first number
- 57:21taken to the second number. So if I said like three and then to over here, this is going
- 57:26to give me back three raised to the second power. So we should get nine. And you can
- 57:32see we do and that's this will also work with like decimal numbers. So I can say like 3.8
- 57:38raised to the second power. And now we should be able to run this as well. So 14.44. And
- 57:42you can also do the opposite. So we could say like Sq RT, this is going to Stanford
- 57:48square root. So if I put like a 36 in here, now, I'm actually going to get back to the
- 57:56square root of 36. So we should get six back. And we do, there's also another useful method,
- 58:03which is called min max. And with Max, you can actually pass two numbers into these parentheses.
- 58:09So I could say like four and 90, and Max is actually going to spit out whichever one is
- 58:14bigger. So it'll tell us which number is the biggest. And so this should give us 90, as
- 58:20you can see down there. And that can come in handy, there's going to be a lot of situations
- 58:23in C sharp, where you might have two numbers, maybe they're stored inside of variables,
- 58:29and you don't necessarily know which one is bigger. So you can use that math function
- 58:33to figure that out. There's also another one called min, and that just does the opposite.
- 58:37So now this will give us the smaller of the two numbers. So we're going to get four back.
- 58:42There's one more that I want to show you guys. It's called math dot round. And this is a
- 58:46really great way to round numbers. So when I say math dot round, I can pass this like
- 58:52a decimal number like 4.3. And this will round it just according to standard rounding rules.
- 58:57So you can see, we get four. But if I was to up this to like 4.6, now we're gonna go
- 59:03ahead and get a seven or a five back. So we get the five back there. So those are just
- 59:08some basic, you know, math operations that you can do. And you can see I'm using this
- 59:12Visual Studio text editor when I just say math, and then I click this dot, you can see
- 59:17all of these different options are showing up down here. So there's all these different
- 59:22methods that we can call, I mean, there's all sorts of stuff we can do. And you can
- 59:26kind of just like play around with each of these, like, see what they do. But for the
- 59:30most part, like any major math operation that you're going to want to be able to do, there's
- 59:34usually going to be a method for it, or at least a method in here to help you with it.
- 59:39So you definitely want to check that out. And you can also just google like C sharp
- 59:42math methods on line and you'll find a huge listing of them. So that's sort of the basics
- 59:47of you know, using math and then also working with numbers. Again, numbers are extremely
- 59:51important. So you want to make sure that you're, you know, up to date and sort of up to speed
- 59:55with how to use them and how they interact with each other and all that stuff in the
- 1:00:02C sharp. In this tutorial, I'm gonna show you guys how to get input from a user in C
- 1:00:08sharp. Now one of the cool things about C sharp is we can actually have our programs
- 1:00:13interact with the user. So I can run my program. And I can actually like give it some information.
- 1:00:18And it can use that information to do different things. And when you think about it, most
- 1:00:23programs that you probably use on a daily basis are going to allow you to input information
- 1:00:27into them. And then depending on the information that you input, they're going to do different
- 1:00:31things. So today, we're going to look at the most basic form of that, which is just, you
- 1:00:36know, allowing the user to basically like type in a value, and then we can use that
- 1:00:39value to do something. So down here, I have a simple program set up, you'll see I'm just
- 1:00:44saying console dot write line, and I'm writing out Hello. And then down here throughout this
- 1:00:49core. So far, I've always included this line of code in our little programs. And basically
- 1:00:54what this line of code has been doing is it's been allowing us to see the output of our
- 1:01:00programs, right. So if I include this read line, when I run my program, that I can see
- 1:01:06what gets output so I can see Hello, right sort of stays on the screen. But if I was
- 1:01:11to get rid of this line, like if I remove this, when I click the start button, you'll
- 1:01:15see that that window, like pops up really quick and then disappears. And that's because
- 1:01:21we're essentially executing this line of code. And then once that line of code has been executed,
- 1:01:26there's nothing else to execute, right, there's no other instructions for the computer to
- 1:01:30like, carry out. So essentially, the program just terminates, and then that window disappears.
- 1:01:36We've been using this console dot read line instruction in order to keep that black window
- 1:01:42up on the screen in order to keep the console on the screen. Because what this line does,
- 1:01:47is it actually waits for the user to enter in a line of text. So this line essentially
- 1:01:53pauses the execution of our program, and waits for the user to enter something. So if the
- 1:01:59user hasn't entered something, then our program is essentially just paused. So actually, if
- 1:02:03you guys watch over here, my program is essentially paused. But if I was to enter something, and
- 1:02:09then click enter, the program terminates, right, because now this line of code down
- 1:02:14here has finished executing. But what's cool about this line is, like I said, it pauses
- 1:02:19the program. So we can sort of see what's happening. Another thing that this line of
- 1:02:23code does though, is it allows us to enter in a line of text. And we can actually use
- 1:02:28this instruction over here to get input from the user. So I'm going to show you guys how
- 1:02:33we can do that. In this tutorial, I want to basically design a little application, that's
- 1:02:38going to ask the user for their name, it's going to get their name, and then it's going
- 1:02:42to say hi to them. So I'll basically be able to type in my name, and the computer will
- 1:02:46be like, Hey, Mike, what's up. So the way we can do that is with a couple steps, the
- 1:02:51first thing we want to do is prompt the user for information. So I'm basically just going
- 1:02:55to write out a line and I'm just gonna say console dot write. And you'll notice before
- 1:03:02we were using console dot write line. Now essentially, the difference between write
- 1:03:06and write line is that when we say write line, this is going to print out a line of text,
- 1:03:12and then it's going to go on to a new line. So it's basically going to print out and then
- 1:03:15we'll go on to a new line, when I just say console dot write, it's not going to go off
- 1:03:20to a new line, it's just going to kind of print out on the same line. And you guys will
- 1:03:24see what I mean in a second. So I'm going to use console dot write because I'm basically
- 1:03:29going to be prompting the user. So I can just say, enter your name, and then I'm going to
- 1:03:34make a colon. So this is going to be our prompt for the user. Now the next thing that we want
- 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,
- 1:03:46like I said, with console dot read line, just like that. And this is going to pause the
- 1:03:52program and wait for the user to enter in a line of text. But here's the thing, I don't
- 1:03:57just want to let them enter in the line of text, I want to be able to take the text that
- 1:04:01they enter in store it inside of a variable, right? Like if they just enter text, it's
- 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
- 1:04:11they entered somewhere. And we can do that with a variable. So whenever we use this console
- 1:04:15dot read line, it's basically going to get the line of text that they enter as a string.
- 1:04:20So it's going to store the line of text that the user enters as a string of characters.
- 1:04:24So I'm going to create a variable. And we're just going to call this name and I'm going
- 1:04:29to set this equal to console dot read line. So I'm saying string name is equal to console
- 1:04:36dot read line just like that. Now the last thing I want to do is I'm actually going to
- 1:04:41write something out to the screen. So I'm just going to say write line. And over here,
- 1:04:47we're going to print out Hello, and then we're going to print out the name that the user
- 1:04:51entered. So I'm basically just going to print out Hello, plus name. So basically, the user
- 1:04:59is going to enter in Name. And then we're going to say hi to the user. And you'll notice
- 1:05:03I'm keeping this relined down there, just so we can see what the output is. So let's
- 1:05:09go ahead and run this program. And we'll see what happens. So over here, it says, enter
- 1:05:13your name. And you'll notice over here, it's letting me enter this on the same line as
- 1:05:18this prompt. And that's why we use that console dot write instead of console dot write line.
- 1:05:24So over here, I'm just gonna type in my name, my name is Mike. And now when I click enter
- 1:05:28the value, Mike is going to get stored inside of that name variable. And then we're going
- 1:05:33to be able to print it out on the screen. So you can see down here, we're printing out
- 1:05:36Hello, Mike. And that's essentially all we needed to do to get this program to work.
- 1:05:42So we use console dot read line, which pause the program and let the user enter in a line
- 1:05:47of text. When they hit enter, the value that they entered gets stored inside of this name
- 1:05:53variable, and then we printed it out onto the screen. So that's pretty cool. And we
- 1:05:58can actually do this as many times as we want. And so if I wanted, I could get another piece
- 1:06:01of information from the user. So I'm going to do that, I'm going to copy this, and we'll
- 1:06:05come down here. And this time, I'm going to ask them to enter in their age. So over here,
- 1:06:11we're going to create another string, and we're just going to call it age. And again,
- 1:06:15I'm just gonna say console dot read line. So now in addition to having their name, we
- 1:06:20also have their age. So I can say hello, name. And then I can basically tell them how old
- 1:06:25they are. So I can say you are, and then we'll print out their age. So now we're saying hello,
- 1:06:31whatever the name is, you are, whatever their ages. So let's go ahead and run this program.
- 1:06:36Now we're getting two pieces of information from the user. So it says, enter your name.
- 1:06:40So we can enter in like john, and let's say john is 40 years old. So now when I click
- 1:06:46enter, we're going to have two values. And we'll be able to print them out. So it says,
- 1:06:51Hello, john, you are 14. So that's sort of the basics of getting input from the user.
- 1:06:58And again, remember what I said, when I use console dot read line, it's taking whatever
- 1:07:03the user enters, and it's storing it as a variable. So this is always going to be a
- 1:07:07variable, it's always going to get the information that they enter as a variable. And that's
- 1:07:12why we had to store both of those pieces of information inside of strings. But there's
- 1:07:17actually a way that we can get other types of information like numbers from the user.
- 1:07:22And in the next tutorial, we're going to build a little calculator game. And I'm going to
- 1:07:25show you guys how we can actually get numbers from the user. But for now, that is sort of
- 1:07:31the basics of doing this. And you probably just want to practice with this and maybe
- 1:07:35make it a little application of your own. In this tutorial, I'm going to show you guys
- 1:07:42how to build a basic calculator in C sharp. So we're basically going to build a little
- 1:07:47program where the user can enter in two numbers. And then our program will add them together
- 1:07:52and spit out the answer onto the screen. So should be kind of cool. And we're also going
- 1:07:56to talk about getting numbers as input from the user. So this is going to be a pretty
- 1:08:01cool tutorial. So down here, before we start building this, I actually want to show you
- 1:08:06guys how we can convert a string into a number. And this is a technique that's going to come
- 1:08:12in handy for this application. Essentially, like if I had a string that had a number in
- 1:08:18it, so like 45, or like 45 point 80, what I could do is I can actually use a special
- 1:08:24method in C sharp that would be able to convert this into an actual number. And the reason
- 1:08:30that's useful is because like if I had console dot write line, like if I had two strings
- 1:08:38that had numbers inside of them, like 43, plus 56. If I tried to add these two together,
- 1:08:46this is just going to concatenate the strings. So you see over here, we get 4356. So it's
- 1:08:52not adding the two numbers that are in here together, it's just like concatenating the
- 1:08:57strings together. So in certain cases, you'll have numbers that are kind of trapped inside
- 1:09:01of strings. And actually, we're going to have that situation in this tutorial. And so you
- 1:09:06want to be able to get the numbers out of there so that you can like do math with them.
- 1:09:09So I'm gonna show you guys how we can convert a string into a number. So actually over here,
- 1:09:15why don't we come down here and we're going to make a number variable. So I'm just going
- 1:09:20to make an integer. And we'll just call this num and I'm going to set it equal to a string.
- 1:09:26So I'm going to set equal to a string that has a number in it like 45. You'll see over
- 1:09:31here, C sharp is yelling at us because this number is actually a string so it's not going
- 1:09:36to be able to be stored inside of an integer variable. So we need to convert this into
- 1:09:43a integer. And the way that we can do that is just by typing out, convert like that not
- 1:09:48end with a capital letter, and then dot and now we need to specify what we want to convert.
- 1:09:54So you'll see over here there's a bunch of options like to in 32, two and 16 to double
- 1:09:59to decimal to string, this little convert function is basically going to allow us to
- 1:10:05convert different values to other values. In our case, we want to convert this to an
- 1:10:08integer. So I'm just going to say convert.to, int 32. And that was one of the options that
- 1:10:14we had over here. And then I'm going to make an open closed parentheses around the actual
- 1:10:20string. So when I do this, this is going to go ahead and convert the string inside of
- 1:10:25here into an integer. And just to note, the string that we put in here has to have a number
- 1:10:31inside of it. Like if this was just something like that, this isn't going to work, we're
- 1:10:33actually gonna get an error. But if I have a number in here, like 45, then this is going
- 1:10:38to work just fine. So if I was to come down here, now I could actually use this as an
- 1:10:43integer. So I could say num, plus six. And now we'll actually be able to do this math
- 1:10:49operation. So you can see we get 51. And that's essentially how we can convert a string into
- 1:10:56an integer. And just so I can point this out, like if this didn't have a number inside of
- 1:11:00it. Or even if it did have a number inside of it, but the number had like text next to
- 1:11:05it, this isn't going to work. So we're actually going to get an error. And you can see the
- 1:11:09program wasn't able to run, because it says down here input string was not in correct
- 1:11:16format. So that's just something to keep in mind. And later in the course, we're actually
- 1:11:20going to talk about how we can fix something like this and mitigate it. But for now, we're
- 1:11:24just going to assume that the string is going to be formatted correctly. And I'm just going
- 1:11:27to exit out of this exception. Alright, so that's kind of an explanation of how we can
- 1:11:31convert a string into an integer. And we're actually going to be able to use this in our
- 1:11:35program. So like I said, we're going to build a calculator program that's going to allow
- 1:11:39the user to enter in two numbers. And then we'll take those numbers, add them together
- 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
- 1:11:47to say console dot write. And I'm actually just going to use right, and then over here,
- 1:11:52I'm just gonna say enter a number. And now we're prompting the user to enter a number.
- 1:11:57And what I want to do now is I want to be able to get the number that they enter. So
- 1:12:00I'm just going to say console dot read line. And this is going to go ahead and wait, it's
- 1:12:07going to pause the program until they enter in a line of text. And so what I actually
- 1:12:12want to do is I want to store the number that they enter inside of a variable. So I'm going
- 1:12:17to create a variable over here, and and we're just going to call it num one. And I'm going
- 1:12:21to set it equal to console dot read line. But you'll notice over here, we're getting
- 1:12:25an error. And the problem is that console dot read line is going to give us a string.
- 1:12:31So console dot readline takes whatever the user enters and turns it into a string. And
- 1:12:35that's sort of the value that we have. If we want to eventually add these numbers together,
- 1:12:40we're not going to be able to use a string like I showed you before. So we're actually
- 1:12:44going to have to convert this into an integer, which now we know how to do. So I can just
- 1:12:48say convert.to, and 32. And I'm going to surround console dot read line, this whole thing with
- 1:12:56parentheses. So now this whole thing is surrounded with these parentheses, and it's going to
- 1:13:01get converted into an integer. And that's basically all we need to do for that. So now
- 1:13:05I'm going to copy this and we'll be able to get the second number. So I'm gonna come down
- 1:13:08here, and I'll just say enter another number. And we're going to call this one, two. So
- 1:13:15now we're prompting the user for a number, we're getting that number and turning it into
- 1:13:20an integer. And we're doing the same thing down here. So the last thing we want to do
- 1:13:23now is just print out the answer. So I'm going to go ahead and say console dot print line
- 1:13:29or write line. And I'm just going to write out number one plus number two, because we
- 1:13:36converted these guys into integers, we're going to be able to add them together. So
- 1:13:40let's go ahead and run this program. And we'll see how we did. It says enter a number. So
- 1:13:44let's enter in a 10 and insert another number. So why don't we insert a three. And so now
- 1:13:50it's going to convert both of those into integers, and add them together and we get 13. So looks
- 1:13:55like the program's working out pretty well. There's actually a problem with this program,
- 1:13:59though, if I was trying to add a integer number with a decimal number. So if I said like four,
- 1:14:05and over here, I put in like a 2.6. This is actually not going to work. So you'll see
- 1:14:11the program isn't actually printing anything out. That's because the program is stalling,
- 1:14:16we didn't enter in a correct value, we inserted a decimal value and this can't be converted
- 1:14:22into an integer. So you'll notice that the program over here is stalling. And basically,
- 1:14:28that's because we entered in a decimal number. So if I wanted to allow this program to be
- 1:14:33able to handle decimal numbers, instead of converting these two integers, I'd want to
- 1:14:37convert them to doubles. So I could change both of these two doubles like this double
- 1:14:43double. And instead of saying convert dot two and 32 I could say convert.to double and
- 1:14:49same thing down here. And now we should be able to work with decimal numbers in our program.
- 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
- 1:15:04those two together. So in certain circumstances, you might want to have a situation where the
- 1:15:07user can only answer an integer numbers. But I think in most cases, you'd want to use doubles,
- 1:15:12just like I did down there. And you can see how easy it is to convert them into doubles
- 1:15:16instead of integers. So that's a very basic calculator. And that kind of gives you an
- 1:15:20idea of how we can get numbers as input from a user. In this tutorial, I'm going to show
- 1:15:30you guys how to build a little mad libs game in C sharp. Now, if you're not familiar with
- 1:15:35Mad Libs, it's basically a game where you can enter in a bunch of like random words
- 1:15:40and maybe like a couple nouns and adjective and adverb, and you'll take all those random
- 1:15:45words, and you'll kind of sprinkle them in throughout a story. And generally, because
- 1:15:48you're entering in random words, the story ends up being like pretty funny. So I actually
- 1:15:53have an example of a Madlib over here on my web browser, you'll see it's basically just
- 1:15:56a little story and you kind of sprinkle in different words like this one that's asking
- 1:16:01for an adjective or a plural noun or a celebrity name. So these are pretty fun. And I actually
- 1:16:06want to show you guys how we can build a Mad Libs for ourself in C sharp, it's actually
- 1:16:11really easy. So down here, I have a little program set up. It's basically just writing
- 1:16:16out this story. So it's writing out Roses are red, violets are blue, I love you. And
- 1:16:21this is sort of like a classic little poem. But I think this poem would be a lot better
- 1:16:25if we turned it into a Mad Libs. So how about instead of saying Roses are red, we would
- 1:16:31say roses are and we'll let the user choose a color. And instead of saying violets are
- 1:16:36blue, we'll let the user choose a plural noun. And instead of saying, I love you, we'll let
- 1:16:42the user choose a celebrity that they're going to love. And so will basically allow the user
- 1:16:48to enter in three random words, and then we'll sprinkle them in through our story. And hopefully,
- 1:16:54it'll be pretty funny. So let's think about how we can build a Mad Libs, essentially,
- 1:16:59what we're going to do is we're going to have to prompt the user for input, we're going
- 1:17:02to have to take that input and store it inside of variables. So let's go ahead and do that.
- 1:17:08And, essentially, what we're going to need is we're going to need three variables to
- 1:17:12store the color, the plural noun and the celebrity. So I'm actually going to show you guys one
- 1:17:18way that we can do that. And we're actually going to declare these variables first. And
- 1:17:23then we're going to give them value. So up here, I'm just going to say string. And I'm
- 1:17:26basically just going to tell C sharp that I'm going to want to use three variables in
- 1:17:30my program. So I'm going to want to use one called color, I'm going to want to use one
- 1:17:34called plural noun. And I'm going to want to use one called celebrity. And I'm just
- 1:17:40gonna put a semicolon after this. And if you've been following along with this course, we
- 1:17:44haven't necessarily been doing you know, exactly this, every time we were creating variables.
- 1:17:50But this is one way that we can create a variable. And we're basically just declaring the variables
- 1:17:55up here. And now down here, we're going to go ahead and give them values, and actually
- 1:18:00want to let the user give these variables their values, because the user is going to
- 1:18:04be entering in the color, the plural noun and the celebrity. So let's go ahead and write
- 1:18:09out some prompts. So I'm just going to say console dot write. And in here, I'm going
- 1:18:14to ask the user to enter a color. So I'm just going to say, enter a color. And after I prompt
- 1:18:21them, I want to wait for them to enter in the color. So I'm just gonna say, console
- 1:18:26dot read line. And this is basically going to pause the program and wait for them to
- 1:18:32enter in a line of text. But I want to take the line of text as they enter in store it
- 1:18:37somewhere. So I'm going to store it inside of this color variable. So I'm just going
- 1:18:41to say color, which we declared up here, and down here, I'm going to give it a value. So
- 1:18:45I'm gonna say color is equal to whatever the user enters. And we can basically do the same
- 1:18:50thing for the other two. So I'm going to go ahead and copy this. And down here, I'm just
- 1:18:55going to paste it. So I'm actually gonna make some new lines. And so now, instead of a color,
- 1:19:01we're going to want to get a plural noun. And then down here, instead of setting the
- 1:19:05color variable equal to whatever they enter, now, we're going to store this value inside
- 1:19:10of the plural noun variable. And then finally, down here, instead of asking for the color,
- 1:19:15we're going to ask for a celebrity. And we're going to store that inside of the celebrity
- 1:19:20variable. So now we've prompted the user to enter in those three things, we've stored
- 1:19:26all of their responses inside of variables. So the last step is to put those guys into
- 1:19:31our story. So over here, I'm gonna say roses are, and I'm gonna use the color. And then
- 1:19:39instead of just saying plural noun, I'm gonna say, plural noun, plus our blue. And then
- 1:19:46finally, we'll do the same for the celebrity. So I'm taking the variables and I'm sprinkling
- 1:19:53them in through this story. So depending on what the user enters, this story is actually
- 1:19:58going to be a little bit different. So Let's go ahead and we'll run this program and we'll
- 1:20:02see how we did. So over here it says enter a color. So why don't we enter in like magenta?
- 1:20:10Enter a plural. Now let's do microwaves and enter a celebrity, why don't we do like Tom
- 1:20:17Hanks. So all of these values that I entered are getting stored in the corresponding variables.
- 1:20:24And now when I click enter here, it should print all of them out inside of our Mad Libs.
- 1:20:30So I click Enter, and it says roses are magenta, microwaves are blue, I love Tom Hanks. So
- 1:20:36we actually built a fully functioning madlibs, right, I can go in, I can enter in any values
- 1:20:41that I would want, and those are gonna get sprinkled into the Madlib. Now over here,
- 1:20:46we built a very simple madlibs. Right, this was, you know, essentially three inputs, and
- 1:20:51then we put them into a very simple poem. But you can see how you can use this same
- 1:20:56technique in order to ask the user to enter in like maybe 10 or 20 different words, and
- 1:21:01you can make a way more complex story down there. So essentially, with very few lines
- 1:21:07of code, I mean, this is probably, you know, 10 lines of code, we were able to write out
- 1:21:11an entire Madlib in C sharp. In this tutorial, I'm going to talk to you guys about arrays
- 1:21:21in C sharp, a lot of times when we're running programs in C sharp, we're going to be dealing
- 1:21:25with all types of data, a lot of times we're going to be dealing with large amounts of
- 1:21:30data. And one way that we can manage and keep track of the data in our programs is by using
- 1:21:36variables. But the one problem with variables is that they can only store a single value.
- 1:21:42So I can create a variable like an integer variable or a string variable. But I could
- 1:21:46only store one integer or one string inside of that variable container. And a lot of times,
- 1:21:53if we're dealing with huge amounts of information, we're not going to want to have to create
- 1:21:57like hundreds or 1000s of variables. So it's actually another container, another type of
- 1:22:03data structure that we can use in C sharp, we'll just call it an array. And an array
- 1:22:08is basically a structure that allows us to hold multiple pieces of information in the
- 1:22:12same container. So unlike a variable that can only hold one value, an array can hold
- 1:22:17multiple values. So you can hold 10 or 100, or 1000, or even a million values inside of
- 1:22:22an array. So a good way to think of an array is just a collection of individual variables,
- 1:22:27right, so an array can hold like 20 different values that you would normally have to put
- 1:22:31inside of a variable. So I'm gonna show you guys how we can create arrays, we'll talk
- 1:22:35about sort of the basics of using them. And I'll kind of get you guys up to speed with
- 1:22:39using arrays in C sharp. So down here in my program, I'm going to go ahead and create
- 1:22:45an array. And remember, an array is just a container that can hold a bunch of pieces
- 1:22:49of information. So we create an array very similar to how we create a variable, the first
- 1:22:54thing we have to do is specify the type of information or the type of data that the array
- 1:22:59is going to hold. So we can use all the basic data types string in double Boolean, we can
- 1:23:04use all those things. In our case, let's make an array of integers. So I'm just going to
- 1:23:08say int, and then I'm going to type a space. Now whenever we're creating an array in C
- 1:23:14sharp, we always need to let C sharp know that. So the way that we can tell C sharp
- 1:23:18that we want to make a variable instead of an array is by making an open and closed square
- 1:23:22bracket. And that's going to signify that this is going to be an array. And then over
- 1:23:26here, I can type the name of the array that I want to create. So I can give this array
- 1:23:30a name. So why don't we just call this like lucky numbers, we'll create an array that's
- 1:23:35going to store a list of lucky numbers. In order to give this array some information.
- 1:23:41There's actually a couple of different ways we can assign values to arrays, I'm going
- 1:23:44to show you guys all the ways in this tutorial. But the most basic way is to just make an
- 1:23:49open and closed curly bracket. And then inside of here, we can start typing out the values
- 1:23:54that we want to put inside of our array. So I can just start typing out some numbers,
- 1:23:59and we'll be able to store them all in the same container. So over here, I'm just going
- 1:24:03to tell you like for a 15 1620 3042. So I'm just typing out a bunch of different integers.
- 1:24:11And you'll notice that I have one value here, and then I separate it with a comma, and then
- 1:24:17I can put another value. So all of these would be considered elements inside of the array.
- 1:24:22So I would say like, four is the first element in the array, eight is the second element
- 1:24:26in the array. And we can delineate the elements using these commas. And essentially, now we
- 1:24:32have a container, we have this lucky numbers container, which is able to store all of these
- 1:24:38values. And this makes it really easy for us to manage and maintain large amounts of
- 1:24:43information. So I mean, over here, I only have like maybe six numbers, but if I wanted
- 1:24:47I could put hundreds or 1000s or even millions of values inside of one of these arrays, and
- 1:24:53they're extremely useful. So now that we looked at how we can create an array, let's talk
- 1:24:57about how we can access the individual element inside of an array, right, because this array
- 1:25:02is going to be no use to us unless we can actually get access to each of these values.
- 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
- 1:25:11write line. So we'll just print out particular value onto the screen. So if I want to access
- 1:25:16a particular value, I can just type out the name of the array. So in our case, lucky numbers,
- 1:25:21and then I want to make an open and closed square bracket. And inside of this square
- 1:25:25bracket, I want to give a number so I'm basically going to specify the index of the element
- 1:25:31that I want to print out or the index of the element that I want to access. So let's say
- 1:25:35I wanted to access this element four right here, it's the first element in the array,
- 1:25:40I'm going to put a zero here. So this four element is at the zeroeth position, it's at
- 1:25:45the zero index inside of the array. So now when I run my program, you'll see we're printing
- 1:25:50out four, so we're printing out that first element in the array. And so the way that
- 1:25:56we index these array elements is starting with zero. So if you've been following along
- 1:26:01with this course, you'll know that this is the same way we index strings. So I would
- 1:26:05say that four is at index position, zero in the array, eight is at index position. 115
- 1:26:11is an index position to 16 at three, etc. So we start counting. In other words, we started
- 1:26:17the indexing at zero. So over here, I could say like lucky numbers, two, and now we're
- 1:26:22going to get 012, we're going to get this 15 back. So we'll be printing out that 15
- 1:26:28you can see over there. So essentially, when we access an element in the array like this,
- 1:26:32it's a lot like we're accessing a variable. And it's actually you can think of each one
- 1:26:36of these elements in here as their own kind of separate variables. And you can do everything
- 1:26:41with them that you could do with normal variables. So another thing we could do would be to update
- 1:26:45one of these elements. So for example, if I wanted, I could go ahead and change one
- 1:26:50of the values over here. So let's say we wanted to change this element, we wanted to change
- 1:26:54eight, we could actually just say, lucky numbers, and I'll make an open and close square bracket,
- 1:27:00I'm going to put the index of the element that I want to change. So this eight is going
- 1:27:04to be at index position one, and I can give us a new value. So we can give this a value
- 1:27:08of like 900. Now if I print out lucky numbers, one, you'll see that the value will have updated.
- 1:27:16So now we get the value of 900, instead of just getting the value of eight. So I'm able
- 1:27:21to update these, I can really, you know, do anything that I could do with a normal variable
- 1:27:26with each one of these individual values. And that's pretty cool. So in addition to
- 1:27:30creating a, an array of integers, I could also create like an array of strings. So why
- 1:27:36don't we do that I'm gonna make an array of strings. And I'm just going to call this friends.
- 1:27:40So this will be like a list of maybe my friends or something. And I'm going to show you guys
- 1:27:44another way that we can create an array like this. So you'll see up here right away, I
- 1:27:49was giving this array A bunch of value. So right off the bat, when I created it, I was
- 1:27:54populating this array with a bunch of information. But a lot of times when you create arrays
- 1:27:59in C sharp, you're not going to know exactly what elements you want to put inside of them.
- 1:28:05So if you don't know exactly what elements need to go in here, upfront, we can just create
- 1:28:09an empty array. So I can actually just say, a string friends, and I'm just going to set
- 1:28:14this equal to new string. And then I'm going to type in open and close square bracket.
- 1:28:19And inside of these square brackets, I need to put a number and this number is going to
- 1:28:24tell a C sharp, how many elements we want this array to be able to hold. So if we create
- 1:28:29an array like this, and we don't give it elements like we did up here, we still need to tell
- 1:28:34C sharp how many elements this array can hold, we need to basically tell C sharp, how big
- 1:28:40we want to make the array. So over here, if I put 10 like that, that means that this friends
- 1:28:45array is going to be able to hold 10 values. If I put three, that means the friends array
- 1:28:50is going to be able to hold three values. So why don't we put five and down here, what
- 1:28:55I can do is I can populate this array with information. So I could say like friends,
- 1:29:00zero is equal to and then I could type out the name of a friend. So I could say like,
- 1:29:05Jim, and then I could do the same thing for the other one. So I could say like friends,
- 1:29:10one is equal to Kelly, etc. So I could do that for each element inside of this array
- 1:29:15and individually populate it. And this is actually going to come in handy in a lot of
- 1:29:19situations. And as we go forward in the course, I'm going to show you guys how we can do something
- 1:29:23like that in a future tutorial. But for now, just know that this is another way that we
- 1:29:27can create an array. Alright, so really, that is the basics of using arrays. Arrays are
- 1:29:31extremely simple, but they're extremely powerful. It's essentially just a type of container
- 1:29:36much like a variable. But unlike a variable, it can store multiple pieces of information.
- 1:29:42And again, all of those pieces of information need to be of the same type, just like we
- 1:29:46have up here. In this tutorial, I'm going to talk to you guys about methods in C sharp.
- 1:29:56Now a method is basically just a little block of code where we can perform a specific task.
- 1:30:02So a lot of times when you're writing programs in C sharp, you'll have like a few lines of
- 1:30:06code, maybe like five or 10 lines, which are performing a specific task, like the whole
- 1:30:11idea of having those lines in the program is that they're going to do a certain thing.
- 1:30:16So what we can actually do is we can create something called a method, which will allow
- 1:30:19us to take all that code and put it into its own little container. And then we want to
- 1:30:25execute it and we want to perform the task, that code is going to carry out, we can just
- 1:30:29call that method, and we'll be able to reuse it in different spots in our program. So I'm
- 1:30:35gonna show you guys exactly how to create a method. And we're going to talk exactly
- 1:30:38about what it does and how they're useful. So what I want to do in this tutorial is I
- 1:30:43want to create a method, which is going to say hi to the user. So the task of this method
- 1:30:49is going to be to say hi to whoever is using the program. So what I want to do when I create
- 1:30:56a new method is I actually want to come down out here outside of these curly brackets.
- 1:31:02So believe it or not this line of code up here where it says static void Main, this
- 1:31:06is actually a method inside of our program. So like I said, a method is just a container,
- 1:31:12or it's sort of like a block where we can put code that's going to perform a specific
- 1:31:17task. And this main method is very special, because this is the method that gets executed
- 1:31:24when we run our program. So any code that I typed down here in this main method gets
- 1:31:29executed when I run my program. But in addition to writing this main method, we can also define
- 1:31:35other methods that are going to work a little bit differently. But it's the same idea, we're
- 1:31:40encapsulating a bunch of code inside of its own little container. So in order to create
- 1:31:45a new method, I want to first go outside of this main method. So this main method is all
- 1:31:49the code that I have highlighted, you can see it's delineated with this open and closed
- 1:31:54curly bracket. So I'm going to come down here below this. And we can actually create a new
- 1:31:58method. So when I create my method, I need to tell C sharp a couple of different things.
- 1:32:04And the first thing we need to do is, say a keyword static. And I'm actually going to
- 1:32:10talk a lot more about what static means in later tutorials. And as of right now, it's
- 1:32:16really not super important as to what this is doing. Again, I will explain what this
- 1:32:20is. But for now just know that we have to put static here, if we're going to put our
- 1:32:24method alongside this main method. After I say static, I need to specify something called
- 1:32:30a return type. Now, these methods are awesome, because they can perform specific tasks for
- 1:32:35us. And one of the things they can do is they can actually give us back information. So
- 1:32:40if I was to run one of these methods, it can actually give information back to me. And
- 1:32:45we'll see how to do that. We're going to talk more about return types in the next tutorial.
- 1:32:49But for now, I'm just going to say void, and that basically means that this method isn't
- 1:32:53going to return any information. So this is kind of like the simplest method that we can
- 1:32:57create. Alright, so after I say void, I want to give this method a name. Now remember,
- 1:33:02methods are generally going to be performing a specific task. So you want to name the method
- 1:33:08based off of the task that it's performing. Our method in this tutorial is basically just
- 1:33:13going to say hi to the user. So I'm just going to call this Say hi. And generally in C sharp,
- 1:33:20when we're creating a method, we're going to create it using a capital letter for the
- 1:33:23name. So you can see I named it just like that you can also see up here, main was named
- 1:33:28with a capital letter. And that's just a naming convention, that's not necessary. But a lot
- 1:33:33of people are going to do that, then what we want to put in here is an open and close
- 1:33:36parentheses, and then an open and closed curly bracket over there. And I'm just going to
- 1:33:40go ahead and indent in for into this curly bracket. Any code that we put inside of these
- 1:33:46two curly brackets is going to be considered part of the method. So any code that we put
- 1:33:51in here will be run when we call our method. So I can essentially write all the code in
- 1:33:56here to perform the task that the method is designed to carry out. Remember, this method
- 1:34:01is just going to say hi to the user. So I can basically just print something onto the
- 1:34:04screen, I'll say console dot write line. And why don't we just come over here and say,
- 1:34:10Hello, user, our method down here has one line of code and methods can have as many
- 1:34:16lines of code as you want. I mean, you can have, I think most methods have anywhere from
- 1:34:19five to 10 lines of code. But you can have more or less than that, it doesn't really
- 1:34:23matter. In my case, though, I just have a very simple method. So it's going to be one
- 1:34:26line of code. Alright, so now what I want to do is go ahead and run my program, and
- 1:34:32we'll see what happens. So I'm just going to click this start button. And you'll see
- 1:34:37over here, nothing got printed out. So even though we created our new method, and we put
- 1:34:42a line of code inside this method, it didn't execute this line of code. In other words,
- 1:34:47this line of code never got executed by C sharp. Here's the reason. When we create a
- 1:34:53method, this code isn't going to get run automatically, only the code that's inside of this main method
- 1:35:00is going to get run when we run our program. So because this code wasn't inside of this
- 1:35:05main method, it didn't get run. Now, if I want to execute all of the code inside of
- 1:35:11this, say hi function, I can actually do something called the calling it. So I can call this
- 1:35:17method. Basically, what that means is C sharp is going to go over to the method execute
- 1:35:22all the code inside of it. So the way I can call the method is just by typing on its name,
- 1:35:26I can just say, say hi. And I can make an open and close parentheses. And then now we
- 1:35:31should be able to execute this code. So when I run my program, now, you'll see we're printing
- 1:35:36out Hello, user, so the code inside the method got executed. Now, essentially, what's happening
- 1:35:42is, whenever C sharp sees this line of code, what it does is, it jumps over, it goes over
- 1:35:49to this, say hi method. And it's going to go through and execute all of the code inside
- 1:35:54of there, then when it's done executing the code inside of here, it's going to come back
- 1:35:58and it's going to move on to the next line of code in the main method. That's essentially
- 1:36:03what's happening. So this is extremely useful. And there's going to be a lot of circumstances
- 1:36:08where you're going to want to have methods that are doing different things. And you're
- 1:36:11going to want to have all of that code inside of its own single container. But this is really
- 1:36:17just scratching the surface of what we can do with these methods. And I want to show
- 1:36:20you guys another thing we can do, we can actually give these methods information inside of this
- 1:36:25Say hi function, I could actually specify that this Say hi function should take in some
- 1:36:30information. And this is what we would call parameters or arguments. And a parameter is
- 1:36:35basically just a value that gets passed into a method. So it's the value that we would
- 1:36:40give to this method. And then it can use that value to perform its task better or do something.
- 1:36:46And the way that we can specify a parameter is just by coming in here and enter the open
- 1:36:49and close parentheses. And I can just basically specify what type of information I want to
- 1:36:54accept. So let's say that instead of saying hello, user, we wanted this method to say
- 1:36:59hi to a particular person, well, over here, I could specify string name. And now when
- 1:37:06I write out this line, string name, I'm basically saying that the Say hi method needs to take
- 1:37:12as a parameter as an argument, one value, which is going to be a name, then what I can
- 1:37:17do is I can come down here and I can print out Hello, name, you'll see we're getting
- 1:37:22an error up here, when we're calling this method. That's because I didn't include a
- 1:37:28name when I called it. So now whenever I call this method, I have to pass it a string. So
- 1:37:34I'm going to pass it a string Mike. And essentially, what's going to happen is this string Mike,
- 1:37:39that I passed, this method is going to get stored inside of this string variable inside
- 1:37:45of this name variable. And then I'm just going to print it out. So now, we should be printing
- 1:37:49out Hello, Mike. And you can see we get that right there. So that's one cool way that we
- 1:37:57can run these methods. And the other cool thing about methods is that I can call this
- 1:38:02code as many times as I want. And so I can call it again and again, and each time pass
- 1:38:06in a different name. So we'll pass in Mike, john, and Tom. And now each time we call this,
- 1:38:13the method is going to do something slightly different. So it's going to say hi to someone
- 1:38:18different. So now it's saying, whoops. Now it's saying hello, Mike. Hello, john. Hello,
- 1:38:24Tom. So really, what we're doing is we're able to write this code in one place, and
- 1:38:30then we're able to reuse it continually inside of our program. And that's another reason
- 1:38:35why methods are awesome. In addition to just passing in one parameter, we could also specify
- 1:38:40that this should take two parameters. So instead of just a name, why don't we also pass in
- 1:38:44an integer age. And then down here, we can add the agent so I can say hello, name you
- 1:38:51are and then we'll just print out the age. So now when I call these methods up here,
- 1:38:58you'll see we're getting more errors, because I have to give these values. So I can say
- 1:39:02like 3356 and 12. And now when I run my program, it's going to be able to handle that. So this
- 1:39:12method is now printing out both the name and the age that we passed in. And really, you
- 1:39:17can pass anything you want into these methods, you can pass strings and doubles. You can
- 1:39:22even pass things like arrays, it doesn't really matter. You know, any value that you want
- 1:39:27to work with, you can pass into here, as long as you can specify it over there. So that's
- 1:39:32kind of the basics of working with methods. Now I will say you'll hear most of the time
- 1:39:37people are going to call these methods you'll also hear people refer to these as functions.
- 1:39:42methods and functions are essentially the same thing. There is a small difference, just
- 1:39:46in the definition, but generally you can use the word method and the word function interchangeably.
- 1:39:52And you'll hear a lot of people call these functions. You also hear a lot of people call
- 1:39:56them methods, but generally in C sharp like the official term would be A method. So that's
- 1:40:01really why methods are useful, you can kind of see, you know, we were essentially able
- 1:40:05to reuse all the code that we wrote down here. So I can reuse it and give it different information
- 1:40:11and it'll do different things. Now, in the next video, I'm going to talk to you guys
- 1:40:16about this value over here, which is going to be the return value. So in addition to
- 1:40:20passing information into methods, methods can also pass information back to the callers,
- 1:40:26and we're going to talk about that in the next tutorial. In this tutorial, I'm going
- 1:40:33to show you guys how to use return statements in C sharp methods. So in C sharp a method
- 1:40:39is basically just a little block of code, which is designed to perform a specific task.
- 1:40:46And in the last tutorial, I showed you guys how we could create a very simple method that
- 1:40:49would say hi to the user. In this tutorial, I want to show you guys how we can use another
- 1:40:54feature of C sharp methods, which is the return keyword. And basically, what we can do is
- 1:41:00inside of these methods, we can return values back to the callers. So I'm gonna show you
- 1:41:06guys how this works. And we'll kind of get an idea of what this is and what it does.
- 1:41:10So down here in my program, you'll notice I have my main method over here. And I'm actually
- 1:41:14going to create another method. So I'm going to come down here below the main method. And
- 1:41:18I want to create a method which is going to cube a number. So when you cube a number,
- 1:41:23you basically take it to the third power. So two raised to the third power is the same
- 1:41:27as two times two times two, right, so we would say that this is two cubed. So what I want
- 1:41:33to do is actually create a method that will take as a parameter, so take as an argument,
- 1:41:39one number, and then it'll cube it. And it'll actually return that number back to the caller.
- 1:41:44So this is going to be kind of cool. So remember, whenever we're creating a method, we need
- 1:41:49to say, static. And like I said, I'm going to explain what static is and what it does
- 1:41:55later in the tutorial. But for now, just know that whenever you're creating a method, here
- 1:42:00next to this main method, because main is such a special method, I'm also going to use
- 1:42:05this static keyword for the new method that I create. Then the next thing I need to tell
- 1:42:10C sharp is going to be the return type. And in the last tutorial, we use this void return
- 1:42:15type, which basically meant we weren't going to return any information. But now what I
- 1:42:20want to do is I want to actually specify a particular data type that we're going to return.
- 1:42:26So in our case, why don't we just say we're going to return an integer. So now what I
- 1:42:31want to do is give this method a name. So I'm just going to say cube, so it's going
- 1:42:35to be called cube. Because it's cubing a number, I want to make an open and close parentheses
- 1:42:39and an open and close curly bracket. inside of this parentheses, what I want to do is
- 1:42:45specify a parameter. So this cube method is going to take one parameter, and it's going
- 1:42:50to be an integer, and we're just going to call it num. So num is going to be the number
- 1:42:55that they pass in. And eventually, what we're going to want to do is we're going to want
- 1:42:59to return to them the value of num cubed. So what I can do down here is I can make an
- 1:43:05integer. And I'm just going to say and we'll call it result, and I want to set result equal
- 1:43:10to the cube value of num. In order to get that we can just say num times num times num.
- 1:43:17And so now this variable result is going to have num times num times num stored in it.
- 1:43:24In other words, it's going to have the num cubed stored inside of it. Now what I want
- 1:43:29to do is use a special keyword in C sharp called return. And whenever we put this keyword
- 1:43:35inside of a method, this is basically going to tell the method that we want to return
- 1:43:39an piece of information back to the caller. And also this return keyword is going to break
- 1:43:46us out of the method. So over here, what I can do is I can just say return results. So
- 1:43:51I'm going to go ahead and return this result variable that we just created back to the
- 1:43:56caller. Now let's go up here in our main method and actually call this method. So what I want
- 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
- 1:44:05let's pass in five. So now I'm going to go ahead and run my program and we'll see what
- 1:44:11happens. So you'll see my program ran. But I mean, nothing really happened, right? There
- 1:44:15was no output there really, you know, there was no errors, but really nothing happened.
- 1:44:19Here's the thing, though, if I was to come over here and say console dot write line,
- 1:44:24if I printed out this method, in other words, if I printed out calling this method, you'll
- 1:44:31see we're actually going to print out result we're actually going to print out the value
- 1:44:35of cubing these numbers. So over here, I just said console dot write line. And inside of
- 1:44:40here, I'm printing out cube five. Now when I run my program, you'll see we're printing
- 1:44:46out 125. Basically what happened was when I use this return keyword down here, that
- 1:44:53returned the value back over to this method. So then when I called the method it was actually
- 1:45:00Given the value of 125, and that is extremely useful. And that's basically a way that we
- 1:45:07can get a piece of information back from a method. Just to further illustrate this point,
- 1:45:11I want to create another example. So down here, I'm going to make an integer. And I'm
- 1:45:16just going to call it cube number, and I'm going to set it equal to cube five. So I'm
- 1:45:22actually setting this variable equal to cube five. Now I can print out cube number, and
- 1:45:32you'll see we're going to get that value 125 back. And so that's essentially what we're
- 1:45:37able to do, we're able to return a value back from this method, in addition to returning
- 1:45:43an integer, we could return anything. So if I wanted, I could return a string, or a double.
- 1:45:50Or I could even return like an array of doubles, right, something like that. So you can really
- 1:45:55return any value that you want back from these methods. But returning information can be
- 1:46:00extremely useful. And you're definitely going to want to use it inside of your C sharp methods.
- 1:46:09In this tutorial, I'm going to talk to you guys about if statements in C sharp, an if
- 1:46:13statement is basically a special structure in our program, which will allow our program
- 1:46:19to respond to different situations. So essentially, with if statements, our programs can become
- 1:46:25smarter. And I want to show you guys some examples of if statements, and we're going
- 1:46:28to kind of look at how we can create and use if statements in our programs. Now over here,
- 1:46:33I have this little text file open. And one of the cool things about if statements is
- 1:46:37that as human beings, we encounter them every single day. So over here, I have some examples
- 1:46:42of if statements that you might see on a daily basis. So up here, it says, I wake up, if
- 1:46:47I'm hungry, I eat breakfast. So this is an example of an if statement, essentially, an
- 1:46:52if statement has two parts, it has a condition, which is right here. And then it has an action
- 1:46:58that gets executed when the condition is true. So our condition up here is if I'm hungry.
- 1:47:05Now this condition is either true or false. It can only have two possible values true
- 1:47:10or false. You're either hungry, or you're not. If you are hungry. In other words, if
- 1:47:16this condition is true up here, then you're going to eat breakfast. But if you're not,
- 1:47:20if this condition is false, then you're just going to move on. And that is the essence
- 1:47:24of an if statement, we check a condition. If the condition is true, then we'll perform
- 1:47:28an action. If the condition is false, then we'll move on and we won't perform that action.
- 1:47:35So just like you're able to make a decision about whether or not you're hungry, and whether
- 1:47:39or not you should eat breakfast, we can use these in our programs to do the same thing
- 1:47:43to make decisions. And there's another one down here it says I look at my phone, if it's
- 1:47:47about to die, I charge it. So again, this is a condition, it's either true or false.
- 1:47:53If it's true, if your phone is about to die, then you charge it. Otherwise, we're just
- 1:47:57going to move on. Here's a little bit of a more complex if statement, it says I leave
- 1:48:02my house. If it's cloudy, I bring an umbrella. So again, here's our condition. If it's true,
- 1:48:07then we're going to bring the umbrella. If it's false, though, now we're going to do
- 1:48:11something else. So up here, when the condition was false, we just moved on. But now, if this
- 1:48:17condition is false, otherwise, we're going to bring our sunglasses, so we're going to
- 1:48:21do something else. And these are just some basic if statements. But I just kind of wanted
- 1:48:25to give you guys an idea of what these were before we implement them in our programs.
- 1:48:29So we can do essentially the same thing. But we can allow our programs to make decisions.
- 1:48:34So when certain conditions are true, we can do certain things. And if they're false, then
- 1:48:40we can do other things. So let's go back over to our program dot C sharp. And you'll see
- 1:48:45down here I'm just in my main method, and I'm going to show you guys how we can use
- 1:48:49some if statements. So before we do any f statements, I'm actually going to create a
- 1:48:54variable. So this is going to be a boolean variable, and I'm just going to call it is
- 1:48:58male. And this variable is going to tell us whether or not somebody is a male. So I'm
- 1:49:04a male. So I'm going to set this equal to true. So now we have this Boolean variable
- 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.
- 1:49:15And we can actually create an if statement and based off of this is male variable. So
- 1:49:20let's say that we have this variable in our program, we're working with it. And we want
- 1:49:24to do something when the person is male. So inside of our program when the person's male,
- 1:49:29when this variable is true, we want to be able to do something, I can use an if statement
- 1:49:34to check that. I can come down here and say if and I'm gonna make an open and close parentheses,
- 1:49:39and an open and closed curly bracket. And this is sort of the general outline of an
- 1:49:44if statement in our program. Now, inside of this parentheses, I want to specify a condition.
- 1:49:49So just like over in that text file, we had some conditions like if I'm hungry, or if
- 1:49:54my phone's about to die. Inside of these parentheses, we want to specify a condition. Now this condition
- 1:50:00It needs to be a true or a false value, right? If I'm hungry, that was our condition before
- 1:50:06that's true or false, right, it can only have one of two values. If my phone's about to
- 1:50:11die is true or false, it can only have one of two values. So inside of this parentheses,
- 1:50:16we need to put a condition which is basically just going to be a true or a false value.
- 1:50:21So in my case, I want to check to see if the person is male. So I can say, if is male,
- 1:50:28and if this variable is true, then we're going to be able to execute the code that we put
- 1:50:32down here. So why don't we just write out console dot write line, and we'll just write
- 1:50:40out you are male. So we're writing out console dot write line, if the variable is male is
- 1:50:46equal to true. So let's go ahead and run this program. And you'll see over here, it's printing
- 1:50:50out you are male. So our program was smart enough to determine whether or not someone
- 1:50:55was male. If I was to set this equal to false, and now I went to run my program, when I run
- 1:51:01the program, you'll notice that nothing is gonna get printed out because the variable
- 1:51:06was false. So this line of code down here only gets executed when this condition up
- 1:51:12here in these parentheses, is true. So that's how we can use a basic if statement. But let's
- 1:51:18say that when is male was false, we wanted to do something else. So let's say even if
- 1:51:24this variable was false, we wanted to try and do something else. So I can actually use
- 1:51:28another keyword in C sharp, which is called else. So I can say else and open and close
- 1:51:33curly bracket. And then down here, we can write out another line of code. So I'm actually
- 1:51:38just going to copy this guy and down here, and we're going to write out a you are not
- 1:51:45male. So when they are male, we'll tell them that and when they're not mail when his mail
- 1:51:50is false, we'll also tell them that so his mail is false right now. And it should tell
- 1:51:55us that so it should tell us you are not male. Essentially, what we've done now is we've
- 1:52:01created a program that is able to respond to the value of a variable. So this program
- 1:52:07can respond to this variable, right? If his mail is true, then we're going to print out
- 1:52:14your mail. If his mail is false, then we're going to print out you are not mail. So our
- 1:52:19program is smart, our program can adapt and adjust what it's doing, depending on the value
- 1:52:26of a variable. And therein lies the essence of if statements, if statements allow our
- 1:52:32programs to make decisions and to do different things, when different conditions are met.
- 1:52:38But this is a very simple if statement. In fact, this is probably like the simplest if
- 1:52:41else statement that we can write, I'm going to make this a little bit more complex. And
- 1:52:45we're gonna go ahead and we're going to create another variable. So I'm gonna come up here
- 1:52:50and make another Boolean, and I'm just going to call it is tall. And this is tall variable,
- 1:52:55again, is going to store a true or false value. So why don't we set both of these equal to
- 1:52:59true, just to start off, so this is tall variable is going to tell us whether or not someone
- 1:53:04is tall. So this will determine if they're like tall or short. And down here, let's say
- 1:53:11that instead of just checking to see if they are male, we also wanted to check to see if
- 1:53:16they're tall. So I can say if is male. And I can actually use another operator in C sharp,
- 1:53:24and it's called the AND operator. So I can say, two ampersands, like this. And this stands
- 1:53:29for and, and basically, when we use this and operator, it allows me to write two conditions
- 1:53:35in the same condition block. So it allows me to not only check to see if they're male,
- 1:53:40but I could also check to see if they're tall. So now this is saying if is male, and is tall.
- 1:53:49So now, not only does izmail have to be true, but is tall also has to be true for this whole
- 1:53:55thing to be true. So if one of these guys was false, then this whole thing would be
- 1:54:00false. So now I'm down here, I could say you are a tall male, right, and then down here,
- 1:54:06instead of saying you are not male, I can instead say you are either not male or not
- 1:54:13tall or both, right, because if this code gets executed, it means one or both of the
- 1:54:20conditions up here is false. So let me demonstrate this. You'll see over here, both of these
- 1:54:25are set to true. And when I run my program, it's going to be able to respond to that.
- 1:54:29So you'll see it says you are a tall male, because both of those were true. But let's
- 1:54:34say I set one of these to false, so I set his male equal to false. Well now because
- 1:54:40one of these guys is false, this whole thing is going to be false. So we're going to print
- 1:54:45this out down here. So now when I run the program, you will see it's printing out you
- 1:54:49are either not male or not tall or both. So it's able to respond to those two variables
- 1:54:55in our program. In addition to using this and operator we can also use another operator
- 1:55:00Which is called for, and or does the same thing as and except it's a little bit different
- 1:55:04so or will allow us to check two conditions. But this time, only one of these conditions
- 1:55:09has to be true for this whole thing to be true. So we would read this as if is male,
- 1:55:15or is tall. So they either have to be male, or they have to be tall, or they can be both.
- 1:55:22And then we'll be able to execute this code right here. But if both of these guys are
- 1:55:27false, then we're going to go down here and execute this code. So let me just demonstrate
- 1:55:31this really quick. And I'm not going to bother changing the text here, you guys kind of hopefully
- 1:55:35get what's going on. So even though false and true, are both up here. So even though
- 1:55:39one of these guys is false, we're still going to be able to print out You are a tall male.
- 1:55:46But if I was to make both of these false, now we're going to end up printing out you
- 1:55:51are either not male or not tall, or both, which again, I realize isn't the right text
- 1:55:56for that, but you can just kind of understand what's going on. So it says you're either
- 1:55:59not male or not tall, or both. So that's the difference between and and or when we use.
- 1:56:05And here, both of these conditions have to be true. So both of these variables would
- 1:56:09have to be true for this whole thing to be true. And for us to execute that code, when
- 1:56:14we use or only one of them has to be true. So this could be false, or this could be false,
- 1:56:21but one of them has to be true for the whole thing to be true. And that's really the difference.
- 1:56:24So I'm going to turn this back to and and I want to show you guys some other stuff we
- 1:56:28can do. So right now we're able to check the condition where they're male, and they're
- 1:56:33tall. But there's also a couple of different scenarios. In addition to that, for example,
- 1:56:38let's say that they're male, but they're not tall. Well, what if we wanted to do something
- 1:56:42when they were male, but they were not tall. So if they're like a short male, I can actually
- 1:56:47use another keyword or another reserved word in C sharp, which is called else F. And I
- 1:56:53can just type out alsef, like this, I can make an open and close parentheses and an
- 1:56:56open and close curly bracket. And then I'm just going to make a new line. So essentially,
- 1:57:01what I did here was I added in elsif. And then you can see this else basically goes
- 1:57:06after it. When I use LS if it allows me to check another condition. So if this condition
- 1:57:11up here is false, I can actually come down here and check another condition. So we wanted
- 1:57:18to check to see if they were male, and they were not tall. So I could say else if is male.
- 1:57:24And now I need to figure out how I can specify and not tall. So remember, this variable is
- 1:57:29tall will tell us if they're tall or not. If I wanted to find out if they're not tall,
- 1:57:34I could just say exclamation point is tall. And this is going to read as not tall, not
- 1:57:42is tall. This exclamation point here is what's known as the negation operator. And this will
- 1:57:47basically negate the condition. So over here I have is tall. If this was true, then when
- 1:57:55I put this exclamation point here, it's going to make it false. If this was false, when
- 1:57:59I put this exclamation point here, it's going to make it true. So this whole thing is going
- 1:58:04to be true when the person is not tall. And so now this is essentially checking to see
- 1:58:10if they're male, and they're not tall. So down here, we can write another message. So
- 1:58:16I can basically say like you are a short male, because they're male, but they're not tall.
- 1:58:23And finally, I'm going to make one more elsif. So I'm just going to come down here. And there's
- 1:58:27one more thing that we want to check. So I also want to check to see if they're tall,
- 1:58:32but they're not male. Right. So over here, we're checking if they're male, and they're
- 1:58:36tall. Over here, we're checking to see if they're male, and they're not tall, down here,
- 1:58:41I want to check to see if they're not male. And they are tall. So this is saying else
- 1:58:48if not male, and is tall. So down here, we can put another little message. And I'm just
- 1:58:54going to basically say like you are not a male, but you are tall. So this is essentially
- 1:59:03just saying exactly what it is you're not male, but you are tall. And then down here
- 1:59:08for this else. Remember, this code now is only going to get executed when none of these
- 1:59:13conditions up here are true. So now down here, we can just say you are not male and not tall.
- 1:59:19Alright, so let me walk you guys through this one more time. Up here, I'm saying if is male
- 1:59:27and is tall. So this condition is only going to be true when both of these are true. And
- 1:59:33then if this is true, we're going to come down here and execute this code, we're going
- 1:59:36to say you are a tall male. But if this is false, we're going to come down here to this
- 1:59:41elsif. And we're going to check another condition. So only when this is false, or we're going
- 1:59:46to check this condition down here. And we're basically going to check to see if they're
- 1:59:50male, and they're not tall, and I'm using this negation operator here. And if that's
- 1:59:55true, then we're going to tell them that we're going to print that out. Then we're going
- 1:59:58to check to see if they're not missing. And they're tall. And we'll print out a message
- 2:00:02for that. And then finally, we'll say you are not male and not tall. So let's go ahead
- 2:00:08and run our program. And you'll see both of these guys, we're going to be set to true
- 2:00:12initially. And we'll try all these different combinations. So both of them are set to true,
- 2:00:16I'm going to run the program, and it says you are a tall male. So our program was smart
- 2:00:21enough to be able to respond to the value of these two variables. So now let's set his
- 2:00:26male equal to false. So I'm going to come down here, and we'll just say false. And now,
- 2:00:31essentially, this is going to be able to respond, so I can run my program now. And you'll see
- 2:00:36it's printing out, you are not male, but you are tall. So it was able to respond to that
- 2:00:41different value. Now we'll set his male back equal to true, and we'll set his tall equal
- 2:00:46to false. And now we should get a appropriate response. So it says you are a short male.
- 2:00:53And finally, if these are both equal to false, then again, the program's going to be able
- 2:00:57to respond to that. So it's gonna say, you are not male, and not tall, awesome. So essentially,
- 2:01:04what we did with these if statements was we allowed our program to respond to different
- 2:01:10values, right, our program was able to make decisions based off the information that it
- 2:01:16was given. And this is really just scratching the surface of if statements. You'll see over
- 2:01:20here, we're using these Boolean variables, right. But there's actually another way that
- 2:01:24we can specify these conditions, and it's using something called comparisons. And in
- 2:01:29the next tutorial, I'm going to talk to you guys all about comparisons about how to use
- 2:01:32them. And it's basically just another way that these if statements can come in handy.
- 2:01:41In this tutorial, I'm going to talk to you guys some more about if statements, more specifically,
- 2:01:45we're gonna be talking about comparisons. So inside of an if statement, we can actually
- 2:01:49compare two values. And depending on you know, the result of the comparison, we can do different
- 2:01:55things, this is going to be kind of cool. And essentially, what I want to do in this
- 2:01:59tutorial is I want to build a method. And this method is basically going to be a max
- 2:02:06method. So the max method, if you're not familiar, is basically a method where we can give it
- 2:02:11two numbers, and it'll tell us which one is bigger. So we're going to write a method that
- 2:02:14takes two parameters as input, number one, and number two, and then we'll basically be
- 2:02:20able to return whichever is bigger. So I'm going to go down here, and I'm going to go
- 2:02:25below this main method, and we're going to create another method. So I'm just going to
- 2:02:29say static. And I'm going to want this to return an integer. So why don't we just say
- 2:02:33that it's going to return an integer, and I'm going to call it get max. And then I'm
- 2:02:38going to make an open and close parentheses and open and close curly bracket. So get Max
- 2:02:43is going to take one or two parameters. So we're going to take in an integer num one,
- 2:02:48and then we're also going to take another integer, num, two, basically, the job of this
- 2:02:53method is to figure out which of these two numbers is bigger, and then return it to the
- 2:02:58caller. So let's go ahead and set this up, I'm actually going to create a variable. So
- 2:03:05I'm just going to make an integer I'll call it result. And I'm not actually going to give
- 2:03:09this a value yet. So we're going to end up giving this value inside of the method. And
- 2:03:13then down here, I'm going to return results, I'm just going to return this variable. And
- 2:03:20inside of this method, now our goal is to figure out which number is bigger, right.
- 2:03:25So inside of this method, we don't know if num one is bigger or num two is bigger, we
- 2:03:29have no idea. But all we know is that we have two numbers here that we can work with. So
- 2:03:34what we want to do is use an if statement and an if statement will allow us to do something
- 2:03:40when a certain condition is true, and do something else when a certain condition is false. So
- 2:03:45I'm going to make an if statement, I'm just going to say if and I'm gonna make an open
- 2:03:48and close parentheses and open and close curly bracket. So this is like the sort of outline
- 2:03:53the structure for our if statement. Now inside of these parentheses, I need to put a condition
- 2:04:00and remember a condition is a true or false value. In the previous tutorial, we were using
- 2:04:07Booleans as our condition. So I had a Boolean like is male and another one is tall. And
- 2:04:12we use that as the conditions for if statements. But here's the thing, we don't necessarily
- 2:04:16have any Booleans to work with, right? All we can work with here are these two integers.
- 2:04:21So what I can do is I can actually use something called a comparison. So I can compare the
- 2:04:25two numbers. So I can actually say like if num one is greater than num, two. And essentially
- 2:04:34what I'm doing here is I'm comparing the two values. But remember, inside of these parentheses,
- 2:04:39we need a condition we need a true or a false value. But actually the cool thing is that
- 2:04:45when when we do this comparison, this is going to resolve down to a true or false value.
- 2:04:50Right? So num one is either going to be greater than num two or it's not. There's only two
- 2:04:56possible scenarios with this comparison. Right either it's true And num one is greater than
- 2:05:01num two or it's false. And num two is greater than or equal to num one, right. So really,
- 2:05:08this gets resolved down to a true or a false value anyway, so these comparisons can be
- 2:05:15super useful inside of our parentheses. Now inside of this actual f block, if num one
- 2:05:21is indeed greater than num two, then we can just set result equal to num one. And then
- 2:05:27I'm going to make an else statement down here. And I'll just say else. And I'm just going
- 2:05:33to say, result is equal to num two. So if num, one's greater than num, two, then we'll
- 2:05:41make result num one. Otherwise, we'll make it num two. And then down here, we're just
- 2:05:45returning result. So this method is basically done, we have everything that we need, we
- 2:05:51should be able to figure out which number is bigger. So let's go back up here to our
- 2:05:55main method. And I'm going to call this and I'm actually going to print out the answer.
- 2:05:59So I'm just going to say, console dot write line. And then inside of these parentheses,
- 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.
- 2:06:12So ideally, we're going to get a 10 back, because 10 is the bigger number. So let's
- 2:06:17go ahead and run this program. And you'll see we're getting that 10 back, no problem.
- 2:06:23So let's now see if it's gonna work when the first numbers the bigger one, and hopefully
- 2:06:28it does, so we should get 20, which we do. So this method is working out. And you can
- 2:06:32see, we're able to use this comparison in order to figure it out. So that's awesome.
- 2:06:39And this is pretty cool. But I'm going to get rid of this, let's up the ante a little
- 2:06:43bit, let's make this a little bit more difficult. Let's say instead of two numbers, we wanted
- 2:06:48to be able to compare the three numbers. So we wanted this max function to be able to
- 2:06:53tell us the maximum of three separate numbers. Well, in here, I can just specify another
- 2:06:59parameter. So I can say num three. And now down here, not only do we have to figure out
- 2:07:04two of them, we have to figure out three of them. So of these three numbers, which one
- 2:07:08is bigger. Now we're going to want to use another if statement. So I'm going to go ahead
- 2:07:13and this time, I'll make that same if statement, essentially. And now we need a condition.
- 2:07:18So not only do we need to check to see if num one is greater than num two, but we also
- 2:07:23have to check to see if it's greater than num three. So I'm going to say if num one
- 2:07:28is greater than or equal to num, two. And so I'm using this and operator num one is
- 2:07:35greater than or equal to num three, then down here, we can set result equal to num one because
- 2:07:44if no one's bigger than num, two, and it's also bigger than num three, then we know it's
- 2:07:48the biggest one, right, so we can just go ahead and return that. But let's say that
- 2:07:53this isn't the case, I want to do another one. So I'm going to say else if and we'll
- 2:07:57make a another condition down here. So I'm basically just going to say else if num two
- 2:08:03is greater than or equal to num one, and number two is greater than or equal to num three.
- 2:08:10Right. So if num two is bigger than num one, and it's bigger than num three, then num twos,
- 2:08:16the biggest. So we'll say result is equal to number two. Finally, then we can just say
- 2:08:21ELSE, AND down here we can specify the else. So I'm just going to go ahead and set result
- 2:08:29equal to num three. Because if num one's not the biggest, and remember, we check that up
- 2:08:34here and num twos, not the biggest, which we checked down here, then num three is going
- 2:08:40to be the biggest. So let's go ahead and test this out. So now we need to include another
- 2:08:45number. So I'm going to put like a four in here. So we should get back this 20 because
- 2:08:49that's the biggest number. And you can see we do, let's try it with the other places.
- 2:08:54So let's make this last one here, we'll make this a 40. So this is going to be the biggest.
- 2:08:59And we'll see if this works, which it does. So this get max method seems to be working
- 2:09:04out pretty well. Now down here, you'll notice when I compare to the two numbers, I'm using
- 2:09:10these like greater than or equal to zero before I just use a greater than these are called
- 2:09:14comparison operators. And these comparison operators can be used to compare different
- 2:09:19values. So I can use greater than, less than, greater than or equal to, less than or equal
- 2:09:25to, I can also use equal to, and this double Eagles is special because it's going to allow
- 2:09:30us to compare these two numbers. So this isn't used for assignment like down here we use
- 2:09:35a single equals for assignment so I assigned the value of num one to result up here I'm
- 2:09:41comparing the value so I'm basically checking to see if the value of num one is equivalent
- 2:09:47or equal to the value of num two. And then we can also use one more which is going to
- 2:09:53be not equals and this is going to be the opposite. So this will say num one not equal
- 2:09:57to num two. So that's essentially how we can use comparisons. And you can also, in addition
- 2:10:03to numbers, you could also compare like characters. So I can compare like two characters, you
- 2:10:09know, basically like that. And that's going to work out just fine. So these comparisons
- 2:10:13are extremely useful. And I would say that a lot of times when you're using if statements,
- 2:10:17you're going to be using comparisons just like that. In this tutorial, I'm going to
- 2:10:25show you guys how to build a four function calculator in C sharp. Now, if you've been
- 2:10:30following along with this course, you'll know that in the beginning of the course, we actually
- 2:10:33created a very simple calculator. And essentially, the calculator would let the user enter in
- 2:10:38two numbers. And it would take those numbers and sort of add them together and then print
- 2:10:43out the answer, right, it's very simple. In this tutorial, we're going to take that calculator
- 2:10:46to the next level. So we're going to actually allow the user to enter in two numbers. And
- 2:10:51they're also going to be able to specify an operator. So they'll be able to specify whether
- 2:10:55whether or not they want to add the numbers, subtract the numbers, multiply or divide the
- 2:11:00numbers, so it's gonna be pretty cool. And let's go ahead and get into it. So the first
- 2:11:04thing we want to do is we want to be able to get all the information from the user.
- 2:11:09So I'm going to basically just going to be printing out prompts, and then we'll be storing
- 2:11:13the values that the user enters inside of variables. So the first thing I'll do is just
- 2:11:18print out a prompt, basically prompting them to enter in some information, and actually,
- 2:11:23I'm just gonna use right instead of write line. So I'll say, enter a number. And they'll
- 2:11:30enter their first number. And then what we want to do is want to get that number and
- 2:11:34store it inside of a variable. So let's store this inside of a double. So I'm just going
- 2:11:38to call this num one. And I'm just going to set it equal to console dot read line. But
- 2:11:47remember, whenever we use console dot read line, this is going to give us a string back.
- 2:11:51So if I want to store this value inside of a double over here, I'm actually going to
- 2:11:56have to use something called convert. So I'm going to say convert.to, double, and I'm going
- 2:12:03to place console dot read line inside of this methods parentheses. So I'm going to pass
- 2:12:07this in as an argument as a parameter to the Convert to double method. That's essentially
- 2:12:14what's happening there. And I'm basically just going to copy this line. And I'm going
- 2:12:17to paste it down here, because we're going to do the same exact thing for the second
- 2:12:22number. So I'm going to say num two is equal to convert to double console dot read line.
- 2:12:27So we're getting the first number and we're getting the second number and we're storing
- 2:12:31in them in the num, one variable in the num two variable. Now one more thing I want to
- 2:12:35do is I want to get the operator. So I'm basically gonna say I'm just gonna say console dot write.
- 2:12:41And over here, I'll just say, operator. So this is going to be like a plus sign a minus
- 2:12:49sign, division, sign, multiplication, sign, etc. And then down here, we can just make
- 2:12:54this a string. So I'm going to say string op for operator is going to be equal to console
- 2:13:00dot read line. So basically, what I'm doing is I'm getting the first number, I'm converting
- 2:13:07it to a double, I'm getting the operator plus minus division, whatever, I'm getting the
- 2:13:12second number. So now at this point in my program, I should have all the information
- 2:13:16that I need. So the question becomes how can we perform the operation? In other words,
- 2:13:22we have the two numbers, great, but how can we figure out what the user entered in? Right?
- 2:13:27How do we figure out what Opie is telling us to do, we can use an if statement. So I'm
- 2:13:32going to use an if statement, I'm just gonna say if let me make an open and close parentheses
- 2:13:36and open and closed, curly bracket. And down here, we want to check a condition. So essentially,
- 2:13:42what I want to do is I want to check to see if the operator is equal to a plus sign. So
- 2:13:46I can say if op is equal to plus sign, right? If the operator is equal to a plus sign, then
- 2:13:55we know for a fact that they're trying to add the numbers. And I'm actually just going
- 2:13:59to print that out down here. So I can say console, dot write, and we're basically just
- 2:14:04print out num, one plus num two. And actually, I'm going to do write line here. Alright,
- 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
- 2:14:14not a plus sign? Well, I want to check another condition here. So I'm going to say, else
- 2:14:19if and we'll check another one. We basically want to check to see if the operator is equal
- 2:14:25to a minus sign. If the operator is equal to a minus sign, now we're going to do the
- 2:14:30same thing we did up here. It's just that we're going to subtract the number. So I'm
- 2:14:35going to say num one minus num two. We can do the same thing for multiplication and division.
- 2:14:40So I'm going to go ahead and copy this code and I'm just going to paste it down here.
- 2:14:44And down here. Instead of checking minus we're just going to check division and then if it
- 2:14:49is division, we're going to divide the numbers. And then finally, I'm going to check for multiplication.
- 2:14:54So I'm going to say Asterix for multiplication and then num one times Number two. So we're
- 2:15:02able to cover all four operations, right addition, subtraction, division and multiplication.
- 2:15:07Each time, whatever the user enters, we're going to perform the correct operation, right?
- 2:15:11This if statements, pretty simple. But let's say that the user enters in an operator that's
- 2:15:16not equal to any of these guys up here, right? What if they enter in like a random character
- 2:15:22like a string of text? Well, we want to be able to throw an error message. So what I
- 2:15:26can do is I can come down here and just say else. And then inside of this else block,
- 2:15:31I'm just gonna say, console dot write line. And I'm just going to print out like, invalid.
- 2:15:40operator. So this will basically tell them like, Hey, you had an eight, you had an invalid
- 2:15:44operator, like, That's not good. So now we have our whole program. And we use our little
- 2:15:49if else structure here to figure out what was stored inside of that op variable. So
- 2:15:56we figured out what the user wanted us to do. Let's go ahead and run this program. And
- 2:16:00we'll see how we did. Over here it says, enter a number I'm gonna enter 10 and enter an operator,
- 2:16:05why don't we do addition, and enter another number? Why don't we do like 5.5. So now when
- 2:16:11I hit enter that if statement is going to execute, and it's going to see that plus sign,
- 2:16:15and it's going to be able to add the numbers. So you see here, we get 15.5. So we successfully
- 2:16:21added the numbers together. Let's try a different one, I want to do multiplication. So let's
- 2:16:27do 60 times 4.23. And we get 253. Yeah, that seems about right. Alright, so let's try one
- 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
- 2:16:41going to enter in an invalid operator. So I'm going to enter in like an H for the operator,
- 2:16:46and then we'll enter in a five. And now this should tell us invalid operator because the
- 2:16:52program didn't recognize that operator. In other words, it wasn't able to handle that.
- 2:16:57So we threw our little error message. And that's essentially all we need to do to build
- 2:17:01this four function calculator. The magic is in the if statement. So we can use the if
- 2:17:06statement to figure out what calculation or what operation the user wanted to do. And
- 2:17:12we can just keep using all these different assets in order to figure that out. So this
- 2:17:16is extremely useful. And hopefully, you can find a use for something like this and your
- 2:17:20programs. In this tutorial, I want to show you guys how to use switch statements in C
- 2:17:29sharp. Now a switch statement is basically just a special type of if statement where
- 2:17:34we can check a bunch of different conditions really easily. And there's, I'll be really
- 2:17:39clear in the example that I showed you guys. So let's go ahead and get started. Down here,
- 2:17:43my program, I just have a very simple program set up. And in this tutorial, I want to create
- 2:17:48a method. So we're actually going to create a method, which is going to convert numbers
- 2:17:53into days of the week. So if I pass this method is zero, it would give me back Sunday, because
- 2:18:00Sunday is the first day of the week. If I pass this a one, it would give me back a Monday,
- 2:18:07right? If I pass this a two, it would give me back Tuesday, etc, right. So if I, you
- 2:18:15know, it's basically going to map numbers to the specific day of the week that we're
- 2:18:20going to give it. And this is going to be a pretty cool method. So I'm going to come
- 2:18:24down here and we're going to go ahead and write the method, I'm just going to say static.
- 2:18:28And this is going to return a string. So it's going to return the name of the day of the
- 2:18:32week. And I'm just going to call this get day. And this method is going to take one
- 2:18:39parameter and it's going to be the day num. So I'm just going to call this day num. So
- 2:18:45de nom is going to be like 0123456, etc, right? It's going to be a number. And we're going
- 2:18:50to take that number and we're going to convert it into a day of the week, we're going to
- 2:18:55convert it into a string, which would be the day of the week. So I'm going to create a
- 2:18:59variable over here, that's going to be a string variable. And I'm just going to call it day
- 2:19:04name. And I'm not going to give it a value and then down here, eventually Our goal is
- 2:19:09going to be able going to be to return day name. So inside of our little method over
- 2:19:15here inside of our method block, our goal is to populate this day name variable with
- 2:19:21a value right with the correct value, that's going to correspond to the day num that gets
- 2:19:26passed in. Cool. So I'm going to show you guys how we can use a special structure in
- 2:19:32C sharp to do this, and it's called a switch statement. So I'm going to set up my switch
- 2:19:36statement, I'm just going to say switch, I'm going to make an open and closed parentheses
- 2:19:39and open and close curly bracket. And down here, we're going to be able to start writing
- 2:19:44our switch statement. Now, the switch, David needs one piece of information inside of this
- 2:19:49parentheses, and it basically needs a value. So the whole point of the switch statement
- 2:19:55is we're going to pass it a value and then we're going to check to see if that value
- 2:19:59is equal to a bunch of different stuff. And depending on what it's equal to, we're going
- 2:20:02to do different things. So this will be clear in a second. In here, I'm going to pass in
- 2:20:06de nom. Because if daenam is zero, I want to do something if daenam is one, I want to
- 2:20:11do something else. If daenam is two, I want to do something else. In other words, my goal
- 2:20:15is to figure out what the de nom is. And depending on what it is, I'm going to do something in
- 2:20:20other words, I'm going to give this day name variable a different value. Now down here,
- 2:20:24in the switch statement, we can make what are called cases. So I can come down here
- 2:20:28and I can just type out case. And over here, I can just type in a value. So I can say zero
- 2:20:34here. And I'm going to make a colon down here, I'm basically just going to say day name is
- 2:20:41equal to Sunday. And I'm also going to say, break down here. So let's talk about what
- 2:20:48this is saying. Essentially, this line, this little block of code is saying that in the
- 2:20:52case, that day, num is equal to zero, I want to do this. So in the case that de nom has
- 2:21:00a value of zero, we're going to do whatever I specify down here, essentially, what I'm
- 2:21:04doing is I'm saying day name is going to be equal to Sunday. And I'm also saying break,
- 2:21:09and we're going to talk about break in one second. So now what I can do is I can create
- 2:21:14one of these cases for each of the possible numbers, right. So now instead of just making
- 2:21:19one for zero, I'll make one for one. And the case that day num is one, I'm going to say
- 2:21:27day name is going to be equal to Monday, right? And then I get I'm going to say break. And
- 2:21:31we'll do this one more time, I'm going to do it for two. So in the case that day, num
- 2:21:39is equal to two. Now I'm going to say day name is equal to Tuesday. And you'll notice
- 2:21:45here I'm using this break statement. And this is a special reserved word in C sharp, which
- 2:21:51basically says that we want to break out of the structure that we're currently in. So
- 2:21:56we're inside of this switch statement. And essentially, what happens is with this switch
- 2:22:01statement, it's going to look through all of these different cases. So it's going to
- 2:22:05say okay, is it zero? Is it one is it two, but let's say that de nom is zero, right?
- 2:22:10So it says his case, zero, yes, we're going to execute this, we're going to assign a value
- 2:22:15of Sunday to daenam. If I don't put this break statement in here, then C sharp is going to
- 2:22:21keep checking all of these cases after that, so it's not going to stop, it's going to keep
- 2:22:26checking. So if you have a scenario where you want to be able to keep checking the other
- 2:22:31cases, even after one of them was true, then you can get rid of the break statement. But
- 2:22:35I think in most cases, you'll probably just want this break statement in there. Alright,
- 2:22:39so I've made one of these for Sunday, Monday and Tuesday, I'm going to go off and make
- 2:22:43some for the rest of the days of the week, and then we'll come back and check it out.
- 2:22:47Alright, so I went ahead and made one of these for every day of the week. You can see we
- 2:22:51have Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday. And this is a really
- 2:22:56easy way to check all these different cases, right, I can just say case six day name, Saturday
- 2:23:02excetera. So we basically have our case statement set up, we have our switch statement set up.
- 2:23:08But here's the problem, let's say that the user passes a value into this method, that's
- 2:23:13not zero through six. Right? So this case, this switch statement can only handle values,
- 2:23:19zero through six. But what happens if someone passed like a 102? there? Well, I can actually
- 2:23:24account for that. So down here, I can create something called a default, I can basically
- 2:23:29just type out default, I can type a colon. And now down here, I can specify what I want
- 2:23:34to do if none of these cases are true. So I could come down here and I can basically
- 2:23:39just say, de name, and we'll just set this equal to invalid number, or why don't we say
- 2:23:45invalid day number. And this will basically act as like a error message. And then down
- 2:23:51here, we're also going to need to say break. So this is essentially how we can account
- 2:23:56for any values that aren't included up here as cases. So our function or our methods seems
- 2:24:03to be done at this point. So let's go ahead and try it out. So remember, get day is going
- 2:24:08to take one value, which is going to be a num. And let's go up here to our main method,
- 2:24:13and we'll try this out. So I'm actually just going to print out whatever we get. So I'm
- 2:24:16gonna say console dot write line. And I'm going to go ahead and print out get day and
- 2:24:23let's pass this as zero. So I'm going to go ahead and say get day zero. So hopefully,
- 2:24:28we should get Sunday back because Sunday is mapped to zero. And you'll see over here that
- 2:24:35we get Sunday back so let's try another one. Why don't we try four? And I think four is
- 2:24:41going to be Thursday. Yeah. So Thursday. And let's try one more. So I'm going to try an
- 2:24:47invalid number. So let's put ad in here. So if I say get day ad now we should get that
- 2:24:53error message which tells us that it's an invalid day number. Yeah. So over here we're
- 2:24:57getting that invalid day number So that's basically what we can do is switch statements.
- 2:25:02Now let me just point this out everything that you can do with switch statements you
- 2:25:06could actually do with if statements. The reason that we have switch statements though
- 2:25:09is because they make it a lot easier to do essentially what we're doing over here, you
- 2:25:14could do with if statements, but it would be a lot messier than this, and it would be
- 2:25:17a lot harder to manage. With switch statements, it's very clear, right, it's very clear what's
- 2:25:21going on and what values are getting mapped to what code so switch statements are extremely
- 2:25:27useful, and you should definitely consider using them in your C sharp programs. In this
- 2:25:35tutorial, I'm going to show you guys how to use while loops in C sharp. Now while loop
- 2:25:40is basically a programming structure that allows us to loop over a specific block of
- 2:25:45code, while a certain condition is true. Basically, using while loops, we're able to keep doing
- 2:25:51something repeatedly while a condition is true. And this can come in handy in a bunch
- 2:25:56of situations. And loops are extremely useful in a programming language like C sharp. So
- 2:26:01let's get started. I'm going to show you guys how they work and how to use them. Basically,
- 2:26:05just to give you a general introduction into while loops, down here in my main method,
- 2:26:10I'm actually going to create one thing, and this isn't going to be the while loop, I'm
- 2:26:14just going to create a variable, it's going to be an int, and I'm just going to call it
- 2:26:18index, and I'm gonna set it equal to one. And we're gonna end up using this index variable
- 2:26:22inside of our loop. So now I want to create a while loop. So I'm just gonna say while
- 2:26:26and I'm gonna make an open and close parentheses and an open and close curly bracket. Now here's
- 2:26:31how while loops work up here in these parentheses, I'm going to specify a condition. So if you're
- 2:26:37familiar with if statements, you'll know that with an if statement, we have a condition,
- 2:26:40right, we check a condition. And if that condition is true, then we execute the code inside of
- 2:26:45the if statement. Pretty simple. It's the same concept with this while loop up here
- 2:26:49in this while loop, I'm going to specify a condition. And if this condition is true,
- 2:26:54I'm going to be able to execute all of the code that goes down here in these curly brackets.
- 2:26:59The only thing is with a while loop, not only are we going to execute at one time, but we're
- 2:27:04going to keep continuously executing it until the condition up here in these parentheses
- 2:27:09is false. So we're basically going to check this condition. If it's true, we'll execute
- 2:27:14all the code inside of these curly brackets. And then we'll come up with check the condition.
- 2:27:18Again, if it's still true, we'll come down and execute it again. So you're continuously
- 2:27:22looping through this code. So it's essentially an if statement, but you keep going until
- 2:27:27the condition is false. And over here, in these parentheses, we need to specify our
- 2:27:31condition. So I'm going to make a condition I'm just going to say while index is less
- 2:27:36than or equal to five. So as long as this index variable is less than or equal to five,
- 2:27:43I'm going to keep executing all the code down here. And then down here, I'm basically just
- 2:27:47going to write a few lines of code. So I'm going to say console dot write line, and we're
- 2:27:53going to print out the value of index. And then down here, I'm going to increment the
- 2:27:58value of index. So I'm going to say index plus plus. And remember, this plus plus basically
- 2:28:03just adds one onto the index. So essentially, what's happening is, every time we go through
- 2:28:09this loop, I'm going to print out the value of index, then I'm going to increment index,
- 2:28:14and then I'm going to go back here and check the condition again. So this is our basic
- 2:28:19while loop, I'm going to run the while loop, we'll see what happens. And then I'm going
- 2:28:22to explain exactly what happens. So we can get a better idea of how this works. Let's
- 2:28:27go ahead and run the program. And you'll see over here, I'm printing out one through five.
- 2:28:35And maybe you expected that maybe you didn't. But this is what we got. And I'm going to
- 2:28:39go ahead and explain exactly how this happened. Step by step. So the first thing we did up
- 2:28:44here was we created this integer index and we set it equal to one right now, then we
- 2:28:51created our while loop. So when C sharp goes to execute our program, it's first going to
- 2:28:56create this variable index, and it's going to store one inside of there, then the next
- 2:29:00thing it's going to do is check this condition. So the first thing before it executes any
- 2:29:05of the code inside of the loop before it does anything else, it's going to check this condition.
- 2:29:09So it's going to check to see if index is less than or equal to five, since index was
- 2:29:13equal to one initially, that's going to be true. So then C sharp is going to go down,
- 2:29:18it's going to execute this line. So it's going to print out index onto the console, as you
- 2:29:21can see over here, then it's going to increment index. So index is now going to be equal to
- 2:29:26two, then C sharp is going to go all the way back up to this condition and check it again.
- 2:29:32So before C sharp is going to execute that code a second time, it needs to check the
- 2:29:36condition again. So now index is equal to two and two is less than or equal to five.
- 2:29:41So C sharp is going to come down here, it's going to print out two, as you can see right
- 2:29:44there, and it's going to increment index, then it goes all the way back up and checks
- 2:29:49the condition again, right? So every single time we execute all that code before it's
- 2:29:55going to execute it again it goes back up and checks the condition. All right, eventually
- 2:30:00We're going to get to a point where index is going to be equal to five, and we printed
- 2:30:03out five over here, then we're going to increment it to six, C sharp is going to come up here,
- 2:30:09and it's going to check is six less than or equal to five, and then the condition is going
- 2:30:13to be false. So we're going to break out of the while loop just like that. And that's
- 2:30:18essentially how we can use these wild loops. So it's very simple, right, you specify a
- 2:30:23condition, and you're going to keep looping through the loop as long as that condition
- 2:30:27is true, easy enough. Now, I do want to warn you guys of something, there is a possibility
- 2:30:32of something called an infinite loop. And an infinite loop is a situation where this
- 2:30:37condition up here never becomes false. So for example, if I was to get rid of this index
- 2:30:42plus plus, now this condition up here, index less than or equal to five, this is always
- 2:30:48going to be true. Because we're never modifying the value of index when we were incrementing.
- 2:30:53It, eventually we would increment it enough to get to five. But since we're not incrementing
- 2:30:59it anymore, we're just going to loop infinitely. So let me show you guys what happens when
- 2:31:02I do this. When I run my program, you'll see over here that I'm just constantly printing
- 2:31:07out one, I mean, this, it's literally just printing out one as fast as it can. And it's
- 2:31:12going to do this forever. If I let it it'll do this infinitely, because it's never told
- 2:31:18that it needs to stop. In other words, this condition up here is never false. And so that's
- 2:31:24why we have an infinite loop. And that's probably going to happen to you, as you play around
- 2:31:27with loops. Everyone's had an infinite loop before. And in most cases, you're going to
- 2:31:32want to avoid them, although there are some special cases where you're actually going
- 2:31:35to want infinite loops. So that's sort of the basics of while loops. It's very simple,
- 2:31:40right? We have a condition up here, as long as that condition is true, we're gonna keep
- 2:31:43executing the code down here. And every single time we go through the loop, we check that
- 2:31:47condition. Again, I want to show you guys one more thing. And there's actually another
- 2:31:51type of loop that we can use, which is similar to a while loop, it's called a do while loop.
- 2:31:56And in order for me to kind of illustrate what this is doing, I want to show you guys
- 2:32:00an example. So if I came up here and set index equal to six, let's talk about what's going
- 2:32:05to happen. So if index is equal to six, right, the first thing that my program does with
- 2:32:11this while loop is it checks this condition. So the first thing before it executes any
- 2:32:15of this code before it's allowed to even touch that code, it checks this condition. So since
- 2:32:20index is equal to six, and this condition is getting checked, before we execute the
- 2:32:24code, this code is never going to execute, because six is not less than or equal to five,
- 2:32:30this condition is false. So we're never going to execute this code. So if I run my program,
- 2:32:35you guys will see that nothing gets printed out, right, we don't execute any of the code
- 2:32:40inside that while loop. But there's actually another type of loop, which is essentially
- 2:32:44the opposite of a while loop. And I'm going to show you guys that. So I'm going to basically
- 2:32:48going to take this while loop declaration up here, I'm going to throw it down here.
- 2:32:52And I'm going to make sure I put a semicolon after it. And then up here, instead of saying
- 2:32:56while I'm going to say do. And so now we have another type of loop, which is a do while
- 2:33:03loop. And the difference between a do while loop and a while loop is that a do while loop
- 2:33:07will execute the code inside of the loop before it checks the condition. So now even though
- 2:33:13index is equal to six, we're still going to be able to execute this loop one time before
- 2:33:19we look at the condition, right, so it's basically going to execute all this code, then it's
- 2:33:24going to look at the condition, it's going to figure out that it's false. And it's going
- 2:33:27to go ahead and break out of this. So I'm going to show you guys how we can do this.
- 2:33:32And I'm just gonna run my program. And you'll see that even though this condition was false,
- 2:33:37even though six is not less than or equal to five, we were still able to print it out
- 2:33:42one time, because in a do while loop, you execute the code inside the loop first, and
- 2:33:48then you check the condition. So that's basically another type of loop that we can use. And
- 2:33:54do wire loops definitely come in handy. And there's a lot of situations where you want
- 2:33:57to use them. Although I would say that by a huge majority, while loops are more useful.
- 2:34:04But do while loops are also awesome. So that's kind of the basics of while loops, and do
- 2:34:07while loops. And this is really just the basics of looping. In general, there's a lot of situations
- 2:34:12when we're programming, we're going to want to continually do the same thing until a certain
- 2:34:16condition is false. And that kind of gives you an overview of how it works. In this tutorial,
- 2:34:25I'm going to show you guys how to build a guessing game in C sharp. Now this is going
- 2:34:30to be a very basic guessing game. Essentially, we're going to define a secret word. And then
- 2:34:35the user is going to have to try and guess that word. And we're going to keep asking
- 2:34:39the user to try and guess it until they get it right. So this is going to be kind of cool.
- 2:34:43And I'm going to show you guys how we can use some of the stuff that we've learned in
- 2:34:46this course. So far things like loops, and if statements and variables, we're going to
- 2:34:51use all of them together to create this cool game. So let's start creating the game. Remember,
- 2:34:56this is going to be a guessing game. So the first thing we want to do is Come up with
- 2:35:00a word that the user is going to have to guess. So I'm going to create a variable and it's
- 2:35:04just going to be a string. And I'm just going to call it secret word. And the secret word,
- 2:35:09why don't we just give it a value, we'll set it equal to draft. So draft is going to be
- 2:35:14the secret word. And the user's goal for our game is going to be to try and guess that
- 2:35:19secret word. Now I want to create one more variable. And we're actually going to call
- 2:35:23this one guests. So this is going to be another string. And I'm just going to say it's called
- 2:35:29guests. And why don't we just set it equal to this empty string over here, so I'm not
- 2:35:32really giving this a value yet. Alright, so now we have our secret word. And we also have
- 2:35:38our guests. And what I want to do is, I want to be able to keep prompting the user to guess
- 2:35:44the word until they get it correctly. So anytime in C sharp that we want to keep doing something
- 2:35:50repeatedly until a certain condition is false, we're going to use a while loop. So I'm going
- 2:35:56to go ahead and create a while loop. And I'm just gonna make an open and close parentheses
- 2:36:00open and close curly bracket. And there's really two things to this while loop, the
- 2:36:04first thing is going to be a condition. And the second thing is going to be the code that
- 2:36:07we want to continually execute. So let's think this through what condition do we want to
- 2:36:12put up here? Well, basically, we want to keep looping, while the guests is not equal to
- 2:36:18the secret word. In other words, as long as the guest the user guesses isn't equal to
- 2:36:24what the secret word is, we want to keep looping and keep asking them to guess. So down here,
- 2:36:30what I want to do is keep prompting them to guess. So I'm basically just gonna say, console
- 2:36:35dot write, and I'll write out to the screen a prompt, I'll say, enter guests. And then
- 2:36:41now that we've prompted them to enter the guest, what I want to do is I want to take
- 2:36:45whatever they entered, store it inside of this guests variable. And that way, we can
- 2:36:50keep checking it every time we go through this a loop. So down here, I'm basically just
- 2:36:54going to say guess, is equal to console dot read line. So I'm going to go ahead and read
- 2:37:02the line of text that the user enters, basically just read whatever word they entered in and
- 2:37:07store it inside of this guest variable. So essentially, what's happening now is, every
- 2:37:12time we go through the loop, we're prompting the user to guess the word, and we're storing
- 2:37:16whatever they guess, inside of this guest variable, then we're going to go up here,
- 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
- 2:37:24secret word, then we're going to go through and ask them again. But if it is equal to
- 2:37:29the secret word, then we're gonna go ahead and break out of the loop. And then down here,
- 2:37:33we can just print out a success message. So I can just print out console dot write, and
- 2:37:38I'll just print out you win. So this is our program, it's actually a pretty easy program.
- 2:37:44And I do just want to point out that in addition to using a while loop, we also could have
- 2:37:49used a do while loop in order to write this program. So if you want a little challenge,
- 2:37:54and you want to try and challenge yourself, go ahead and try to write this with a do while
- 2:37:59loop instead of a while loop. And if you need to figure out what do while loops are just
- 2:38:02check out the previous tutorial, I explained what they were. But for our case, I'm just
- 2:38:06doing it with a simple while loop just to keep it as straightforward as possible. But
- 2:38:09like I said, if you want a challenge, try it with a do while loop. So let's see if we
- 2:38:13can run our program. Believe it or not, this is actually all the code that we need for
- 2:38:17this guessing game. So I'm going to go ahead and run the program. Now you'll see over here,
- 2:38:23it says enter guests, so I'm just going to type in some random text. And you'll notice
- 2:38:27that because this text didn't match the secret word draft, it's asking me to enter again.
- 2:38:35So I'm going to keep entering and I can keep entering essentially infinitely many times.
- 2:38:40And it's going to keep prompting me to guess until I get the word. But if I was to type
- 2:38:44in the secret word, so if I was to type in draft here, now when I hit Enter, draft is
- 2:38:50equal to the secret word. So we're going to break out of that while loop. And we're going
- 2:38:54to get this you win message. So that's essentially all our game did. It allowed us to keep guessing
- 2:39:01the word until we got it? Right. So this is a pretty cool game. And you know, you guys
- 2:39:05can see down here, it's actually pretty easy for us to implement it. There's only you know,
- 2:39:09maybe like seven or eight lines of code. But I think there's a little problem with this
- 2:39:13game. And the problem is that the user gets unlimited guesses, right? So the user can
- 2:39:18just keep guessing infinitely. I mean, technically, technically, they could type in, you know,
- 2:39:22every possible combination of letters until they got the secret word, right. And that's
- 2:39:26not a very good game, I think we'll make this game better would be imposing a guess limit.
- 2:39:32So we could say like, the user can only guess three times or they can only guess four times.
- 2:39:35And if they don't get it in four guesses, then they lose the game. I think that would
- 2:39:40be a little bit better game. So why don't we try to implement that. Let's see if we
- 2:39:44can come up with some code that will allow us to program that game. Well, I think I know
- 2:39:50what we need to do. So the first thing we want to do is we need some way to keep track
- 2:39:54of how many times the user guesses right. If we're going to impose a guess limit on
- 2:39:59this Game, we need to be able to keep track of how many times they've guessed. So that
- 2:40:04was a good place to start, why don't we just start with that I'm going to create an integer.
- 2:40:07And I'm just going to call it guest count. And we'll set it equal to zero initially,
- 2:40:11right? Because Initially, the user hasn't guessed at all. And what we want to do is
- 2:40:15we want to increment this variable every time the user guesses. So let's go down here. And
- 2:40:22you'll notice this is the code down here, that allows the user to guess right, it's
- 2:40:26this code, which is essentially prompting the user to guess and then storing their guests.
- 2:40:32So in addition to prompting them and storing the guests, we can also increment the guest
- 2:40:36count. So I can come down here and I can say, guest count plus, plus, right, just like that.
- 2:40:42So I'm incrementing, the guest count every single time they guess, awesome. But we're
- 2:40:48still gonna need some more information. So another piece of information we're going to
- 2:40:52need is the guest limit. So we need to essentially tell our program how many times the user can
- 2:40:58guess. So I'm just going to make another and I'll just call this guest limit. And over
- 2:41:03here, we can define how many times we want the user to be able to guess, well, why don't
- 2:41:07we say three guesses? I think three guesses is enough, three strikes in your out. And
- 2:41:12so now we're basically limiting the user to three guesses. I also want to create one more
- 2:41:19variable, and this is going to be a Boolean. And this is going to be called out of guesses.
- 2:41:25And I'm going to set this equal to false. And this Boolean is basically going to tell
- 2:41:29us whether or not the user is out of guesses. So if the user still has guesses left, this
- 2:41:36is going to be equal to false. But if the user is out of guesses, then this is going
- 2:41:40to be equal to true. So we're going to set it to false initially. And then down here,
- 2:41:44we need to use these two new variables inside of this while loop. So the first thing I think
- 2:41:50we should do is we should only allow the user to guess when they still have guesses left,
- 2:41:56right? In other words, I only want the user to be able to guess the word if they haven't
- 2:42:01already guessed three times, right? If they haven't guessed the guessed limit. So what
- 2:42:05we can do is we can actually create an if statement. So I can say if and down here,
- 2:42:09I'm going to make open and close curly brackets. And inside of these parentheses, I need to
- 2:42:14put a condition. So let's think about this. What do we want to check? Well, I want to
- 2:42:18check to see if the user is out of guesses, right? So I can check to see if the guest
- 2:42:24count is less than the guest limit. And if this is true, in other words, if the guest
- 2:42:30count is less than the guest limit, then the user still has a guest left, so they can go
- 2:42:34ahead and guests. So I'm going to take all this code for guessing. And I'm going to put
- 2:42:38it inside of these curly brackets. Right. So once again, we're checking this condition,
- 2:42:44if the guest count is less than the guest limit, then we're gonna go ahead and print
- 2:42:49out all of this stuff. So we're going to allow them to guess the word, and then we'll increment
- 2:42:53the guest count. But if this isn't the case, in other words, if guest count isn't less
- 2:42:58than the guest limit, that means that they've already reached their guests limit, right?
- 2:43:03That means they don't have any guesses left. So I can say else down here. And if they run
- 2:43:09out of guesses, what I want to do is I want to modify this out of the guesses variable.
- 2:43:14So when they run out of guesses, in other words, when this condition is false, I want
- 2:43:18to say out of guesses is equal to true. So I'm going to say out of guesses is equal to
- 2:43:24true. So basically, what we're doing down here is we're saying, hey, they're out of
- 2:43:29guesses. And this is actually what we call like a flag. So you'll hear people sometimes
- 2:43:34call these like Boolean flags. And this is basically just a value that's giving us a
- 2:43:39piece of information, right? This is telling us that the users out of guesses. So there's
- 2:43:43one more thing we have to do in this while loop, you'll notice that this while loop is
- 2:43:47going to keep executing as long as the guest is not equal to the secret word. But now since
- 2:43:53we're adding in the gas limit, there's actually another situation where we're going to want
- 2:43:57to stop looping. And that situation is when the user is out of guesses. So I want to modify
- 2:44:03this, I want to say that we want to keep guessing Wow, they haven't guessed the secret word.
- 2:44:08And they're not out of guesses, right? Because if they haven't guessed the secret word, then
- 2:44:14we want to keep asking them. But if they ran out of guesses, then we want to break out
- 2:44:18of the loop, right? So I'm going to keep looping while the guest is not equal to the secret
- 2:44:23word, and the user is not out of guesses. So now we have a while loop set up, which
- 2:44:29is going to allow us to specify a guest limit. So there's one more thing we have to do, which
- 2:44:35is down here. Now down here. We're basically just saying you win, right? So whenever we
- 2:44:40break out of the loop, we're telling them that they won. Here's the problem though.
- 2:44:44So there's two situations now where they can break out of the loop when the guest is equal
- 2:44:48to the secret word in other words when they guess correctly, but there's also another
- 2:44:52situation which is when they're run out of guesses, right? And so down here, we're not
- 2:44:57going to be super sure which situation occurred. So what we can do is we can use another if
- 2:45:02statement. And we can check. So I can basically say if, and I can check to see if they're
- 2:45:07out of guesses. So I can say if out of guesses. If this is true, then I want to print out
- 2:45:13a lose message. So I'm basically just gonna say you lose. But otherwise, if it's not true,
- 2:45:20that means that they guessed the word correctly. So we can go ahead and put this win message
- 2:45:26down here. Alright, so that is essentially everything we need to do to specify the guest
- 2:45:32limit, I know this was a lot of code, why don't we run our program, we'll play the game,
- 2:45:36make sure we got everything right, and then we can come back, and we can kind of go over
- 2:45:40what we did again. So I'm gonna run the program. It says enter guests. So I'm going to go ahead
- 2:45:45and lose the game. So remember, we have three guesses, so I can just type in some random
- 2:45:49guests. Another one. Now we're on our third and final guests. If I don't get the word,
- 2:45:54this time, we're going to lose, so I'm not going to get it. And I'm gonna click Enter,
- 2:45:59and it says, you lose. So it was able to figure out that we didn't guess the secret word and
- 2:46:04three tries, and it told us that we lost the game. Cool. So now let's see if we can win
- 2:46:08the game. So I'm going to go ahead and run this program, and it's gonna say enter guests.
- 2:46:12So let's enter two wrong guesses. Now we're on our last try. Let's say I have a stroke
- 2:46:18of genius. And I type in the right word. And it says, You win. So the program is now smart
- 2:46:24enough to play this game with a guest limit. Let's go over one more time, what we did,
- 2:46:30we created a bunch of variables up here, the secret word is pretty obvious. The guest is
- 2:46:34just storing what they guessed, the guest count is keeping track of how many times the
- 2:46:39user's guest right? So every time we run the code to allow them to guess the word, we're
- 2:46:44going to increment it, the guest limit is just telling us how many times they can guess
- 2:46:49in this out of guesses variable is basically going to tell us whether or not the user has
- 2:46:54run out of guesses. And then down here we have our loop. And this loop is basically
- 2:46:58just going to keep going and keep asking the user to guess the word. And it's going to
- 2:47:02keep doing that as long as the guest is not equal to the secret word. And as long as the
- 2:47:08user is not out of guesses. So if either of those conditions is false, we're gonna break
- 2:47:12out of the loop, the first thing we do in the loop is we basically use this if statement,
- 2:47:16and we say if guest count is less than guest limit. And so if the guest count is less than
- 2:47:21the guest limit, in other words, if they haven't guessed three times in our case, then we're
- 2:47:27going to prompt them for another guest. But if they have guest three times, then we're
- 2:47:32going to set this out of guesses variable equal to true, and that's going to trigger
- 2:47:36some breaking out of the loop. So if we do break out of the loop, there's two situations,
- 2:47:40right? The first situation is that we won the game. And we guessed the secret word correctly.
- 2:47:44But the other is that we ran out of guesses. So down here, I'm basically checking that
- 2:47:48I'm checking if we're out of guesses. If we are, that means we lost otherwise, that means
- 2:47:54we won. And so that's the basics of building this program. And I do just want to say, this
- 2:48:00while loop up here, we use a simple while loop to do this. But you could also run this
- 2:48:06program using a do while loop. And so if you want to challenge if you really understand
- 2:48:11what's going on here, and you you know, you're pretty solid on what's going on, a good challenge
- 2:48:15would be to try and build this program with a do while loop instead of a while loop. I
- 2:48:20mean, it's gonna be pretty similar, a lot of the code can stay the same. But there are
- 2:48:23going to be some key differences that you're going to have to try to account for. So if
- 2:48:27you're looking for a challenge, try to build this with a do while loop. But otherwise,
- 2:48:31that's sort of how you build a guessing game. And you know, In this tutorial, we were able
- 2:48:34to integrate a lot of the stuff that we've learned up to this point in the course like
- 2:48:38variables, we use all different types of variables, we use the loops, we also use if statements.
- 2:48:42So hopefully this gives you an idea of how you could build something like this using
- 2:48:47all that stuff. In this tutorial, I'm going to show you guys how to use a for loop in
- 2:48:55C sharp. a for loop is a special type of loop, which allows us to keep track of what's called
- 2:49:01an iterating variable. And basically, this is just a variable that's going to change
- 2:49:06every time we go through our loop. So basically, it's it's kind of a situation where we have
- 2:49:11a variable and generally that variable will like keep track of how many times we've gone
- 2:49:14through the loop, or it can keep track of other things. But it's sort of a special scenario
- 2:49:19in our programs where we want to use a loop. But we want to have a variable that's going
- 2:49:23to be changing or doing something every time we go through that loop. And this is actually
- 2:49:28really easy to understand. I'm just going to show you guys real quick this example that
- 2:49:31I have down here, you'll notice that I have a simple while loop. So I said this integer
- 2:49:36I over here I said equal to one. And I'm looping while i is less than or equal to five, right?
- 2:49:42This is a very simple while loop and every time we go through the loop, I'm basically
- 2:49:45saying console dot write line I'm printing out I and then I basically saying i plus plus
- 2:49:51so a very simple loop. And but you'll notice here when I run my program, I'm going to go
- 2:49:56ahead and run it right now that I basically printing out this variable i every time I
- 2:50:02go through the loop, and you'll notice that the first time we go through the loop is equal
- 2:50:07to one, the second time we go through the loop is equal to two, the third time it goes
- 2:50:11through the loop is equal to three, this variable i is constantly changing every time we go
- 2:50:18through the loop. And I is actually telling us what iteration of the loop we're currently
- 2:50:23on. This is pretty cool, right? This variable i is essentially keeping track of the times
- 2:50:29we've gone through the loop. And we can use this variable i to you know, print that out,
- 2:50:34and to know where we're at, in the execution of the wild. This is actually a very common
- 2:50:40situation. So a lot of times when we're writing loops in C sharp, we're going to want to have
- 2:50:46a variable just like I a variable that's going to change every time we go through the loop,
- 2:50:52right? In this case, I is changing every time it's getting incremented by one. So I is essentially
- 2:50:57telling us how many times we've gone through the loop, but you can essentially use this
- 2:51:00variable to do anything. So instead of just incrementing it by one, I can get incremented
- 2:51:05by two or I can increment incremented by another value. And basically the idea here is that
- 2:51:10we have a variable that we're using constantly as we go through this loop. And it's such
- 2:51:15a common situation to have a variable like this, that there's an entirely separate loop
- 2:51:21called a for loop, which is designed just for this situation. So down here, I'm going
- 2:51:27to go ahead and create a for loop. And I'll show you guys how this for loop is actually
- 2:51:31going to be able to do exactly what this while loop is doing but a lot cleaner and a lot
- 2:51:36simpler. To create a for loop, I'm just gonna say for I'm gonna make an open and close parentheses
- 2:51:40and open and close curly bracket. So so far, this is exactly like this Wilding, right,
- 2:51:45it's basically structured the same exact way. But you'll notice in the while loop inside
- 2:51:50of the parentheses, I only put one thing right I put the loop condition, you'll hear people
- 2:51:54call this the loop guard, right, it's basically like the condition for when we should loop
- 2:51:59inside of this for loops, parentheses, we're going to put three different things. So instead
- 2:52:04of just having one thing, we're going to put three things, the first thing we're going
- 2:52:08to put in here is a variable initialization. You'll notice up here in my while looping
- 2:52:14structure, I have this integer I and over here, I'm saying and i is equal to one. But
- 2:52:18this is actually outside of the while loop. So I had to do that separately from the actual
- 2:52:23while loop in a four loop, I can actually do this same thing inside of this parentheses.
- 2:52:28So I'm going to copy this, and I'm going to paste it over here. And you'll notice I'm
- 2:52:32going to leave this semi colon in. So the first thing I'm going to do inside this for
- 2:52:36loop parentheses, is I'm going to initialize this variable i. Now the next thing I want
- 2:52:41to do, and you'll notice I'm separating these two things with a semicolon. The next thing
- 2:52:46I want to do is write out my loop condition. So in the while loop, our loop condition is
- 2:52:51right here, you know what I mean? It's just i is less than or equal to five, whatever
- 2:52:54it's basically like telling us how long we should loop for, I can just take this, and
- 2:52:59I can put this as the second item inside of these parentheses. So now I'm able to take
- 2:53:05the variable initialisation and the loop condition. And finally, I'm going to put another semicolon
- 2:53:11here. And I'm going to include one more thing over here. And this is going to be a line
- 2:53:15of code that's going to get executed every time we go through the for loop. Now you'll
- 2:53:20notice up here in this while loop, every time we go through this loop, we're doing something
- 2:53:24special to this variable, every time we go through the while loop, we're incrementing
- 2:53:29i and this is exactly what I want to do over here in my parentheses. So the last spot inside
- 2:53:35of my parentheses over here, I'm just going to put in i plus plus. And like I said, this
- 2:53:39is just going to be a line of code that's going to get executed after every single iteration
- 2:53:45of our loop. So you'll basically see that I'm doing the same thing over here, as I am
- 2:53:49over here, right over here, I'm defining this variable, I'm initializing this variable with
- 2:53:54a value. I'm doing that down here in the parentheses. Over here, I'm specifying a looping condition.
- 2:54:00I'm doing that down here in the parentheses. And then finally down here after every iteration
- 2:54:05of the loop, I'm incrementing I. And I'm able to do that over here in this third section.
- 2:54:11So basically, the for loop is just taking this situation where we want to have a variable
- 2:54:16that's going to, you know, be used inside the loop. And it's compacting it into this
- 2:54:21one single structure. And so now I can basically copy this line of code over here, paste it
- 2:54:27into the for loop. And now these loops are identical. So these loops are actually going
- 2:54:32to be doing the same exact thing. And I'm going to prove that to you guys. So I'm going
- 2:54:36to go ahead and get rid of this while loop. And we only have the for loop now and let's
- 2:54:40run our program and see what output we get. So I'm going to run the program and you'll
- 2:54:44see we're getting the same exact output that we got before we're getting 12345 printed
- 2:54:50out. So this for loop is doing exactly what that wildly was doing. It's just, it's a lot
- 2:54:56more compact. It's a lot easier to manage and it's a lot better for us. So We walk you
- 2:55:00guys real quick through how this gets executed. Essentially, what happens is, the first thing
- 2:55:05we do is we create this variable and we give it an initial value. The second thing we do
- 2:55:09is we check this condition. If the condition is true, then we're going to execute all of
- 2:55:14the code inside of here. Once we're done executing all that code, then we're going to go up here
- 2:55:19and increment AI, then we're going to check the condition. Again, if it's true, we'll
- 2:55:23come back down here, and we'll execute this code, then again, we're coming back up here,
- 2:55:28and we're going to do that and then we check the condition again. So it's continuously
- 2:55:31looping through just like that. And this is the same as that while loop. So you could
- 2:55:36do what we're doing here with a while loop, it's just that it's a lot more convenient,
- 2:55:40and a lot easier to do it with a for loop. So that's kind of the basics of a for loop.
- 2:55:44And before I end this tutorial, I want to show you guys one cool thing we can do with
- 2:55:47these. Up here, I have this array that I created, and I just kind of commented it out. But I'm
- 2:55:54going to copy it and bring it down here. And you'll notice this is just an array of lucky
- 2:55:59numbers. So it's just an integer array, and I have these numbers in here, there's like
- 2:56:02four different numbers, I'm gonna show you guys how we can use a for loop in order to
- 2:56:07loop through and print out all of the elements inside of this array. So I just want to remind
- 2:56:11you, the way that we can access an individual element in an array is by saying the arrays
- 2:56:17name and then referring to the index of that value. So if I said lucky numbers zero, this
- 2:56:21is referring to this for if I said lucky numbers two, this is referring to this 15. Right,
- 2:56:27basically, that's how we can access an element inside of an array. For loops are really useful.
- 2:56:32Remember, we have this indexing variable. So I can use a for loop to actually loop through
- 2:56:37and print out all the elements inside of an array. So down here, so actually want to modify
- 2:56:41a couple of values, I'm going to say int i, instead of setting it equal to one, I'm going
- 2:56:45to set it equal to zero, because array indexes start at zero, so this element is element
- 2:56:51zero inside of the array, then I'm going to change this condition. So instead of saying
- 2:56:56i less than or equal to five, I want to say i less than or equal to lucky numbers dot
- 2:57:03length. So this is going to allow me to access the length of the array. So when I say lucky
- 2:57:08numbers dot length, this is going to tell me how many elements are inside of the array.
- 2:57:13Then down here, I'm going to say console dot write line, and I want to print out lucky
- 2:57:19numbers I. And the way this is going to work is the first time we go through this loop,
- 2:57:24we're going to be printing out lucky numbers zero, right, because I is initially going
- 2:57:30to be zero. So the first time we go through here, we're basically printing out lucky number
- 2:57:34zero, the second time we go through, I gets incremented. So we're going to be printing
- 2:57:38out lucky numbers one, the third time, we're going to be printing out lucky numbers two,
- 2:57:42etc. So we can use this for loop in order to loop through this array. Let me show you
- 2:57:47guys when I run this program, and actually, you know, I forgot to modify one more thing.
- 2:57:51So instead of saying less than or equal to here, I'm just gonna say less than. And basically,
- 2:57:56since we're starting at zero, I want to say i is less than lucky numbers dot link, that's
- 2:58:03because the length of the array is going to be 123456. But the last element in the array
- 2:58:10is an index position five. So if I say i is less than or equal to lucky numbers dot length,
- 2:58:15then we're going to be trying to access lucky numbers six at one point. But the last element
- 2:58:22is it lucky numbers five, so we have to say less than so I'm going to change this back
- 2:58:26to an eye. And let's go ahead and run this. And you'll see that we'll be printing out
- 2:58:30all the elements inside of that array. So we're printing out for 815 1623 42. So that's
- 2:58:37one very common use of these four loops is you can use them to loop through all the elements
- 2:58:43inside of an array. In this tutorial, I'm gonna show you guys how to build an exponent
- 2:58:51function in C sharp, an exponent function is basically a function where we can pass
- 2:58:56in two numbers, a base number and a power number. And it'll take the base number to
- 2:59:01the power number. So essentially, it's gonna like if I passed in a two and a three, it
- 2:59:07would give me two raised to the third power, right, or if I passed in a four and seven,
- 2:59:13it would give me four raise to the seventh power. That's essentially what where you want
- 2:59:18to write in this tutorial. So I'm gonna come down here below my main method, and I'm actually
- 2:59:23going to create this function. So I'm just going to say static. And we're actually going
- 2:59:28to return a integer. So why don't we just have this except integers, we'll just keep
- 2:59:32it simple. And I'm just going to call this get POW. So this will basically like get the
- 2:59:38power. And I'm going to make some open and close curly brackets. And in here we're going
- 2:59:44to accept two parameters. So the first is going to be an integer base num, and the second
- 2:59:50is going to be an integer power num. So we'll basically take the base num and raise it to
- 2:59:59the pound right So a base number two and pound on three, then we'll do two cubed, right?
- 3:00:03It kind of makes sense. So this is going to be a cool little method. And it kind of poses
- 3:00:08an interesting challenge, because we don't know what base num and power num is. Right?
- 3:00:15If we knew what power number was, for example, if we knew power number was two, then I could
- 3:00:19just say, base num times base not right, I could just square base num, and we'd be good
- 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
- 3:00:30base num by itself, in order to get the answers. So this kind of poses an interesting challenge.
- 3:00:35And I'm gonna show you guys how we can use a for loop to solve this challenge. So the
- 3:00:40first thing I want to do is just create an integer. And I'm just going to call this result.
- 3:00:45And I'm just going to set result equal to one. And then eventually, what we want to
- 3:00:49do is return result. So we want to modify result enough where it represents bass num,
- 3:00:56taken to power numb, okay. And what we need to do is we need to keep multiplying result
- 3:01:02times bass num, power num times. So let me show you guys, we can create a for loop. And
- 3:01:07I'm just going to create a simple for loop. And remember, when we're creating for loops,
- 3:01:12the first thing we want to do is create a variable. So I'm just going to say int i is
- 3:01:17equal to zero, and I'm going to start this off at zero, then what I want to do is create
- 3:01:23a looping guard. So what condition in other words, we're saying we want to keep looping
- 3:01:28as long as this condition is true. So I want to keep looping as long as i is less than
- 3:01:32a pound. And when I say this, this is basically telling C sharp that I want to keep looping
- 3:01:39power num times. So if power num is equal to three, we're going to execute this loop
- 3:01:44three times if count is equal to five, or executed five times etc. And the last thing
- 3:01:48we want to put inside of this for loop is going to be something want to do after each
- 3:01:52iteration of the loop. So I'm just going to increment i. And this is a very, very, very
- 3:01:57simple for loop. So we're looping through here, power num times. So like I said, If
- 3:02:03count is two, we'll go here through here two times. And every time I go through this loop,
- 3:02:07I basically just want to multiply result, times base nom, so I can say result is equal
- 3:02:13to result times base num, just like that. So the first time we go through this loop
- 3:02:21result is going to be equal to one, right, so the first time we go through result is
- 3:02:25going to be equal to one. So now result will be equal to one times based on right, the
- 3:02:30first time through results gonna essentially become equal to based on the second time we
- 3:02:35go through results equal to base num, right, so we're essentially multiplying base num
- 3:02:40by base num, in other words, based on m squared, and storing it inside of this result variable.
- 3:02:46And we're going to keep doing that through each iteration. So if we go through this loop
- 3:02:50three times, then result is going to have the value of base num cube, if we go through
- 3:02:55this loop, five times result will have the value of base num taken to the power of five,
- 3:03:00right? And then down here, we can just return result. So by using this for loop, we're actually
- 3:03:06able to figure out what the answer is. So up here inside my main method, let's go ahead
- 3:03:11and run this. And I'm actually going to print out the answer. So I'll just say, console
- 3:03:15dot write line, and we're going to write the result of get POW. So why don't we try to
- 3:03:23do like three squared, right, so pass in three and two. And I'm going to go ahead and run
- 3:03:29this program. So we should get the result of three squared, which is going to be nine.
- 3:03:35And you can see over here, we get nine. Now I just want to point out this, this get power
- 3:03:41method is only going to work for positive number powers. So we're not going to be able
- 3:03:46to do like three and negative two. That's not going to work down here. But we can do
- 3:03:52positive number exponent so I could say like three two, why don't we try like four and
- 3:03:58three so this is going to be four cubed. So we should get 64 and yeah, we get 64 Alright,
- 3:04:04so it looks like this methods working so once again I'll just walk you through what we did.
- 3:04:10This get power method accepted two parameters bass num, and power num. So the goal was to
- 3:04:16take bass num to power them. We created this for loop over here and essentially all I did
- 3:04:22was I said I equal to zero. And we're going to keep looping as long as i is less than
- 3:04:26power num and what that means is we're going to go through this loop power num times. So
- 3:04:31if lb M was five that means we went through this for loop five times. And every time we
- 3:04:36went through this for loop, we multiplied result which was initially equal to one, we
- 3:04:42said result is going to be equal to result times base num. So the first time through
- 3:04:46the loop, result was equal to one so we ended up getting result is equal to one times based
- 3:04:51on so we got based on second time through the loop result was equal to base num. So
- 3:04:56we said result is equal to base num times base. Not Essentially, the third time through
- 3:05:01the loop result was equal to base num squared. So essentially, we were saying result is equal
- 3:05:05to base num squared times base node based on cube. And that's essentially what we did.
- 3:05:11And that was actually enough for us to get base num taken to pound them. So it's a pretty
- 3:05:17simple method, although it is a little bit complex, because we use this for loop. So
- 3:05:23what you should do is play around with this method, and you know, write it on your own
- 3:05:27and see if you can really wrap your head around what's going on here. In this tutorial, I'm
- 3:05:35going to show you guys how to use two dimensional arrays in C sharp, a two dimensional array
- 3:05:40is basically a situation where we have an array, and every element inside of that array
- 3:05:45is itself an array. So essentially, we're building like a little matrix structure inside
- 3:05:50of our program. And I'm gonna show you guys how we can do that, it's actually pretty simple.
- 3:05:54So if we want to create a two dimensional array, we can do it just like we would a normal
- 3:05:58array. So I'm going to create an array of integers. So we'll just say ends. And now
- 3:06:02what I want to do is I want to make an open and close square bracket and inside of this
- 3:06:06open close square bracket, I'm just going to put a comma here. And basically, this is
- 3:06:09telling C sharp that we want to have a two dimensional array. And I'm going to give this
- 3:06:14a name. So I'm just going to call this number grid. And I'm going to go ahead and set this
- 3:06:19equal to something. So now what we can do is we can actually fill this array with a
- 3:06:24bunch of different elements. So I'm going to make an open and closed curly bracket,
- 3:06:29and then I'm going to make a new line and down here, we're actually going to be able
- 3:06:32to populate this array with elements. So if I was making a normal array, I would usually
- 3:06:37just put in like a bunch of numbers, so I'd say like 1234, etc. But when we're making
- 3:06:42a two dimensional array, all of the elements inside of this array need to be arrays, so
- 3:06:49themselves, they have to be array, so the first element has to be an array, and the
- 3:06:53second element would have to be an array, and the third element would have to be an
- 3:06:56array excetera. All of these elements have to be arrays, and then I could give these
- 3:07:01values. So I could say like 112. And then down here, we could say like three, four,
- 3:07:09and we could say five, six. So basically, now we have a little matrix of numbers, right?
- 3:07:15Not only does the number grid have three elements, but each of the elements inside of the number
- 3:07:19grid has two elements inside of it. And this is a two dimensional array. So let's look
- 3:07:24at how we can actually access some of these elements, what I can do is I can come down
- 3:07:28here, and I'm just gonna say, console dot write line. And if I wanted to, like for example,
- 3:07:34let's say I wanted to print out this element over here, I can just refer to the name of
- 3:07:38the array. So I can say number grid, and then I'm going to make an open and close square
- 3:07:42bracket. And in here, I'm going to put two numbers. So the first number I'm going to
- 3:07:46put is going to be the element inside of the array. So it's going to be like this is the
- 3:07:51first element, this is the second, this is the third, or this is element at index position
- 3:07:55zero, this is element at index position one index position two, etc. So I'm gonna put
- 3:08:00a zero because we're trying to access this one over here. And then I'm going to put another
- 3:08:05zero, so I'm just going to put 00, and that's referring to row zero, column zero. Now when
- 3:08:12I run this program, it's going to be able to print that out. So you'll see we're printing
- 3:08:16out one, we're printing out that element over there. Let's say that I want to access this
- 3:08:22four over here, well, this is going to be row 01. So I'm gonna put a one right here.
- 3:08:28And then it's going to be 01. So it's going to be one, one. And now when we print this
- 3:08:34out, we should get a four, which we do. So that's essentially how you access those elements
- 3:08:40inside the array. And this works exactly like it does with a normal array. So if I wanted,
- 3:08:45like I can modify one of these values, I can do everything that I would do in a normal
- 3:08:50array. And that's kind of the basics. This is a structure that you might not use all
- 3:08:53the time. But when you need it, it's extremely useful. And in addition to two dimensions,
- 3:08:58you could also have like three, or four or five dimensions, all you have to do is just
- 3:09:01put more like commas over here. There's also a situation where you might not know how many
- 3:09:06elements you want to go in there initially. And you could actually just like declare one
- 3:09:11of these, so I can say like and, and I'll make a two dimensional array, and we'll just
- 3:09:15call this like my array. And if I don't know what elements I want to put in here, initially,
- 3:09:21I can actually just say equals new int. And then over here, I need to give this some information.
- 3:09:26So I'd have to say like two, three. And this is basically telling C sharp how many rows
- 3:09:33and how many columns we want to have. So this would mean there's two elements and then each
- 3:09:38of those elements has three elements inside of it. So that's another way that you could
- 3:09:42do that. And then you could just manually like insert the elements. In this tutorial,
- 3:09:49I'm gonna show you guys how to use comments in C sharp. Now comment is basically just
- 3:09:54a special line or block of text in our program that's going to get ignored when we run program.
- 3:10:00So comments are really useful for leaving little notes or comments inside of your program.
- 3:10:05And a comment is essentially just a line of code that's meant for humans, and not meant
- 3:10:11for the computer. And these can come in handy. So I'm going to show you guys basically how
- 3:10:15these work and what we can do with them. If I wanted to create a comment, I can just come
- 3:10:19down here and type two forward slashes just like that. And now you'll see that the text
- 3:10:24in my text editor has changed color, it's now this green color. And that's basically
- 3:10:28signifying that we have a comment here. So like I said, a comment is a little line or
- 3:10:35a little block of code, that's actually going to get ignored by C sharp. So when I run my
- 3:10:40program, anything that I have inside of this comment is just gonna get ignored. The comment
- 3:10:45is for the programmer, so it's for me, or it's for another developer who comes in and
- 3:10:50is reading my file. So I could leave like a little note here, I could say like, a to
- 3:10:56do, like, you know, maybe this block of code needs to be done. Or I can even come in here
- 3:11:01and like describe a line of code. So I could describe this line of code over here, I could
- 3:11:04say like, this prints something out, etc. You could also take these comments and put
- 3:11:11them after a line of code. So I can come over here and say, two forward slashes, and you
- 3:11:16could type out, you know, a comment, whatever. And this is what we call a single line comment.
- 3:11:23So a single line comment is a comment that can only be on one line. So if I was to make
- 3:11:27a new line you see down here, this is no longer a comment. And you'll see it's actually throwing
- 3:11:32an error because the program is trying to render it as actual code. But in addition
- 3:11:37to single line comments like this, a lot of times, you're going to want to have comments
- 3:11:41that are span multiple lines, one thing you can do is just make multiple lines like that.
- 3:11:47And then you'll see a lot of developers do stuff like that, you can also use a starting
- 3:11:50and an ending tag. So you can type in a forward slash, and a star. And now everything that
- 3:11:55comes after this forward slash and a star in our file is going to be considered a comment.
- 3:12:00That is until we end it off with another star and a forward slash. So I can say star forward
- 3:12:07slash, and now you'll see only the stuff that's in between these two little tags, is going
- 3:12:13to be considered a comment. And these can go on multiple lines. So those are basically
- 3:12:17the two types of comments that we can use. And really a comment is extremely open ended.
- 3:12:22I mean, there's no wrong way to use a comment. It's really however you want to use it. And
- 3:12:27I do want to show you guys one other way these can be useful. So let's say that I'm writing
- 3:12:31code, and you know, I want to try and run my code without a certain line in it. So maybe
- 3:12:37I'm like, you know, have a program. And I think one line might be causing it to break,
- 3:12:41or I just wanted to see what it would look like without a single line of code. Well,
- 3:12:45one option would be to remove that line of code. So for example, I can take this line
- 3:12:49of code, maybe this is a line of code that I think is causing my program problems, I
- 3:12:54could delete it, and then I can go ahead and run the program, right, and then nothing gets
- 3:12:58printed out. But here's the problem, when I do that, I have to actually like physically
- 3:13:02remove the line of code. Instead, what I can do is comment it out. So I could just put
- 3:13:07a comment in front of this. And now this whole thing is basically going to get ignored by
- 3:13:12C sharp, so I can run the program. And we're still going to be running it without that
- 3:13:16line of code. But I don't have to physically delete it from the file. And that just allows
- 3:13:21me to keep track of it a little bit better. And you know, I can still have it there, it's
- 3:13:25still taking up space in the file, it's just going to get ignored. So that can be a really
- 3:13:30useful way. So really, I'd say comments, you know, are basically used for two things. The
- 3:13:34first is like writing out little messages or notes, either for yourself or for other
- 3:13:39programmers, or for commenting out lines of code. And you'll see people do both of those.
- 3:13:44And they're both really awesome. So like I said, comments are pretty much open ended,
- 3:13:47you can do whatever you want with them. But I'd say those are the two most common use
- 3:13:51cases. In this tutorial, I'm going to show you guys how to use Exception Handling in
- 3:14:00C sharp. A lot of times when your C sharp programs are running, there's gonna be certain
- 3:14:04things that can go wrong. And generally, when those situations occur, C sharp is gonna throw
- 3:14:10what's called an exception. An exception basically just tells you that something happened in
- 3:14:14your code that C sharp couldn't handle. And whenever an exception gets thrown, generally
- 3:14:19the program crashes. So there's a lot of situations where something might happen in the execution
- 3:14:25of the program, and exceptions gonna get thrown and then the program is going to terminate.
- 3:14:29And sometimes you're going to want that to happen. Sometimes when an exception gets thrown,
- 3:14:33you're going to want the program to terminate because you're going to want to know what
- 3:14:36happened. But a lot of times, especially if you're writing a program that's being designed
- 3:14:40to run for, you know, months and months at a time, you're not going to want an exception
- 3:14:45to crash the program. In other words, you're going to want the program to be able to keep
- 3:14:48going when something wrong happens and an exception gets thrown. So I'm going to show
- 3:14:54you guys the basics of working with exceptions. We're also going to talk about handling exceptions
- 3:14:58and the different scenarios. where an exception is gonna get thrown down here I have a basic
- 3:15:03program written out. And this is a very simple program, essentially all it does is it gets
- 3:15:07two numbers from the user. And it takes those numbers, and it divides them. So you see over
- 3:15:12here, I have console dot write, I'm asking them to enter a number. And then I'm taking
- 3:15:17whatever they entered, and I'm converting it into an integer. And I'm storing it inside
- 3:15:22of this integer variable. And I'm doing that for both of these numbers, then down here,
- 3:15:26I'm just writing out the value of dividing both numbers onto the screen. So there's two
- 3:15:31major things that we're doing here. The first is we're converting this string into an integer
- 3:15:37using this convert instruction. And then down here, we're dividing the two numbers, and
- 3:15:41I'm gonna show you guys how we could actually enter in information to this program that
- 3:15:46will cause exceptions. So let's go ahead and run this. And I'm just gonna click Start.
- 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
- 3:15:55going to show you guys one way that an exception could get thrown. So remember, we're dividing
- 3:16:00these two numbers, and you can't actually divide a number by zero. So just by basic
- 3:16:05math rules, if we have a whole number like this, it's impossible for me to divide it
- 3:16:10by zero. So essentially, what I'm doing here is I'm asking the computer to do something
- 3:16:15impossible, right? I'm asking the computer to do something that it's not going to be
- 3:16:19able to do, and it's not going to be able to handle when I hit Enter, and I asked the
- 3:16:23computer to do this, it's gonna throw an exception. In other words, the computer is going to be
- 3:16:27like, Hey, I can't do this, like I can't divide these two numbers. So the program is going
- 3:16:32to terminate. So you'll see when I hit Enter, that this little screen goes away, and this
- 3:16:36exception pops up. So it says exception, unhandled. System dot divide by zero exception attempted
- 3:16:43to divide by zero. It's basically telling us what went wrong, right? saying, hey, you
- 3:16:48tried to divide by zero, that's illegal, you can't do that. And we can't handle that. So
- 3:16:53basically, it's throwing this exception. So you can just come up here and click stop debugging.
- 3:16:57And that'll make that go away. I'm gonna show you guys one other way we can break this program.
- 3:17:01So if I put a number in here, like a four, that's all well and good, right? But remember,
- 3:17:07we're converting whatever is up there into a number. So if I was to type in like a G,
- 3:17:12this isn't a number, right? G is not a number, it has no numerical value. So the program
- 3:17:17isn't going to be able to convert g into an integer, even though I asked it to. So when
- 3:17:22I hit Enter, another exception is going to get thrown This time, we're getting a format
- 3:17:27exception, it says input string was not incorrect format. In other words, we asked you to enter
- 3:17:32a number and you entered in a character, the program can handle that. So it breaks. That's
- 3:17:36another example of an exception. So the question becomes, what do we do about this? Right?
- 3:17:42Our program keeps throwing these exceptions. So how can we handle that like, if the user
- 3:17:47enters in a letter, instead of a number, I don't want the program to terminate, I want
- 3:17:50to be able to do something else, like I want to be able to yell at them and be like, hey,
- 3:17:53you entered in the wrong thing, I don't want the program to terminate. Or if the user tries
- 3:17:57to divide by zero, you don't want the program to terminate, you want to tell them that they
- 3:18:01entered in an invalid number. So if we want to do that, we can actually do something called
- 3:18:06Exception Handling exception handling is basically the process of catching these exceptions.
- 3:18:11So we're basically going to tell C sharp like, hey, there's some code over here that might
- 3:18:16throw an exception. If it does, we want you to catch it. And instead of crashing, we want
- 3:18:20to be able to handle it. I'm going to show you guys how we can do that. So we want to
- 3:18:23use something called try catch blocks. So I'm just going to type out try and open and
- 3:18:28close parentheses, curly bracket. And then down here, I'm going to type out catch, and
- 3:18:36again, an open and closed curly bracket. So try catch blocks work like this, any code
- 3:18:42that we think is going to break the program, for example, the code down here, we can put
- 3:18:47inside of these try blocks. And if that code does indeed break the program, then instead
- 3:18:52of the program terminating, the program is going to go down here into this catch block.
- 3:18:56And it's going to execute all the code down there. So what I want to do is take all of
- 3:19:00my risky code, in other words, all of this stuff, and I can place it inside of these
- 3:19:04try blocks. So I'm just going to paste it inside of here. So now all of this code that
- 3:19:09potentially would break the program is inside of this try block. So what I want to show
- 3:19:13you guys, now that I have this catch block over here, I can just do like console dot
- 3:19:18write line, and over here, I'm just gonna print out error. So essentially, what's going
- 3:19:25to happen now is if any of this code causes an exception, we're going to come down here
- 3:19:29and print out there. So let me show you guys, I'm going to go ahead and run this program,
- 3:19:33and it says enter a number. So let's do the 01. Again. So now I'm going to hit zero, this
- 3:19:38should break the program. But now instead of breaking the program and causing an exception,
- 3:19:43we just print out error. So it's, it's kind of like a backup system. And what we can also
- 3:19:48do is we can get more information about the error that occurred so I can say catch, I
- 3:19:52can make an open and close parentheses and then I can say, exception, and I can just
- 3:19:57call this E. So basically saying that this catch block is going to take a an exception
- 3:20:02parameter and we're going to call it E. If I came over here now I can basically print
- 3:20:06out E. And I can say e dot message. And this is going to tell me what went wrong. So now
- 3:20:13when I run my program, and when we can do that zero division, I'll say five divided
- 3:20:18by zero, and it's going to tell me what went wrong, it's gonna tell me you attempted to
- 3:20:22divide by zero, if I did the other error, so for example, if I entered in a letter like
- 3:20:28GE, now it's going to tell me input string was not in correct format. So it's able to
- 3:20:34catch the error that happened. And by saying exception II appear in these parentheses,
- 3:20:39we can actually get the error message just like that. So this is a really great way to
- 3:20:44kind of stop our programs from terminating. But here's the thing, there's all different
- 3:20:49types of exceptions that can get thrown inside of our programs, right. And all I have down
- 3:20:53here is one catch block. So no matter what exception gets thrown, we're always executing
- 3:20:58this same code down here. But let's say there was a situation where when the zero division
- 3:21:03error occurred, you wanted to do one thing. And when the input error occurred, you wanted
- 3:21:08to do a different thing, we can't really do that, because we only have one catch block.
- 3:21:13Well, what we can do is we can actually define specific exceptions that we want to catch.
- 3:21:18So down here, instead of saying exception, I can actually specify a specific type of
- 3:21:23exception that I want this catch block to catch. So before we were getting a divide
- 3:21:29by zero exception, so I can put this in here. And now this is only going to catch a divide
- 3:21:35by zero exception. So if I was to run my program, and we did the divide by zero, I'll say five
- 3:21:41zero, you'll see it's able to catch it. But if I was to do that invalid format, but I
- 3:21:46came over here, and I said, like six, and then I said, Gee, it's not going to be able
- 3:21:50to catch that. So down here, we're getting this format exception, right. And basically,
- 3:21:56it's not being caught because we didn't specify it down here, this catch block is only going
- 3:22:02to catch divide by zero exceptions. But what we can actually do is we could make another
- 3:22:07one. So I could say, catch, and in here, I'm just gonna type in format exception. And now
- 3:22:12this is going to be able to catch any format exceptions that occur. So now I can say console
- 3:22:16dot write line. And again, we can just print out like e dot message. And so the idea is
- 3:22:22that instead of just printing out a message, you would do something different depending
- 3:22:26on the error that gets thrown. In our case, though, we're just gonna say e dot message.
- 3:22:30So now when I get that format exception, if I say like five, and then G, it's gonna say
- 3:22:36input string was not in correct format. So it's able to catch both of those. And that's
- 3:22:41essentially what you want to do. So what we were doing before is we just had, like, catch,
- 3:22:46and we were just kind of catching any exception under the sun. But now we're catching individual
- 3:22:51exceptions. And this is actually a better way to do it. Because we're able to account
- 3:22:56for each of these situations, right? When the user divides by zero, we could print out
- 3:23:00like, Hey, you divided by zero, like watch it. If they have a format exception, then
- 3:23:05we could do something else, like maybe we could prompt them to enter that input again,
- 3:23:08or, you know, we could do something else. And so being able to have these multiple catch
- 3:23:13blocks, is extremely useful. There's also one more thing I want to show you guys, which
- 3:23:17is called finally So down here, I can just type up. Finally, and I'm gonna make an open
- 3:23:22and closed curly bracket. And any code that we put inside of this, finally, block is always
- 3:23:29going to get executed no matter what, right no matter what it's going to get executed.
- 3:23:33So for example, up here, this code, like if the user enters in an invalid number, like
- 3:23:40if they enter in a letter here, instead of a number, this code won't get executed, right?
- 3:23:45Because we threw the exception, and we broke and we came down here. But sometimes in your
- 3:23:49programs, even if something throws an exception, you're always going to want to execute something,
- 3:23:53and that code can go down here in the finally block, but you don't always need finally blocked
- 3:23:59off kind of optional. So really, this is just how we can handle different errors in our
- 3:24:04program. And there's a bunch of different exceptions. If you just go online and you
- 3:24:07know, search for like C sharp exception list, you'll find a bunch of these different exceptions.
- 3:24:12And this really just makes your programs more durable. Like before, this was a very fragile
- 3:24:16program. If I entered in a letter instead of a number, like he would just crash and
- 3:24:19burn. But now we're actually able to handle all those different situations using these
- 3:24:23try catch blocks. So these are extremely useful. And anytime you have code that's a little
- 3:24:28dicey, especially if you're like getting input from a user and stuff like that, these can
- 3:24:32come in handy. In this tutorial, I'm going to show you guys how to use classes and objects
- 3:24:41in C sharp classes and objects is actually a big subject. And there's a lot for me to
- 3:24:46talk about in this particular topic, but this video is just going to be a very basic introduction
- 3:24:52into what classes and objects are and how we can start using them in our C sharp programs.
- 3:24:58Now in C sharp, we're going to be dealing With all types of data, and a lot of times
- 3:25:02when you're writing your programs, you're going to want to work with and represent different
- 3:25:06things in your programs. And by default, C sharp gives us a bunch of different data types
- 3:25:12that we can work with. So in C sharp, we can represent things like text. So we can use
- 3:25:17strings to represent text, we can represent characters, we can represent whole numbers
- 3:25:21with integers. And we can represent decimal numbers with things like doubles and floats,
- 3:25:25right, we can also represent true and false values with Boolean variables. But here's
- 3:25:30the problem is with just those limited data types, we're not going to be able to represent
- 3:25:36or model everything that we'd want in our programs. In other words, there's a lot of
- 3:25:41things in the real world that can't just be represented using a string or a number or
- 3:25:46a Boolean, right. I mean, think of something like a phone, for example, like imagine I
- 3:25:50wanted to represent a phone inside of my program, well, I can't represent a phone with like
- 3:25:56a string, you know, I can't represent a phone with just a number, like a phone is a complex
- 3:26:01entity in the real world. And I can't sort of break it down into just one single, like
- 3:26:08string or number, right, I can't represent it using just one of those data types. And
- 3:26:13that's kind of one of the limitations that you're going to encounter is like, you can't
- 3:26:17represent everything just using one of these data types. So what I want to talk to you
- 3:26:21guys about today is something called classes and objects. And classes. And objects essentially
- 3:26:26allow you to create your own custom data types. By default, were given things like strings,
- 3:26:33numbers, and Booleans. But I could actually create my own data type that would allow me
- 3:26:37to represent something that I wanted inside of my program. So generally, what we'll do
- 3:26:42when we want to create a data type in C sharp is we'll create something called a class.
- 3:26:47And a class is basically just a specification for a new data type. So I could create a class
- 3:26:53for a phone or I could create a class for like a water bottle, or I could create a class
- 3:26:58for a keyboard or a computer. And then I could represent all those different things inside
- 3:27:04my programs. So classes are basically use to model real world entities inside of our
- 3:27:10program. So I'm going to show you guys how we can build a class. In order to do that.
- 3:27:15The first step when you want to build a class is you want to open up your solution explorer
- 3:27:18over here, and my case is just over here on the right side of my screen, and I'm going
- 3:27:21to go down to my project. So I'm over here in my draft project. And when I want to create
- 3:27:26a new class, I can just right click, and I'm going to go down to add, and I'm just going
- 3:27:29to click New Item. And then over here, we should get this little menu that pops up and
- 3:27:34it says, class, this is the first one right up here. So I'm going to select that. And
- 3:27:39then I can actually come down here to the bottom and give this a name. So I'm going
- 3:27:42to go ahead and name my class. And generally when we're creating a class in C sharp, you
- 3:27:46want to name it with a capital letter. so in this situation, I think we should create
- 3:27:51a class for a book. So let's say that I was writing a program, maybe this was a program
- 3:27:56for like a library or a school bookstore. And I wanted to be able to represent a book
- 3:28:02inside of my program. So I want to create a book data type. So just like I create a
- 3:28:07string, or I create an integer, or I create a Boolean, I want to be able to create an
- 3:28:11actual book inside of my program. So I'm gonna create a book class. And I'm just going to
- 3:28:15call this book.cs. And I'll hit Enter. And you guys will see over here that another file
- 3:28:20has been added to our solution explorer. So now we have this book.cs file. And you'll
- 3:28:26notice that this is pretty much laid out exactly like the other one. So exactly like this program
- 3:28:33class that we were using over here. And it's basically just like an empty class. So remember
- 3:28:38what I said a class is essentially just a specification for or really, it's like a blueprint
- 3:28:43for a new data type in our program. And one of the cool things about C sharp is it actually
- 3:28:48allows us to, you know, specify a custom data type. And essentially, what we want to do
- 3:28:54is we're going to be creating a complex datatype. So a string, for example, is a data type,
- 3:29:00right. And all it is is just plain text. A number is a data type. And all it is is just,
- 3:29:05you know, a number, right, we have integers, we have floats that are whole numbers and
- 3:29:09decimal numbers. But when we create this book data type, it's actually going to be composed
- 3:29:15of all of those other data types. So inside of here, we're going to define a series of
- 3:29:19attributes. And these attributes are basically going to describe what a book is inside of
- 3:29:25our program. And that's generally what you want to do. When you create a class, we're
- 3:29:28going to take a class, and you're going to give it a series of attributes. And like I
- 3:29:33said, those attributes will kind of define what it is. So I have this book class. And
- 3:29:39I want to think about what are the different attributes that are going to allow me to represent
- 3:29:43a book inside of my program. And what I want to do is I want to actually create variables
- 3:29:48for each of those. So let's think about this. What are the different attributes of a book
- 3:29:52Well, I can think of the first one is going to be title. So over here, I'm gonna say string
- 3:29:57title. And before I say string title, I'm gonna say Public. So I want to say public
- 3:30:02string title. And I'm actually going to make this lowercase. So that's one attribute that
- 3:30:07we can store about a book. What's another one? Well, let's think we would also want
- 3:30:12to store the book's author. So I can say public string author. And let's just try to think
- 3:30:18of one more. So I think one more good one might be the number of pages. So I'm gonna
- 3:30:22say public, int pages. And all of these different variables that we're declaring over here are
- 3:30:29called class attributes. And these are essentially pieces of information that are describing
- 3:30:36what a book is in our program. So I'm essentially modeling the book data type, I'm creating
- 3:30:42a blueprint for the book datatype, I'm saying that inside of our program, a book is going
- 3:30:47to have a title, an author, and a number of pages. So this is a specification, it's a
- 3:30:53template. Now, what I can do is I can go off to my other program, and I can actually create
- 3:30:59a book inside of my program. So just like I create a string, or I create an integer,
- 3:31:04or I create a double or a Boolean, I can create a book, and I can store it inside of a variable.
- 3:31:10And we can actually create something called an object, which is going to be a actual book
- 3:31:15inside of our program. So I'm gonna show you guys how we can do that, I'm going to save
- 3:31:18this file. And I'm going to head back over to program.cs. So this is like the file that
- 3:31:23has our main method over here. And I want to show you guys how we can create a book
- 3:31:28now. So if I wanted to create an represent an actual book inside of my program, since
- 3:31:34I've created that class, and I define what a book is, I basically defined the book datatype,
- 3:31:40I can actually do that. And I can create something called an object. And an object is an instance
- 3:31:47of a class. So when we created the class, we created the template for what a book is,
- 3:31:52when we create an object, we're creating an actual physical book inside of our program,
- 3:31:58right? So over here, this class specified like a title, author pages, right? This is
- 3:32:03a specification for what a book is, when I create a book object, I'm creating an actual
- 3:32:10book with an actual title, actual number of pages and an actual author. So I'm gonna show
- 3:32:14you guys how we can do that. The first thing I want to do is I'm basically going to create
- 3:32:18this, like I would a normal variable. So the first thing I'll do is specify the data type.
- 3:32:22And in our case, we're going to create a book using the book data type. So just like if
- 3:32:26I was creating a string, I would say, like string, you know, whatever is equal to whatever.
- 3:32:31Here, I'm just gonna say book. And we're gonna do the same thing. So I'm gonna say book,
- 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
- 3:32:40going to do is set it equal to new book, and an open and close parentheses. And this is
- 3:32:46how we can create what's called a book object. This is essentially an instance of a book.
- 3:32:52So this is like a physical book inside of our program. So the class is a specification,
- 3:32:58the class tells us what the book data type is, an object is an actual instance of that
- 3:33:04class. All right, hopefully that makes sense. So now I've actually created this book called
- 3:33:09book one. And we can use book one over here, just like a variable, I mean, it's essentially
- 3:33:14just a variable that's storing a book object. So remember, the book has all these attributes,
- 3:33:21right? So a book can have a title, a book can have an author and a book can have a number
- 3:33:26of pages. So when we create our book, we want to give it all of those attributes. So what
- 3:33:31I can do is I can say like book one dot, and now I can access the attributes. So I could
- 3:33:36say book one dot title, and I can actually give this a value. So let's say that we wanted
- 3:33:41to create an object that would represent one of the Harry Potter books. So I could say
- 3:33:45book, one dot title, is equal to Harry Potter. So now the title of my book is going to be
- 3:33:52Harry Potter. Let's come down here. And we'll say book, one dot author, and the author of
- 3:33:57the Harry Potter books is JK Rowling. So I'm gonna say book, one dot author is equal to
- 3:34:03JK Rowling. And then finally, down here, we're gonna say book one dot pages. And let's just
- 3:34:08say that this Harry Potter book has 400 pages in it. So what I did was I created a book
- 3:34:13object was, which is an instance of the book class. And I was able to define all of the
- 3:34:19pieces of information about this book. So I define what the book's title was, I define
- 3:34:24what the book's author was, and I defined how many pages the book had. And so now what
- 3:34:28I can actually do is I can work with this piece of information. So I can say like console
- 3:34:33dot write line, and over here, I could print out some attributes of the book. So I could
- 3:34:39say like book one dot title, and now when I run my program, it's going to print out
- 3:34:44the title of the book we just created. So it should print out Harry Potter. I can do
- 3:34:49the same thing for the other attributes. So I could say like book one dot pages, and now
- 3:34:54we're going to print out however many pages book one has, so we're going to get 400. So
- 3:34:59essentially, I'm creating here a book, right? I defined the book data type in that class.
- 3:35:05And now I'm creating an actual book inside of my class, which we're calling an object.
- 3:35:10And what's cool about these objects is I can create as many objects as I wanted. So I could
- 3:35:13come down here, and I could actually make another object. So I'm going to copy this
- 3:35:17guy. And I'm actually going to paste it down here. And instead of being book one, I'm going
- 3:35:22to change it to book two. So this is going to be Book Two, Book Two, Book Two, book two.
- 3:35:27So let's start giving this some different information. Let's say this object is going
- 3:35:31to represent one of the Lord of the Rings books. So I could say, Lord of the Rings,
- 3:35:37and then the author is token and let's say the Lord of the Rings has like, I don't know,
- 3:35:42700 pages in it. So now I have a book up here called book one, which is representing the
- 3:35:48Harry Potter book. And then I have another book down here, Book Two, which is representing
- 3:35:53the Lord of the Rings book. And I'm actually able to have an actual book in my program,
- 3:35:58right, so I'm able to represent and store and work with and model a book inside my program.
- 3:36:05So down here, I could do the same with Book Two, I could say like Book Two, dot author,
- 3:36:11and now this is going to print out the author of book two. She'll see over here, we get
- 3:36:16token. Now I want to bring up one point. So both of these objects are books right over
- 3:36:24here. In this book class, I defined the book datatype, I basically told C sharp like, Hey,
- 3:36:29we got a new data type. It's a book, it's awesome. All books have a title and author
- 3:36:34and a number of pages. And over here, I created individual books off of that blueprint off
- 3:36:40of that template. Right. So book, one is its own book. It has its own title, its own author
- 3:36:46and its own pages, Book Two, down here is another book. And it has its own title, its
- 3:36:53own author and its own pages. So if I was creating an application for like a library,
- 3:36:58for example, let's say the library had 10,000 books. Well, I could create 10,000 objects,
- 3:37:04all of which would have the title, the author and the number of pages of each one of those
- 3:37:09books. And then I could work with those books, I could pass them into methods, I could use
- 3:37:14them in if statements, I can do all sorts of stuff with those books, just like I would
- 3:37:18with other data types, like strings, numbers, or Booleans. So classes and objects are extremely
- 3:37:25powerful. And really, this is just scratching the surface. This is like lesson one. You
- 3:37:29know, this is the bare basics of working with classes and objects. As we go forward. For
- 3:37:33the next few videos, we're gonna learn all sorts of ways to make these a lot more powerful.
- 3:37:42In this tutorial, I want to talk to you guys about using constructors in C sharp classes.
- 3:37:48A constructor is basically a special method that we can put inside of a C sharp class,
- 3:37:54which is going to get called whenever we create an object of that class. So anytime I create
- 3:38:00an object of a specific class, the constructor method will get called. And we can do different
- 3:38:05things. So I'm going to show you guys how we can leverage that constructor method in
- 3:38:09order to make it a lot easier for us to create objects off of the classes that we create
- 3:38:15in C sharp. So down here, if you're not following along with the course, I basically created
- 3:38:20a class over here called book. And I just created this book class. And I basically said
- 3:38:26that the book is going to have a title and author and a number of pages. That's kind
- 3:38:30of all this book is. And then over here in my program.cs, and my main method, I created
- 3:38:36two books. So I created a Harry Potter book, and I created a Lord of the Rings book. Now
- 3:38:41I want to show you guys how we can use a constructor and we're actually going to be able to use
- 3:38:45a constructor to make it a lot easier for us to create these objects. But before we
- 3:38:50do that, I just want to give you a introduction into what constructors are, and kind of show
- 3:38:54you what's happening. So over here in my class, I'm actually going to create a method down
- 3:38:59here, I'm just gonna say public book, I'm gonna make an open and close parentheses and
- 3:39:04open and close curly bracket. Now you'll notice that I said public and then I said the name
- 3:39:09of the class. And this is what's called a constructor. So anytime I make a method like
- 3:39:14this, where I'm saying public, and then the name of the class, this means that this is
- 3:39:18going to be the constructor for this class. And like I told you guys before, the constructor
- 3:39:23is basically a special method inside of our class, that's going to get called when we
- 3:39:28create an object of this class. So let me prove that to you guys. I'm going to come
- 3:39:32down here and I'm just going to type out console dot write line. And I'll make an open and
- 3:39:37close parentheses and down here, I'm just going to write creating book. Okay, very simple.
- 3:39:43Now over here in my program in my main method, I'm going to run this and what you're going
- 3:39:49to see is every time I create a new book, this line and this line that is going to get
- 3:39:54executed, so I'm going to run the program and you'll see over here it says creating
- 3:39:58book and creating Book, essentially, what's happening is when I come down here and I say
- 3:40:03new book, what I'm doing here is I'm actually calling that constructor method. So this right
- 3:40:11here, this line where I'm saying new book, this book is directly linked to this method
- 3:40:18over here. And again, I'm going to prove this to you. So over here, what I'm going to do
- 3:40:23is I'm going to say that this book method, this constructor should accept one parameter.
- 3:40:28So I'm gonna say, string name, that's all I'm gonna say. And then down here, I'm just
- 3:40:33gonna print out name. So this method is accepting one parameter name, and then down here, we're
- 3:40:39gonna print it out. Over here, you'll notice I'm already getting errors. And that's because
- 3:40:44I didn't include a parameter. So I can come over here and say, like, Mike, and then down
- 3:40:48here, we can pass another name like john. So now when I run my program, it's going to
- 3:40:53print out Mike, and it's going to print out john, because I'm calling that book constructor.
- 3:41:00Awesome. So not only does this call that constructor, but from here, we can also pass parameters
- 3:41:10into that constructor. And that is extremely powerful. So I'm going to show you guys what
- 3:41:15we can do with that information. Now, let's talk about the code that we have over here,
- 3:41:21you'll notice that I have a bunch of code here. So when I created this Harry Potter
- 3:41:25book, it took me four lines of code, right? When I created this book to this Lord of the
- 3:41:30Rings book took me another four lines of code, total of eight, eight lines of code just to
- 3:41:35create two objects, right, just to create two books, I had to write out eight lines
- 3:41:39of code. Not only that, I had to manually type out book one dot title is equal to Harry
- 3:41:44Potter book, one dot author is equal to JK wrong, like, that's a huge drag. You know,
- 3:41:49having to do that every single time you want to create a book is extremely annoying. And
- 3:41:54imagine if we were instead of creating two books, we were trying to create 10 or 12,
- 3:41:58or 20 books, right, it would take forever, because we'd have to manually go through and,
- 3:42:03you know, assign a title and an author and a number of pages. So this is a totally valid
- 3:42:08way to create our object and give it some attributes. But there's actually another way,
- 3:42:12and we can use that constructor to do it. So essentially, what I want to do is I want
- 3:42:17to allow the caller to pass in a title and author and a number of pages into this book
- 3:42:25constructor. So when they create the book, I want them to be able to tell me what the
- 3:42:30title is going to be, what the author is going to be and what the number of pages is going
- 3:42:34to be. That way, they can just tell me right away, and I can handle it up front. And then
- 3:42:39I don't have to say like book, one, title is equal to whatever book one dot author is
- 3:42:44equal to whatever. So let's see if we can wire that up over here. Again, this book constructor
- 3:42:50is going to take in three parameters. And we already saw before that over here, we can
- 3:42:57pass parameters into here, right, so I can pass a parameter into here, and it's going
- 3:43:01to get passed into that constructor. So I'm going to say that this constructor is going
- 3:43:05to take three parameters, the first is going to be a string, and I'm going to call this
- 3:43:09a title. And this is going to stand for argument title. And you guys will see why I'm naming
- 3:43:14this differently in a second. And then I'm going to make another string, I'm going to
- 3:43:17call it a author. And then I'm going to make an int, and I'm call it a pages. Now, essentially,
- 3:43:25what's happening is whenever we create a book, we need to pass in the title, the author,
- 3:43:31and the number of pages. And so what I can do now is from inside of this constructor,
- 3:43:37I can just come down here and I can say, title is equal to a title. And basically, what I'm
- 3:43:46saying is this title up here, the title of the object is going to be equal to the title
- 3:43:53that they passed in. So whenever we create a new book, now we're going to pass in a title.
- 3:43:58And down here, I'm going to say that the title of the object is going to be equal to a title.
- 3:44:03And so now instead of having to say like book, one dot title is equal to whatever. Now I
- 3:44:09can just pass the title in over here. And I'll just assign the title from over here.
- 3:44:14So essentially, doing this is the same as saying book one dot title is equal to whatever,
- 3:44:18it's the same exact thing, except I can just do it straight from in here. So what we want
- 3:44:24to do is do this for each of these attributes. So I can say, author is equal to A author.
- 3:44:31And then we can say pages is equal to A pages. And I just want to point out, you can name
- 3:44:36these whatever you want. So I can even name this title if I wanted. Or I could name this,
- 3:44:41you know, whatever, it doesn't matter. I'm just using a title, because it kind of shows
- 3:44:46that this is the argument that got passed in. And it kind of distinguishes it from the
- 3:44:51actual attribute of the object, but you can name it whatever you want. Alright, so now
- 3:44:55when we go back over to our program, you notice we're getting errors here and it's basically
- 3:44:59saying There's no argument given that corresponds to the required formal parameter. In other
- 3:45:04words, hey, you need to pass in all this information now. So what I can do is I can take this information
- 3:45:10from down here, and I can pass it into here. So I can pass in the title Harry Potter, I
- 3:45:15can pass in the author, JK Rowling. And I can pass in the number of pages 400. So instead
- 3:45:23of having to do that manually, I can just get rid of all this. And now we have the same
- 3:45:28thing. So I'm passing in the title, the author and the number of pages. And then over here
- 3:45:34in the book, I'm taking those parameters that got passed in, and I'm assigning them to the
- 3:45:39attributes of the object. And that is super cool and super useful. So let's do the same
- 3:45:44thing for Lord of the Rings, we can copy this paste this bad boy up here. So I can get rid
- 3:45:50of all of this stuff now. And essentially, we have the same exact thing. So we went from
- 3:45:55having eight lines of code to create these two objects. So now we just have two lines
- 3:46:00of code one each, and we're passing in all of these different attributes. So this should
- 3:46:05actually work. And I'm going to go ahead and prove that to you. So I'm gonna say console
- 3:46:08dot write line. And I'm going to go ahead and just print out like Book Two dot, I don't
- 3:46:14know title. And so you'll see that this attribute got set. So now I can run my program. And
- 3:46:21we get a Lord of the Rings. So it's doing exactly the same thing. Now, I also want to
- 3:46:25show you if you want, you can modify any of these values. So I can technically come down
- 3:46:28here and say book to title is equal to the Hobbit. And now this is going to update book
- 3:46:36two's title. So now when we print it out, it's not going to be Lord of the Rings anymore,
- 3:46:40it's going to be the Hobbit. So that's kind of how this constructor works. And that constructor
- 3:46:45is super powerful. It's an amazing way for us to be able to create these objects really
- 3:46:51quickly. And I also do want to point out that you can have more than one constructor. So
- 3:46:56I have this constructor book. And it takes a title and author and a number of pages.
- 3:47:01But if I wanted, I could just create another one. So I could say like public book. And
- 3:47:06this could take no parameters. And I'd still be able to do this. And so now I can have
- 3:47:12that. And I'll still be able to create a book just by saying book, book three is equal to
- 3:47:18new book. So now I'm able to create the book like this with no parameters with no arguments.
- 3:47:24Or I can create the book with a bunch of arguments. So you can create as many of these constructors
- 3:47:29as you want. And yeah, that's kind of the basics of constructors, these are very powerful
- 3:47:34we want to do is just play around with these, right? I mean, as long as you understand what's
- 3:47:37going on, like this constructor over here is directly related to me saying book over
- 3:47:44here, then you have like, have a good understanding of what these are. And this tutorial, I'm
- 3:47:52going to talk to you guys about object methods in C sharp. An object method is basically
- 3:47:58just a method that we can define inside of our class that the objects of that class can
- 3:48:03use to either find out information or modify information about themselves. And object methods
- 3:48:09can be very useful. So I'm going to show you guys just a little demonstration on how to
- 3:48:13use them in this tutorial, what I've actually done is I've created another class in my program.
- 3:48:19So I created this student class over here. And this student class is pretty simple. I
- 3:48:24just defined three attributes that are going to be part of my student class. So every student
- 3:48:29is going to have a name, a major and a GPA. And then down here, I have a constructor.
- 3:48:33So I'm just sort of passing in the name, the major in the GPA, and I'm assigning them down
- 3:48:37here. So this is a very simple class, a lot like the class we created in the last tutorial
- 3:48:43with constructors. So now that I have this student class, I went over to my program,
- 3:48:48and I actually created a couple students. So I had these variables, student one and
- 3:48:52student two. And then I created these student objects. So one is his name is Jim. He's a
- 3:48:58business major, and he has a 2.8 GPA. And then we have Pam, she's an art major, and
- 3:49:03she has a 3.6 GPA. So these are two objects that I've created from that student class.
- 3:49:10And I want to show you guys how we can put a method inside of that student class that
- 3:49:16these student objects can use to find out some information, what I want to do is I want
- 3:49:21to be able to figure out whether or not each of these students is on the honor roll. Right?
- 3:49:25So let's say that we're, you know, modeling students for a college or university and the
- 3:49:30rules that that college or that if you have a GPA of 3.5, or greater than you have honors,
- 3:49:35so you're on the honor roll, I want to be able to find out whether or not Pam and Jim
- 3:49:40are on the honor roll really easily. So one way I can do that is by creating an object
- 3:49:45method. So what I'm going to do is I'm going to come over here to my class, and down below
- 3:49:49this constructor, I'm going to create a method. So I'm actually going to create a method that
- 3:49:55is going to tell us whether or not the student has honors. So I'm gonna come down here And
- 3:50:00I'm just going to say public. And I'm gonna say bool. Because we're going to return a
- 3:50:05boolean value. Essentially, this method is going to return a true if the student has
- 3:50:11honors, or it's going to return false if the student doesn't have honors. So now I'm just
- 3:50:16going to call this has honors. And I'm going to make an open and close parentheses, and
- 3:50:21an open and closed curly bracket. So inside of here, we can start writing out the code
- 3:50:27to tell if a student has honors. Now remember, what I said before was, if a student has a
- 3:50:32GPA of 3.5 or above, that means they're on the honor roll. So what I can do is I can
- 3:50:37just use a simple if statement. And I can just say if, and I'll come down here, and
- 3:50:41I'll just say, if GPA is greater than or equal to 3.5, then I'm going to come over here,
- 3:50:50and I'm basically going to return true. So I'm just going to return true. And if that's
- 3:50:56not the case, then I'll come down here and we'll just return false. So in the case that
- 3:51:01the GPA is greater than 3.5, we'll hit this line of code, which is telling us to return
- 3:51:06true, and we'll exit the method. And if this is false, so if the GPA isn't greater than
- 3:51:113.5, then we'll come down here and return false. So this is obviously a very simple
- 3:51:16method, but it's going to come in handy with our objects. So I'm going to head back over
- 3:51:20here. And basically, what I can do now is I can figure out whether student one or student
- 3:51:25two is on the honor roll. So I can come down here and I can just print this out. So I'll
- 3:51:30say console writeline. And what I want to do is I want to print out whether or not each
- 3:51:35of these students has honors. So I'm actually going to copy this and I'll come over here
- 3:51:40and I'll say, student, one dot has honors. And then we can also say, student two dot
- 3:51:49has honors. Alright, so I'm printing out if whether or not student one has honors, and
- 3:51:57I'm printing out whether or not student two has honors. Now, what should happen is student
- 3:52:03one dot has honors should return false because Jim has a 2.8 GPA. Jim's not that smart. And
- 3:52:10this line down here, student two dot has honors should return true because Pam has a 3.6 Pam's
- 3:52:16pretty smart. So let's go ahead and run our program. And we'll see what happens. So you'll
- 3:52:21see over here, we got exactly what we thought we would we got false and true. Now one of
- 3:52:26the cool things about the method that we wrote over here in this student class is that this
- 3:52:32method is actually going to use different information depending on what object is calling
- 3:52:38it. So when the gym object calls it, when student one calls it, it's going to use Jim's
- 3:52:44GPA. So when Jim calls this method, it's going to use Jim's GPA of a 2.8. When Pam calls
- 3:52:51this method, when that Pam object calls it, it's going to use Pam's GPA of 3.6. So we
- 3:52:57can specify one method here, we only need to write at one time, and the different objects
- 3:53:03will be able to use it, and they'll be able to use it with their own information. So Jim
- 3:53:08calls this method and uses his GPA. Pam calls this method and uses her GPA. And that's why
- 3:53:14these methods can be so powerful, because we can define one method and then different
- 3:53:19objects that can use it. And they can get different information back depending on the
- 3:53:24value of their attributes. Another cool thing about this method is if I ever wanted to change
- 3:53:30the qualification for honors, so let's say that I wanted to change honors from being
- 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,
- 3:53:43now, I only had to change the code right here. And when I run this program, again, now they're
- 3:53:49both going to have honors. So that value is going to be automatically updated for both
- 3:53:55of these objects, just by changing it in that one spot. And that's what's so powerful about
- 3:53:59these methods is they basically allow me to define different rules for my object. So like,
- 3:54:04I can define what it means to have honors in my program. And I can change it around
- 3:54:09if I want. And it's not really going to matter, because the objects are just calling this
- 3:54:13method, and it's telling them whether or not they have honors. So that's why this is useful.
- 3:54:18And in the next couple videos, we're going to be looking at some other ways that we can
- 3:54:21use some methods just like this. But basically, all you need to know is that you can make
- 3:54:26as many of these as you want, right? So I can make a method over here that tells me
- 3:54:30whether or not, you know, specific rule so that I could it could tell me whether or not
- 3:54:35the students have honors, I could also make a method that would modify one of these values.
- 3:54:39So nothing is stopping me from coming in here and saying GPA is equal to 90.4. Right? I
- 3:54:45could modify each of these students GPA, if I wanted to, you can basically do anything
- 3:54:49you would do in a normal method. So play around with these, you know, I'm sure you can think
- 3:54:53of a couple of different, you know, methods that you can create inside of your classes.
- 3:54:57They can be really awesome. In this tutorial, I'm going to talk to you guys about using
- 3:55:05getters and setters in C sharp. Now a getter and a setter are basically two types of methods
- 3:55:12that we can define inside of our C sharp classes, which will essentially control the access
- 3:55:19that people have to the attributes of those classes. And essentially, what this does is
- 3:55:25it kind of like makes your classes more secure. So it kind of allows you to define like, what
- 3:55:30data is valid for specific attributes and what data is invalid. And really getters and
- 3:55:35setters are extremely useful. And there's a lot of different scenarios where you want
- 3:55:39to use them. And this tutorial, I'm going to show you guys one specific scenario, we're
- 3:55:43going to talk about, like how to set these up, and essentially just how they work. So
- 3:55:47what I've done in preparation for this tutorial is I actually created another class, it's
- 3:55:51this movie class over here. And this movie class is very simple. It's basically just
- 3:55:56modeling a movie inside of our program. So this is sort of like, you know, the template
- 3:56:00for the movie data type. And I said that every movie is going to have a title, a director
- 3:56:05and a rating. And then down here, I created this constructor where I basically pass in
- 3:56:10a title, a director and a rating. And I assigned all of those values. So this is a very simple
- 3:56:17class. And you know, like I said, it's just modeling a basic movie. And then over here
- 3:56:20in my program, I created two movies. So I have one called Avengers, and it's just the
- 3:56:25Avengers with director, john Sweden, and it's pG 13. And then we have Shrek with director
- 3:56:31Adam Adamson. And it's rated PG. So pretty simple program, right? We're basically just
- 3:56:36creating a couple of different movies. But here's one of the problems that we actually
- 3:56:41have with this program. And it might not be super obvious right away. But the problem
- 3:56:45has to do with the movie rating. Right? Now generally, when movies have ratings, they
- 3:56:51can only have specific ratings, right? So if we have a movie, like generally, it's only
- 3:56:56going to be able to be G, PG, PG, 13, R, or nr, right. And maybe there's some more ratings,
- 3:57:03whatever. But let's just say that these are all the valid ratings that we can have for
- 3:57:07a movie. So movie can either be rated G, PG, PG, 13, R, or nr four not rated. And let's
- 3:57:13say that we want to enforce these rules inside of this movie class. So we want all the movies
- 3:57:19in our programs to only be able to have one of these ratings, and it can't have a rating,
- 3:57:26other than the ones that I specified down here, right? Well, the problem is, how do
- 3:57:30we enforce something like that? Right, because what I could do is I could easily come over
- 3:57:34here and change the rating of Avengers to dog for example. And now if I was to come
- 3:57:40down here and print out the Avengers rating, for example, if I came down here is that console
- 3:57:45dot write line, Avengers dot rating, when I print this out, it's actually going to print
- 3:57:52out the rating. So it's going to be able to print out dog. So dog was the rating that
- 3:57:56we gave to it. But dog isn't one of the valid ratings down here. Another thing I could do
- 3:58:02is even if I set this to pG 13, over here, so if this was like set to pG 13, I could
- 3:58:08still come over here and manually set it. So I could still say like, Avengers dot rating,
- 3:58:13is equal to, you know, dog over here, and it's going to be the same scenario, right?
- 3:58:17I'm still able to set this value equal to dog. So what I want to show you guys is how
- 3:58:22we can prevent this from happening. In other words, how can we make it so that the only
- 3:58:27possible ratings for the movies are one of these five ratings that I defined down here,
- 3:58:34we can actually use something called getters and setters. And in order to use these getters
- 3:58:39and setters, we're gonna have to modify a couple things. So the first thing that we
- 3:58:43want to do is we want to close off access to this rating property, this rating attribute
- 3:58:50and what I can do is I can actually come over here into my program. And you'll see that
- 3:58:54all of these attributes are public. So we have public title, public director and public
- 3:59:00rating. And I haven't really talked about this too much yet. But public essentially
- 3:59:05means that these can be accessed by anyone. So any other program, any other code that
- 3:59:10we're dealing with can access any of these attributes. So public just means it's open
- 3:59:15to everybody. It's open to the world. But there's actually another modifier that we
- 3:59:19can put in front of here. And instead of saying public, I could say private. And what this
- 3:59:24means when I say private, this means that only code inside of this movie class is going
- 3:59:31to be able to access the rating. So only code that is contained inside of these curly brackets
- 3:59:37over here in this out of this movie class is going to be able to access this and you'll
- 3:59:41see now when I go over to program, I'm getting an error down here. So essentially, what this
- 3:59:47is saying is we can't access the rating because it's private. So because the code over here
- 3:59:53is inside of the program class, and it's not inside of the movie class. It's not going
- 3:59:58to be able to access the rating. So That's the first step in being able to enforce these
- 4:00:04ratings is we need to make the rating field private. So I need to cut off access from
- 4:00:09anybody else. Now, the next thing I want to do is I want to create two things called getters
- 4:00:16and setters. So we're going to create getters and setters for this rating attribute. And
- 4:00:22basically, what these are going to allow us to do is it's going to allow outside code,
- 4:00:27for example, the code inside of this program class like down here, it's going to allow
- 4:00:31code like this to both access the rating, and modify and set the rating. So those getters
- 4:00:39and setters are going to be really important. And we can define those through what are called
- 4:00:43properties. And a property is basically just like it's kind of like a little method. But
- 4:00:48it's a special method that's going to basically allow us to define a getter and setter. So
- 4:00:54what I want to do is I can just say public, and I can just say string. And what I want
- 4:01:00to do is name this, generally, what you're going to do is you're going to name it the
- 4:01:05same as the attribute over here, but in capital letters. So I'll name this public string rating.
- 4:01:10And then down here, I'm going to make an open and close curly bracket. And inside of here,
- 4:01:14I need to define two things or I don't need to, but I'm going to define two things. So
- 4:01:18I can just say, Get an on make an open and close curly bracket. And then I'm going to
- 4:01:22say set, and I'm going to make an open and close curly bracket. And these are essentially
- 4:01:27going to be getters and setters. So a getter is going to allow us to get the rating attribute.
- 4:01:33And notice over here, I'm trying to get this rating attribute by saying Avengers dot rating,
- 4:01:38but I'm not able to do it right, I can't actually access this rating, because it's private.
- 4:01:43And so the job of the getter is to give me that. So I'm just gonna come over here and
- 4:01:48inside this get, we're just going to say, return rating, and I'm going to put a semicolon
- 4:01:53there. And so basically, whenever we say rating now, and we want to get it, it's going to
- 4:01:59return the rating. Now this setter is going to allow us to set the rating, so it's going
- 4:02:05to allow me to modify the rating. So if I came over here and said, Avengers dot rating,
- 4:02:11and you can already see it's trying to make me use this capital letter is equal to dog,
- 4:02:16it's gonna yell at me, because this is a private attribute. So it's not going to let me do
- 4:02:22that. And that's what the that's the point of this setter, the setter is basically going
- 4:02:26to allow me to do that. So what I can do in this setter, though, is I can define a specific
- 4:02:32rule. So I can basically say like, Hey, you can only set this value to be a certain value.
- 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.
- 4:02:43And if it is, if the value they're trying to set rating to is one of the valid ratings,
- 4:02:48then we're going to go ahead and set it otherwise, we're just going to do something else. So
- 4:02:52I want to say if and I want to check to see if the value that they passed in. In other
- 4:02:57words, the value that they're trying to set to rating is equal to one of the valid rating.
- 4:03:01So what we can actually do is we can say if the value and value is going to represent
- 4:03:06whatever got passed in, so value is going to represent whatever they're trying to set
- 4:03:10the rating to. So I can say if value is equal to and we're just going to do one for all
- 4:03:16of these. So if the value is equal to G, or value is equal to PG, and I'm going to do
- 4:03:23this one for each of the valid ratings, so I can just say or value is equal to, and over
- 4:03:29here, we can just put pG 13. So I'm gonna say pG 13. And we're just going to do two
- 4:03:34more so value is equal to R. And then the last one is going to be for nr. So n over
- 4:03:41here, I'll say or value is equal to an R. So if it's equal to one of these, in other
- 4:03:48words, if the value that we pass in is equal to one of these, then we're going to be able
- 4:03:53to just set that is the rating. So I can just say, rating is equal to value. Otherwise,
- 4:04:02though, I'm just gonna set the rating equal to nr, so I'm just gonna say rating is equal
- 4:04:07to nr. So they add in an invalid rating. In other words, they add in a rating, they try
- 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
- 4:04:21set to not rated by default. So this getter is going to allow us to get the rating and
- 4:04:27this setter is going to allow us to set the rating. But you'll notice that in order to
- 4:04:32set the rating, we have to go through this if statement, right? So they can try to pass
- 4:04:37in whatever rating they want. But if it's not equal to G, PG, PG, 13, etc, then it's
- 4:04:42just going to get set equal to not rating. And the reason we needed these getters and
- 4:04:47setters was because we made this guy up here private. So there's one more thing we have
- 4:04:53to do in order to make this super secure. In other words, there's one more thing that
- 4:04:56we have to do to make sure that the rating is on Sent to one of these values up here
- 4:05:02in my constructor, when I set the rating right here, I'm just assigning it a normal value.
- 4:05:07So technically, the user could pass in whatever rating they wanted over here. And then I'm
- 4:05:12just gonna set it to the rating. But now instead, I'm just gonna say rating is equal to A rating.
- 4:05:17And what this means is now we're going to set the rating through this setter down here,
- 4:05:23when I use this capital R, it's essentially calling this setter down here and setting
- 4:05:29it that way. So now this is completely secure. And down here, what I can do now is I can
- 4:05:35set this to whatever I want, and it's going to be safe. So down here, I can say Avengers
- 4:05:39dot rating. So I'm using this capital R now. And over here, if I wanted, I could pass in
- 4:05:45an invalid rating. So I could pass in like dog over here. And when I print it out, you'll
- 4:05:50see that we're going to get an R, so we're not going to get dog, we're actually going
- 4:05:54to get n r. And that's because we had to go through that setup. And the same goes for
- 4:06:00anything else. So I could say down here, like Shrek dot rating is equal to cat. And if we
- 4:06:07tried to print out Shrek dot rating, then we're going to get an R because it's going
- 4:06:13to get filtered out by that center. But here's the thing, if I wanted to set this to a valid
- 4:06:18rating, like if I set Shrek dot rating equal to R, now it's going to be able to take that
- 4:06:23rating, because it's valid. So that's kind of how we can use getters and setters. And
- 4:06:28a lot of times what people will do is they'll create getters and setters for each of the
- 4:06:34attributes inside of their classes, you don't have to, but a lot of times they can be useful.
- 4:06:38So we want to do is just play around with that. And, you know, basically, these just
- 4:06:42make your class more secure. So they make it so someone couldn't, for example, give
- 4:06:46your movie in invalid rating. In this tutorial, I want to talk to you guys about static attributes
- 4:06:55in classes in C sharp, a static attribute is basically a special type of attribute in
- 4:07:01a class, which is shared by all of the objects and all the instances of that class. So a
- 4:07:07static attribute is technically an attribute that is contained on the class itself, instead
- 4:07:13of on the individual objects of that class. And I'm going to sort of explain to you how
- 4:07:18this works. Over here in my program, I actually created a class, and it's called song. And
- 4:07:24this class basically just models a song in our program. So it's kind of like a blueprint
- 4:07:28for what a song should be in our program. songs have a title and artist and a duration.
- 4:07:33And then down here, I just have this basic constructor, which allows the caller to create
- 4:07:39the object right up front. So over here in my program, I created two songs. One's called
- 4:07:44holiday one's called cashmere. This is holiday by Green Day, and I just had it was 200 seconds.
- 4:07:50And then Kashmir by Led Zeppelin, 150 seconds. So we have our two song objects. And I want
- 4:07:56to show you guys how we can actually create a static attribute on this song class. So
- 4:08:03one thing I want to point out is there's these normal attributes that we have over here on
- 4:08:07this song class, like the title, the artist and the duration, if I was to come down here
- 4:08:13and print these out, so for example, I can come down here and I can print out holiday
- 4:08:18dot title. And then right after it, I could also print out cashmere dot title. And if
- 4:08:26I was to do this, you'll notice that both of these objects have different titles. So
- 4:08:30the first objects title is holiday. The second is cashmere, I could do the same thing for
- 4:08:36the other attributes. So if I came down here and said, holiday dot artists, and cashmere
- 4:08:41dot artists, they're gonna have different artists, right. In other words, there's two
- 4:08:46different values that are assigned to the attributes of those objects, right. So when
- 4:08:51I create an object in my program, on basically allowing it to have its own title, its own
- 4:08:58artist and its own duration. So each individual object will have their own title, their own
- 4:09:04artists and their own duration, right? Those things are going to differ across all of the
- 4:09:09objects. And that is what we would consider a normal attribute. But there's another type
- 4:09:14of attribute which is called a static attribute. And a static attribute is an attribute that
- 4:09:20isn't unique to each one of the objects. In other words, a static attribute is an attribute
- 4:09:24about the actual class. So this title attribute, like I said, it's different for each of the
- 4:09:31objects. So holiday dot title, and cashmere dot title, those are different values. But
- 4:09:35a static attribute is going to be the same across all of the objects of a class. In fact,
- 4:09:41a static attribute is basically just an attribute that we would say is about the class. So let
- 4:09:46me show you guys. Basically what this is I'm going to come down here and I'm going to create
- 4:09:50a static attribute. You can create it similar to these ones, I can just say public static.
- 4:09:55So I'm using this static keyword here. And then I'm going to give it a type and I'm just
- 4:09:59going to call it Song count. And basically what this variable is going to do is it's
- 4:10:04going to tell us how many song objects have been created. So I'm just going to set this
- 4:10:08equal to zero initially. So I'm giving it an initial value. Now I want to show you guys
- 4:10:13what we can actually do with this static variable. Because we have this static attribute, I can
- 4:10:19actually come over here. And instead of saying like holiday dot artist, I can actually say,
- 4:10:26song dot song count. And I could actually print this out. So I'm going to print this
- 4:10:32out. And you'll see that we should just get zero, because that's what I set it to initially.
- 4:10:38So unlike the artists, the title and the duration, the song count attribute is actually on this
- 4:10:45class. So I couldn't say song, title. Because title isn't an attribute that's associated
- 4:10:53with song, you'll see down here we're getting this error, I couldn't say song dot artist,
- 4:10:58because artist is going to apply to the objects of the class, not the class itself. But I
- 4:11:04can say song dot song count, because this is a static attribute, which is, which means
- 4:11:09it belongs to the class. So that's kind of the difference. And I want to show you guys
- 4:11:14something that we could actually do. So over here, I have my constructor, right? This is
- 4:11:19a constructor method. It's a very simple constructor. Anytime that we create a song object. So anytime
- 4:11:25a song object gets created, this method gets called it's a guarantee, right? Basically,
- 4:11:31when I come over here, and I say, new song, this is me calling that constructor. So anytime
- 4:11:37we create one of these songs, it's going to get called. So what I'm going to do down here
- 4:11:41is every time we create a song, in other words, after all this code in the constructor, I'm
- 4:11:45just going to increment the song count. So I'm going to say, song count plus plus. So
- 4:11:50basically, what's going to happen is, every single time a song object gets created, the
- 4:11:55song count is going to get incremented. So let me show you guys how this is gonna work
- 4:11:59over here, I'm actually just going to, I'm going to get rid of this guy. And I'm going
- 4:12:05to copy this and I'm going to go ahead and paste it in between here. So in between hot
- 4:12:11the creating holiday and creating cashmere, we're printing out the song count. So let's
- 4:12:17see what happens, what we should get is one in two. So basically, when we create holiday,
- 4:12:23and then we print out the song count, one song has been created, so we get a one over
- 4:12:27here, then we create this cashmere object. And so that song count is going to get incremented
- 4:12:33again, and here we get to. So this static attribute is telling us information not about
- 4:12:39the specific objects themselves, but about the class in general, right. So I can figure
- 4:12:45out how many objects have been created of the song class, right? How many instances
- 4:12:50of the song class have been created in my program, but that's not a piece of information
- 4:12:54that is going to be specific to the holiday object or to the cashmere object, that is
- 4:13:00a piece of information that's going to be specific to the class itself. And that's basically
- 4:13:04what a static class attribute is, it's an attribute about the class not about the specific
- 4:13:11objects of the class. So it's basically an attribute that we store to kind of, you know,
- 4:13:15give us information about that class. And you can create static attributes, you can
- 4:13:20create as many as you want. And honestly, they can be really useful. And you'll notice
- 4:13:24that whenever we access them, we don't say like, I wouldn't say like cashmere dot song
- 4:13:30cow, like, I'm not going to be able to do that. So we have to access it through the
- 4:13:34actual class. Now, one thing that we could do is we could create a method. So I could
- 4:13:40come over here and I could create a method. So I could say, like, public get song count.
- 4:13:46And actually, this is going to return an integer. And then over here, I could actually return
- 4:13:51the song count. So I could say like, and now we're actually going to be able to access
- 4:13:58the song count on each of our individual objects. So I could technically come down here and
- 4:14:02say, cashmere dot get song count. And now we'll be able to get the song count off of
- 4:14:11one of those objects. So you'll see now that we can get to just like we did before. So
- 4:14:16you can either just create a static attribute and access it using like song dot song count
- 4:14:22like I did over here, or you could create a getter method like get song count, and then
- 4:14:26the individual objects could access that. And both of those can be pretty useful. Sometimes
- 4:14:32you're gonna want the objects to be able to have access to those static attributes. But
- 4:14:36that's sort of an overview of static attributes and really that static keyword. Actually,
- 4:14:40as we go forward, I'm going to show you some other cool stuff we can do with that static
- 4:14:44keyword. In this tutorial, I'm gonna show you guys how to use static methods in classes.
- 4:14:53A static method is basically just a method that belongs to the class itself. So a lot
- 4:14:59of times when We create methods inside of our C sharp classes. Usually those methods
- 4:15:04will be used on objects. So we can have an object of a specific class and then that object
- 4:15:09can use those methods to, you know, do different things. But a static method in a class is
- 4:15:15a method that belongs to the actual class, which means you don't have to create an object
- 4:15:20of that class in order to use it and access it. And these can be extremely useful. And
- 4:15:25there's a lot of circumstances where you'd want to create classes that have static methods
- 4:15:30inside of them. So I'm going to show you guys basically how that works. Down here, you'll
- 4:15:34see that I have this little line, it just says console dot write line. And then over
- 4:15:38here, I'm saying a math dot square root 144. Right? Essentially, what's happening here
- 4:15:44is there's this class in C sharp, which is called math. And inside of that math class,
- 4:15:51there's a bunch of these methods. Now one thing you'll notice here, when I went to access
- 4:15:55this square root method, inside the math class, I didn't have to create an instance of the
- 4:16:01math class. And normally up to this point in the course, whenever we wanted to use a
- 4:16:05class, or use any of the functionality that a class described, we had to create an object.
- 4:16:10So I would have to say, like math, my math is equal to new math, right, I'd have to create
- 4:16:16an actual instance of the math class like that. But you'll notice that down here, I
- 4:16:22didn't have to do that, right. In fact, when I try to create an instance of the math class,
- 4:16:26I'm getting this error over here. So down here, I can basically just say, math dot square
- 4:16:32root. And I'm able to use this method without having to create an instance of the math class.
- 4:16:38And this is an example of a static method. So a static method, like I said, it's a method
- 4:16:44that belongs to the class, which means we don't have to create an instance of the class
- 4:16:49in order to use it. So I can go ahead and run this program, and we should get back the
- 4:16:52square root of 144. So we should get back 12. I'm going to show you guys how we can
- 4:16:56do this, and how we can basically create something similar to the math class. So up here, I'm
- 4:17:03going to create a new class. And you can either come over here, right click, and go to add,
- 4:17:07and then click New Item. Or we can also go up here, and we can click project and click
- 4:17:13Add class. So I'm going to do it this way. Then over here, you can see I'm just clicking
- 4:17:17on class. And why don't we just call this useful tools. So this is going to be our useful
- 4:17:22tools class. And you'll see over here that I basically just have class useful tools.
- 4:17:28And inside of this class, if I wanted to create static methods, all I have to do is come down
- 4:17:32here and I can just say, like, public static, and I can create a method. So why don't we
- 4:17:37create one called Say hi, and it's going to take one parameter, so it's going to take
- 4:17:42a string name. And then we will basically just have this Say hi. So it's going to console
- 4:17:49dot, write line. And we're just going to print out Hello, name. And actually, whoops, forgot
- 4:17:57to put a return type here. So this should be void. So public static void, say, Hi string
- 4:18:02name, and it just prints out hello to whatever name gets passed in extremely simple method
- 4:18:09over there. So what I could do is I could actually come down here, and I could access
- 4:18:13that method directly without creating an instance of the useful tools class. So down here, I
- 4:18:18can just get rid of this. And we can just go ahead and call this directly. So I could
- 4:18:21say, useful tools dot, say hi. So I didn't actually have to create an instance of the
- 4:18:30useful tools class, all I all I did was just access this method directly. So I can pass
- 4:18:35in my name, like Mike. And now we'll be able to run this method without having to create
- 4:18:40an instance of the useful tools class, you can see over there, everything's running just
- 4:18:45fine. Now, this is an extremely useful thing. So what you could actually do is you could
- 4:18:49create a class over here, and it could just be contained of, you know, these static methods.
- 4:18:56And that's essentially what that math classes that we looked at before, the math class is
- 4:19:00basically just a class that has a bunch of these static methods inside of it, that are
- 4:19:05used to do different things. So if you just have a normal class, you know, maybe a class
- 4:19:10that's modeling some sort of real world entity, you can include static methods inside of there.
- 4:19:15And then the callers don't have to create instances of that class in order to use them.
- 4:19:20Another thing you could do is set up a class that is solely designed for this purpose.
- 4:19:24So for example, that math class, that math class is actually what's called a static class.
- 4:19:30So you'll notice when I tried to create a instance of that math class before I said,
- 4:19:35like math, my math is equal to new math, I'm not actually able to create an instance of
- 4:19:42the math class, you'll see I'm getting an error here. And this is because the math class
- 4:19:47is a static class, which means you can't create an instance of it. But you'll notice with
- 4:19:53these useful tools class I can create an instance of it And it's going to be no problem. So
- 4:20:02like this is actually like a useful tools object. So in certain circumstances, you're
- 4:20:07going to want to do that like, you're going to want to be able to have a class with static
- 4:20:10methods that can have an object created of it. But in situations like with that math
- 4:20:15class, you're not going to want to do that. So what you can do is you can come over here,
- 4:20:19and we could just say, static class useful tools. And now we're not going to be able
- 4:20:25to create an instance of the useful tools class, you'll see down here, I'm getting this
- 4:20:30error, and basically says, cannot declare a variable of static type useful tools. And
- 4:20:33then over here, it says, cannot create an instance of the static class useful tools.
- 4:20:38So that's sort of like two different ways that you can use this. But these static methods
- 4:20:43are extremely useful. And like I said, there's a lot of situations where you're going to
- 4:20:48want to use them, basically, just to define functionality on the class level instead of
- 4:20:52on the individual object level. In this tutorial, I'm going to talk to you guys about inheritance
- 4:21:01in C sharp. Now, inheritance is basically a technique that we can use in C sharp, where
- 4:21:07we can have one class inherit all of the functionality of another class. So I'm going to find something
- 4:21:13called a superclass. And then I can actually create classes that are going to inherit all
- 4:21:20the functionality from that superclass called subclasses. So I'm just going to give you
- 4:21:24guys a very basic introduction into inheritance. This is a huge subject, and there's a lot
- 4:21:29that you can kind of get into with inheritance. So I just want to show you guys like the basic
- 4:21:32concept of what's happening with it. So over here, in my program, I created a class, it's
- 4:21:38called chef.cs. And this is a class that basically like represents a chef in our program. So
- 4:21:45you can see here, class chef, and this chef has three methods that it can use. So the
- 4:21:52first one here is called make chicken. And basically all this does is it prints out,
- 4:21:57the chef makes chicken Right, so the chef can make chicken, the chef can also make salad.
- 4:22:01So it says the chef makes salad and the chef can also make a special dish. So it just says
- 4:22:06the chef makes barbecue ribs. So this is a you know, a pretty cool class. It's obviously
- 4:22:10a very simple class, but it's, you know, it can basically represent a chef in our program.
- 4:22:15So down here in my program.cs, in my main method, I created a chef object, and it's
- 4:22:21just called chef. And then I basically just said chef dot make chicken. So I'm telling
- 4:22:25the chef to make some chicken. And when I run my program, now, it's going to say the
- 4:22:31chef makes chicken, right. So hopefully that makes sense. You know, I just have this chef
- 4:22:35class over here. And I defined some methods in there. And then I created an object of
- 4:22:39the chef class and told it to make chicken. Now I want to show you guys how we can use
- 4:22:44inheritance to make a nother type of class. So let's say that in addition to having just
- 4:22:51a normal chef, like we defined over here, I also wanted to have a more specialized type
- 4:22:56of chef. So I wanted to represent not just not only just a general chef in my program,
- 4:23:01but also a more specialized chef. So let's say I come up here, I'm going to say project,
- 4:23:06add class. And I'm going to create a class called Italian chef. So a Italian chef is
- 4:23:15not just going to be a normal chef, he's going to be a Italian chef. And I'm gonna go ahead
- 4:23:20and create this class. So you'll see over here we have this class Italian chef. Now,
- 4:23:25let's say that this Italian chef can do everything that the normal chef could do. Right. So this
- 4:23:31Italian chef is a really awesome chef. And not only can he just cook regular food, but
- 4:23:36he can also cook like more specifically Italian food. So let's say the Italian chef can do
- 4:23:42everything that the normal chef can do. So the Italian chef can also make chicken, the
- 4:23:47Italian chef can also make salad. And the Italian chef can also make a special dish.
- 4:23:53So the Italian chef is very similar to the normal chef, right, you can do everything
- 4:23:58that the normal chef can do, you can also do some other stuff as well. One thing I could
- 4:24:03do to give the Italian chef all of this functionality is I could just copy. And I can paste all
- 4:24:08of these methods over here, right. And so now the Italian chef is able to make chicken
- 4:24:13able to make salad and able to make a special dish, right. But in C sharp, there's actually
- 4:24:19something that we can do in a situation like this. So anytime you have a class, like Italian
- 4:24:24chef that can do everything. And that has all the same functionality and attributes
- 4:24:29as another class, like this chef class, we can use something called inheritance. And
- 4:24:34inheritance is basically the process of inheriting all of the functionality, all the attributes,
- 4:24:39etc. From a class like chef into a class like Italian chef. So what I can do is I can come
- 4:24:45over here and I can say Italian chef and I can make a colon. And I can just say chef,
- 4:24:51and basically what this means is that the Italian chef is going to inherit all the functionality
- 4:24:56from the chef class. So now the Italian chef can Do everything that the chef class can
- 4:25:01do. And I'm gonna come over here and prove that to you guys. So I'm going to create an
- 4:25:06Italian chef object. So I'm just going to copy this guy. And I'm going to paste this
- 4:25:11down here. And I'm just going to change everything to Italian chef. So I went ahead and change
- 4:25:18this to Italian chef, it's called Italian chef. And we said, new Italian chef. And now
- 4:25:22I'm saying Italian chef dot make chicken, now, on some calling them make chicken method
- 4:25:28on the Italian chef objects. But you'll notice over here in the Italian chef class declaration,
- 4:25:34I don't actually have any methods specified. So I didn't specify like a make chicken method
- 4:25:39or anything like that. But what you'll see is because I inherited all of the functionality
- 4:25:44from the chef class, I'm actually still able to make chicken with the Italian chef. So
- 4:25:50when I run my program, you'll see that it says, The chef makes chicken. And this is
- 4:25:54that normal chef. And then down here, the Italian chef is also able to make chicken,
- 4:26:01because the Italian chef inherited all the functionality from the normal chef. And that's
- 4:26:06kind of cool. So that's basically what you can do. If you have like two classes. And
- 4:26:11in C sharp, we will refer to chef as the superclass. And we would refer to Italian chef as the
- 4:26:16subclass. And the subclass is always going to be inheriting functionality from the superclass.
- 4:26:21So that's just a little terminology. Now we can actually take this a step further. So
- 4:26:27let's say that the Italian chef, in addition to being able to make chicken salad and a
- 4:26:31special dish, can also do something else. So we could add another method in here, I
- 4:26:35could say like public void, and we'll just call this like make pasta. So in addition
- 4:26:40to making all that other stuff, the Italian chef can also make pasta. So now this chef
- 4:26:49can also make pasta and forgot to put an open and closed parentheses there. So if I was
- 4:26:54to come over here to my program, I could say Italian chef dot make pasta, and the Italian
- 4:27:00chef is going to be able to make pasta. So let's run that. And you'll see over here,
- 4:27:05he's making pasta. But what you'll notice is this normal chef can't make pasta. So if
- 4:27:12I tried to make pasta on the normal chef, it's not going to be able to do that. So that's
- 4:27:15gonna throw an error. And that's because we only define make pasta in the subclass, not
- 4:27:21in the superclass. So that's kind of how that's going to work out. And I want to show you
- 4:27:25guys one more thing we can do. So you'll notice over here in my chef class, I have this method,
- 4:27:31it's called make special dish, right. And the chef is making a special dish of barbecue
- 4:27:37ribs, right. So this chef's special dish is going to be barbecue ribs. But let's say that
- 4:27:44the Italian chef doesn't want to make barbecue ribs as a special dish. So let's say that
- 4:27:49the Italian chef's special dish is actually like some other type of dish, and it's not
- 4:27:53a barbecue dish. Well, the problem is when I come over here, and I say, make special
- 4:28:00dish, and I say, make special dish, both of these chefs are going to be making the same
- 4:28:06special dish. So when I run the program, they're both making barbecue ribs. But remember, I
- 4:28:11don't want the Italian chef to make barbecue ribs, I want the Italian chef to make something
- 4:28:16else as their special dish. Well, in a situation like this, I can do something called overriding
- 4:28:22a method. overriding a method basically means that I take one of the methods that I've inherited
- 4:28:27like this make special dish method, and I override it. So I basically come over here
- 4:28:32into this Italian chef class. And I override the functionality of that method. Now in order
- 4:28:38to do this, we're going to have to do one thing over here in the chef class. And we're
- 4:28:42going to have to include this virtual keyword. So you'll notice I already have this in here.
- 4:28:46So up here on these two methods, it says public void make chicken public void make salad.
- 4:28:51But down here on make special dish, I've given it this special keyword, public virtual void
- 4:28:58make special dish. And when we specify this as virtual, basically, that means that this
- 4:29:03method can be overridden in any subclasses. So when I say virtual, it basically means
- 4:29:08that subclasses can change the functionality of this method. So I can just copy this method.
- 4:29:15And I'm just going to go ahead and paste it down here. And you'll see here I'm saying
- 4:29:20public virtual void make special dish, but instead of saying virtual here in the subclass,
- 4:29:25so I'm in my Italian chef class, I'm gonna say, override, and this is basically telling
- 4:29:31C sharp that we're going to override the make special dish method from the superclass, and
- 4:29:37then down here, I can change it so I could say the chef makes instead of barbecue ribs,
- 4:29:41why don't we say like chicken parm. And so now when the Italian chef makes a special
- 4:29:47dish, it's going to be making chicken parm instead of making barbecue ribs. So let's
- 4:29:52go over here into our program. And you'll see I'm still printing out chef dot make special
- 4:29:56dish and Italian chef dot make special dish when I run the pro They're printing out different
- 4:30:02special dishes. So the Italian chef successfully overrode the functionality of one of the methods
- 4:30:09that it inherited from the superclass. And that is extremely useful. So those are three
- 4:30:15very core concepts in inheritance. So the first is just inheriting all the functionality
- 4:30:19from a superclass. The second is extending that functionality. So for example, in addition
- 4:30:25to being able to do everything that the normal chef could do, the Italian chef could also
- 4:30:28make pasta. And then finally, we were able to override one of the functions or one of
- 4:30:34the methods that we inherited using this override keyword on the subclass, and using this virtual
- 4:30:41keyword on the superclass. So that is sort of the basics of inheritance. And like I said,
- 4:30:46there's, you know, inheritance is a very complex subject, and there's a lot lot lot of stuff
- 4:30:52that you can do with it. But this is kind of just an overview of essentially how it
- 4:30:56works. So now you kind of have the concepts down in your mind and you can kind of go forward
- 4:31:00and learn more specific ways to use it. Hey, thanks for watching. If you enjoyed the video,
- 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.