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