C# Full Course for free ๐ต โ Transcript
Full transcript
- 0:00hey what's going on everybody it's you
- 0:02bro hope you're doing well
- 0:03and in this video i'm going to show you
- 0:05all everything you need to know to get
- 0:06started writing code in c-sharp
- 0:08so sit back relax and enjoy the show
- 0:13if you wouldn't mind please like comment
- 0:15and subscribe
- 0:16one like equals one prayer for the
- 0:18youtube algorithm
- 0:20i'm gonna tell you why you need to learn
- 0:22c sharp c
- 0:23sharp is a very flexible language we can
- 0:26use c
- 0:27sharp for all sorts of things console
- 0:29apps web services
- 0:31games and if you're an aspiring game
- 0:32developer c-sharp is definitely a
- 0:34language you want to learn
- 0:36along with c plus but why not learn both
- 0:38and the average salary for a c-sharp
- 0:41developer if you have some experience
- 0:43is 63 000 per year according to
- 0:45glassdoor
- 0:46if that all sounds good let's begin to
- 0:48code with c-sharp we'll need an ide
- 0:51that's an integrated development
- 0:52environment think of it
- 0:53as software that helps us write other
- 0:55software one that i would recommend is
- 0:58visual studio community so you can just
- 1:00google that then head to this url at the
- 1:02top here
- 1:03okay then we are going to download
- 1:05visual studio and just follow the
- 1:07installation procedures
- 1:08so let's open this and run it so
- 1:11continue and here we have a bunch of
- 1:13different packages that we can download
- 1:14you'll want to check
- 1:15dot net desktop development for using c
- 1:18sharp
- 1:19and if you're interested in using unity
- 1:20you might as well just download this
- 1:22package too
- 1:23then we are going to install you have
- 1:25the option to sign in but i'm just going
- 1:27to click
- 1:27not now maybe later you can also pick a
- 1:30color theme for your ide i'm going to go
- 1:32with dark because i like to feel like a
- 1:33pretend elite hacker
- 1:35then start visual studio now with this
- 1:37home screen we'll continue without code
- 1:39i'll show you all how we can create a
- 1:40new project
- 1:41all right now to create a new project
- 1:43we'll go to file
- 1:45new project select c sharp
- 1:48console application next we'll need a
- 1:51name for this project
- 1:52i'll call this my first
- 1:55program next then create
- 1:58all right now this font size is very
- 2:01small you can either zoom in here or
- 2:02down at the bottom or you can change
- 2:04the font as well as the font size by
- 2:06going to tools
- 2:08options environment fonts and colors
- 2:12and you can change the size here what
- 2:13about 18
- 2:16and we should be good and here we have a
- 2:18small program that was created for us
- 2:20automatically
- 2:20all this does is display some output the
- 2:23words hello world
- 2:24to compile and run a program you just
- 2:26have to click this green play button at
- 2:28the top
- 2:30and this will create a console window
- 2:32that displays our output of
- 2:34hello world whatever we have written
- 2:36here within quotes
- 2:38and this line of code is known as the
- 2:40main method it's the entry point for
- 2:42where our program begins
- 2:44and this set of curly braces after the
- 2:46main method is the
- 2:47body of the main method so our program
- 2:50will begin
- 2:50by executing any code at the top of the
- 2:53main method
- 2:53and it will work its way down and
- 2:55execute any subsequent code that follows
- 2:58so this main method is made up of a
- 2:59bunch of words that we don't quite
- 3:01understand yet
- 3:02static void main string args in time
- 3:05we'll learn what each of these words
- 3:06mean but that's more of an advanced
- 3:08topic
- 3:09so a textbook that i read once said to
- 3:11think of the main method as like a
- 3:13magical spell or incantation that we
- 3:15have to say
- 3:16in order for our program to run so let's
- 3:18see what happens if i remove the main
- 3:20method
- 3:20i'll still keep this line though
- 3:22console.writeline hello world
- 3:24and let's try it okay so it looks like
- 3:27there were build errors so we cannot run
- 3:29the program as it is now
- 3:30so we do need that main method and if
- 3:32you are missing it you can just type it
- 3:34in
- 3:34and it should work now now this is a
- 3:37standard writeline method whatever text
- 3:40that we put within quotes will appear
- 3:42within
- 3:42our console window to display as output
- 3:45so let's actually change it right now it
- 3:47says hello world but let's type
- 3:48something else
- 3:49so make sure that this is within double
- 3:51quotes let's change this to
- 3:53i like pizza
- 3:56and then run this again
- 4:00so now my output should say i like pizza
- 4:03and then you can add a second line of
- 4:04output just by writing
- 4:06console.writeline then within
- 4:08parentheses and within double quotes you
- 4:10can write something else
- 4:11like it's really
- 4:14good and now we have two lines of output
- 4:17i like pizza
- 4:18it's really good so to write output to
- 4:21the console window you type
- 4:23console.writeline then within quotes
- 4:25some text that you want to write one
- 4:27thing that you may want to change
- 4:29is the font and the font size of your
- 4:31console window because right now it's
- 4:33very difficult for me to read
- 4:35so to change that right click in the top
- 4:37left corner go to
- 4:38properties then go to font let's
- 4:41increase this to maybe 28
- 4:43you can pick a new font style as well
- 4:45i'll keep it the same
- 4:46then you can also change the color
- 4:48scheme too i'll change the screen text
- 4:50to maybe green then okay all right but
- 4:54now this window is huge so let's change
- 4:56that
- 4:56so again go to the top left corner go to
- 5:00properties
- 5:01then go to layout let's change the width
- 5:04to
- 5:04what about i don't know 50 and the
- 5:06height to
- 5:08let's say 15 i think that should be good
- 5:12okay yeah that's not too bad and here
- 5:14it's actually a lot easier to read
- 5:16i like pizza it's really good oh one
- 5:19last stupid thing that i wanted to show
- 5:20you all is how we can make our console
- 5:22beep type console dot beep
- 5:25parentheses semicolon so then our
- 5:28program is going to beep when it reaches
- 5:30the end
- 5:32it's not really important at all i just
- 5:34thought it would be something fun to
- 5:35throw in at the end of this video so
- 5:37that's how to make your console beep
- 5:38for some reason all right well that's
- 5:41the very basics of c sharp to get you
- 5:43started in the next video we'll cover
- 5:45more on output and comments so i'll post
- 5:49this code to the comment section down
- 5:50below
- 5:51and well that's the very basics to get
- 5:53you started in
- 5:54c-sharp hey you yeah i'm talking to you
- 5:58if you learned something new then help
- 6:00me help you in three easy steps
- 6:03by smashing that like button drop a
- 6:05comment down below
- 6:06and subscribe if you'd like to become a
- 6:08fellow bro
- 6:21[Music]
- 6:30hey what's going on everybody it's you
- 6:32bro hope you're doing well
- 6:34and in this video i'm going to show you
- 6:35all a few different ways in which we can
- 6:37format output in c sharp
- 6:38so sit back relax and enjoy the show
- 6:43if you find this video helpful please
- 6:45remember to like
- 6:46comment and subscribe your support will
- 6:49help keep this channel running
- 6:51all right everybody here's a few
- 6:52different ways in which we can format
- 6:54output in c sharp
- 6:55as we covered in the last video to
- 6:56display something to our console window
- 6:58we would type
- 6:59console.writeline then you need a set of
- 7:02parenthesis then a semicolon at the end
- 7:04you place your message within the
- 7:06parenthesis this is known as a method
- 7:08and we have a semicolon at the end the
- 7:10semicolon is kind of like
- 7:12the period at the end of a sentence when
- 7:14we finish a statement in programming we
- 7:16end it with a semicolon
- 7:18so to display a message within double
- 7:20quotes you would type something
- 7:22so this is known as a string literal
- 7:24we're literally printing something to
- 7:26our console
- 7:27so we kind of covered this in the last
- 7:28video this will display the word
- 7:30hello now there's another way in which
- 7:32we can display output that is just
- 7:34right console dot right parentheses
- 7:37semicolon
- 7:38and within quotes let's display
- 7:40something else what about
- 7:42hey now let's take a look at the
- 7:44difference between the two
- 7:46all right we have hey immediately
- 7:48followed by hello
- 7:49all in the same line in the last video
- 7:51we used two right line statements
- 7:53each string literal had their very own
- 7:56line but in this case
- 7:57it's all one line so with writeline
- 8:00after you finish your string literal
- 8:02it's going to hit enter like we're
- 8:03hitting the enter key and we'll move
- 8:05down to a new line
- 8:06right doesn't do that so you have two
- 8:08different ways in which you can display
- 8:09output to the console window
- 8:11right and right line write does not hit
- 8:13the enter key after the end
- 8:15it doesn't create a new line and
- 8:16rightline does so you can use either one
- 8:18depending on what you need
- 8:19now there is a shortcut to create a
- 8:22rightline statement you type
- 8:23c then hit tab twice and that will auto
- 8:26generate
- 8:26a right light statement for you okay so
- 8:29the next topic to cover is comments we
- 8:31can add a comment to our code
- 8:33and this will not be displayed as output
- 8:35so to write a comment within your code
- 8:37you need
- 8:38two forward slashes and you can see that
- 8:40the color
- 8:41of our text changed to green so i'm
- 8:44going to tell you
- 8:44this is a comment and this has no effect
- 8:48on our output
- 8:49this comment will not be seen all we see
- 8:52here is
- 8:52hey and hello you can also write a
- 8:55multi-line comment
- 8:56you would type forward slash asterisk
- 8:59this is a multi-line
- 9:04comment then to end a multi-line comment
- 9:08you would end it with asterisk then
- 9:10forward slash again
- 9:11and last but not least we have escape
- 9:13sequences we can add an escape sequence
- 9:15to a string to format our output
- 9:17so let's create a writeline statement
- 9:19and type whatever your name is
- 9:22okay now to create an escape sequence we
- 9:25need to
- 9:25add a backslash followed by a certain
- 9:28character
- 9:29depending on what character you select
- 9:31it's going to have a special effect on
- 9:33your string
- 9:34so if i typed backslash t that will add
- 9:37a tab escape sequence before displaying
- 9:39my name
- 9:43all right so we have a tab before my
- 9:45string so let's change this to maybe
- 9:48a backspace i'll add that here so
- 9:51backslash b that will add a backspace
- 9:55and my name is now br
- 9:59code alright so we can also do newline
- 10:04and for new line so my first name is on
- 10:08one line
- 10:09and my last name is on a second line now
- 10:12you may have noticed that there's all
- 10:13this garbage at the end of my console
- 10:15window when my program is done running
- 10:17so one way in which we can hide that at
- 10:19the end of our program type
- 10:21console.read key so basically this will
- 10:24prevent
- 10:25our program from ending until we type a
- 10:27key
- 10:29and it should be hidden now so if i hit
- 10:31the enter key well then my program is
- 10:33going to finish running
- 10:35all right everybody so those are a few
- 10:37ways in which we can format output in c
- 10:39sharp if you can give this video a
- 10:41thumbs up drop a random comment down
- 10:43below
- 10:43and well those are a few ways to format
- 10:46output in c
- 10:47sharp hey what's going on everybody
- 10:50it's bro hope you're doing well and in
- 10:52this video i'm going to explain
- 10:53variables in c sharp so sit back relax
- 10:56and enjoy the show
- 11:00do you all remember back in middle
- 11:01school when we learned about algebra we
- 11:03had to solve an equation to find the
- 11:05value of x
- 11:06and x as some representation of a number
- 11:09well with programming we can create our
- 11:10own variables but we are not limited to
- 11:12just
- 11:13numbers we can store characters words
- 11:16and even these things called booleans
- 11:17which are either true or
- 11:19false so i'm going to show you all how
- 11:21we can create our own variables in c
- 11:23sharp creating a variable takes two
- 11:25steps declaration and initialization
- 11:28to create a variable we first have to
- 11:30declare what type of data we'll be
- 11:31storing
- 11:32if we need to store a number we'll type
- 11:34int short for integer
- 11:36and we'll need a unique name for this
- 11:38specific variable
- 11:40so let's just call this x and then end
- 11:42the statement with a semicolon because
- 11:44with programming semicolons are kind of
- 11:46like
- 11:46periods when you finish a sentence okay
- 11:48so this step
- 11:50was declaration and i'll just add a note
- 11:52here
- 11:53so this step is declaration
- 11:57and now to assign a value we will
- 11:59initialize this variable
- 12:00so to assign this variable x a value
- 12:03type
- 12:04x equals and then some value
- 12:07let's say one two three then semicolon
- 12:11okay so we now have a variable named x
- 12:14and this step is initialization
- 12:18now you can combine these two steps so
- 12:20to do that we would say
- 12:22and let's say y this time equals
- 12:25three two one semicolon so we have two
- 12:28variables
- 12:29x and y so this step right here is both
- 12:32declaration and initialization now these
- 12:35variables
- 12:36behave as the value that they contain so
- 12:38let's display some of these
- 12:40so within a console.writeline statement
- 12:43i'm going to display x and then with a
- 12:46second rightline statement i will
- 12:48display
- 12:49y so console.rightline
- 12:52y and we have one two three
- 12:55three two one now you can use these for
- 12:58like math two we could say
- 13:00like int z equals x
- 13:03plus y and then display whatever
- 13:06z is console.writeline
- 13:10z and the result is
- 13:14444. so these names for these variables
- 13:18really aren't descriptive
- 13:19x y and z we can give some more unique
- 13:21names that describe what kind of value
- 13:24that
- 13:24these variables contain so let's say int
- 13:27edge and i'll assign this variable age a
- 13:30value
- 13:30of 21 and then i can display my edge
- 13:33within a writeline statement or use it
- 13:35for something
- 13:36so let's say console.writeline
- 13:39and then i can display a message along
- 13:42with my age
- 13:43so let's say your age
- 13:47is so this part is within quotes
- 13:50to display the value of my age i will
- 13:53add
- 13:54plus edge make sure age is not within
- 13:57quotes
- 13:58because then we're printing a string
- 14:00literal and not the
- 14:01value contained within edge so this
- 14:03should display
- 14:06your age is 21. so there's more data
- 14:10types than just int
- 14:11this time let's use a double a double
- 14:14is really just a decimal number it's
- 14:16like a floating point number but with
- 14:18more precision
- 14:19there are floats in c sharp but i don't
- 14:21really think they're that important for
- 14:22beginners
- 14:23so with a double we can store a decimal
- 14:26number
- 14:26and int only stores a hole integer
- 14:30so if i try to display like 21.5
- 14:34and then display it this is what happens
- 14:39so we'll run into a build error so with
- 14:41int we can only store
- 14:42whole integers but with a double we can
- 14:44store something that includes a decimal
- 14:47so let's say we have a double height and
- 14:50i'll assign this
- 14:51a perfectly average height of 300.5
- 14:55centimeters i guess and now since this
- 14:57variable is initialized we can use it
- 14:59for something
- 15:00we can change the value or we can use it
- 15:03within a message
- 15:04so let's use console.writeline and i'll
- 15:07display
- 15:08my height within some sort of message
- 15:11let's say your
- 15:12height is space
- 15:15plus height plus another string let's
- 15:19say
- 15:21cm 4 centimeters
- 15:24then let's try it okay your age is 21
- 15:27your height is 300.5 centimeters
- 15:31so a double variable can store a
- 15:34decimal number unlike with integers
- 15:37they only store whole numbers now the
- 15:40next data type is
- 15:41boolean and to declare a boolean
- 15:43variable we type
- 15:44bool then a variable name so booleans
- 15:48can only store
- 15:49true or false so this might be good if
- 15:51you need
- 15:52something that only has like two options
- 15:54like a light switch it's on
- 15:56or it's off if somebody's online that
- 15:58could be true if they're offline
- 16:00that's false or maybe somebody is alive
- 16:03let's say
- 16:03bull alive either true or
- 16:06false but the last time i checked i am
- 16:08alive so i'm going to say that is true
- 16:11and then we can use this boolean
- 16:12variable for something
- 16:14so let's use this within a
- 16:16console.writeline statement
- 16:18console.writeline and what should we say
- 16:22are you alive
- 16:28plus alive
- 16:31are you alive that is true so if i
- 16:34change this to false
- 16:36this would display are you alive
- 16:40that is false so that is a boolean
- 16:44variable it's only true or false
- 16:47so for practice i'll encourage you to
- 16:49try and type something besides true or
- 16:51false like the word pizza
- 16:53we can't shove a whole pizza into a
- 16:55boolean variable it can only be true or
- 16:57false so let's change that back okay the
- 17:00next data type
- 17:01is chart it's a character a single
- 17:04character
- 17:05what about symbol char symbol
- 17:08and we need to place this within single
- 17:11quotes
- 17:11so let's say our character is the at
- 17:14sign
- 17:15and then let's display it
- 17:19console.writeline
- 17:23your symbol is
- 17:27plus symbol
- 17:33your symbol is at sign so make sure that
- 17:36this is within single quotes when you
- 17:38assign a value
- 17:39so with chars we can only assign a
- 17:42single character if i
- 17:43attempted to add like a different symbol
- 17:45too well this wouldn't work then
- 17:48see we're getting an error here so
- 17:50that's where strings come in with a
- 17:52string we can store
- 17:53a series of characters and to declare
- 17:55this we type
- 17:56string with a capital s string let's say
- 17:59name
- 18:00string name and this value is within
- 18:03double quotes
- 18:04so type whatever your first name is and
- 18:06then we can use our name for something
- 18:09let's display a message that says hello
- 18:11whatever your name is
- 18:13console.writeline
- 18:16hello plus name
- 18:21okay hello bro your age is 21 your
- 18:24height is 300.5 centimeters
- 18:27are you alive false your symbol is at
- 18:30sign
- 18:31let's say that we have a username and
- 18:33i'll say
- 18:35string username
- 18:39equals symbol
- 18:42plus name and then i'll display this
- 18:48console.writeline
- 18:52your user name
- 18:56is colon space
- 18:59quotes plus user
- 19:02name whatever you typed here
- 19:07okay your username is at bro
- 19:11those are variables they're containers
- 19:13for values
- 19:15but you can store more than just numbers
- 19:16you can store boolean values
- 19:19characters and strings and a string is a
- 19:22series of characters
- 19:23so if you would like a copy of this code
- 19:25i will post this to the comment section
- 19:27down below
- 19:28and well those are variables in c sharp
- 19:33okay it looks like we're on constants
- 19:35constants are
- 19:36immutable values which are known at
- 19:39compile time
- 19:40and do not change for the life of a
- 19:42program let's say that we have a
- 19:44double variable named pi and this equals
- 19:47the first few digits of pi
- 19:503.14159 if this is not a constant
- 19:53people can actually change the value
- 19:55contained within this variable and that
- 19:57could be a security concern let's say pi
- 19:59now equals 420 and then i will display
- 20:02the value contained within pi
- 20:04so if you do not want people to be able
- 20:06to change this value
- 20:08we can precede the data type of this
- 20:10variable with the
- 20:12const keyword and this variable is now a
- 20:15constant and we can't update this
- 20:16variable
- 20:17now that it's a constant so yeah that's
- 20:19a constant it's an immutable value which
- 20:21is known at compile time
- 20:23and does not change for the life of the
- 20:25program and it adds a little bit of
- 20:27security to our programs because people
- 20:29cannot later change these values
- 20:31so yeah those are constants in c sharp
- 20:35oh yeah typecasting typecasting
- 20:39is the ability to convert a value to a
- 20:41different data type
- 20:42it's useful when we accept user input
- 20:45because when the user types in something
- 20:47it's going to be a string and strings
- 20:49can only do so many things
- 20:50and different data types can perform
- 20:53different things
- 20:54so it may be necessary to convert from
- 20:56one data type to another
- 20:57like for example we can't use strings
- 20:59for any sort of math we would need to
- 21:00convert a string
- 21:02to an integer or double to be able to
- 21:04use a string representation of a number
- 21:07so let's begin let's say we would like
- 21:09to convert a double to an integer
- 21:11let's create a double variable named a
- 21:14and i'll assign this a value of 3.1 for
- 21:17the first few digits of pi
- 21:19so what we could do if we wanted to
- 21:20convert this number to an
- 21:22end we could store this within a
- 21:23separate variable let's say
- 21:25int b equals now to convert this value
- 21:29to an integer we can use the convert
- 21:32classes
- 21:332 and 32 method so convert
- 21:36dot 2 into 32 parentheses semicolon
- 21:39and within the parentheses i'm going to
- 21:41place what i want to convert
- 21:43i'm going to convert the value stored
- 21:45with an a and then assign it to
- 21:47variable b and then let's display this
- 21:49variable with a rightline statement
- 21:51so console.writeline and then i will
- 21:54display whatever b
- 21:55is then let's try it
- 21:58and the value stored within a b is three
- 22:01so if we convert a double to an integer
- 22:04it's going to truncate
- 22:05any decimal portion if there is any so
- 22:07since we wrote this program this way
- 22:09it's not going to get rid of the value
- 22:11within a
- 22:12so if i were to display a it's going to
- 22:14be 3.14
- 22:16all we did is that we took the value
- 22:18stored with an a
- 22:19and created a copy of it then converted
- 22:22it to an integer and stored it within b
- 22:24it's also possible to display the data
- 22:27type of whatever variable that we're
- 22:28working with
- 22:29so after a within our rightline method
- 22:32type
- 22:33dot get type then add a set of
- 22:36parenthesis this is the
- 22:38get type method so this will display
- 22:41instead of the value stored within a
- 22:43it's going to display the data type of
- 22:45whatever a is
- 22:47so the data type of a is currently
- 22:50a double now let's change this to b
- 22:53b dot get type and our variable b
- 22:56is an integer into 32
- 23:01so if you need the data type of a
- 23:03variable follow
- 23:04that variable with dot get type then a
- 23:07set of parentheses
- 23:08this is a built-in method of variables
- 23:11let's convert an into double this time
- 23:14let's create
- 23:14int c equals what about 1 2 3
- 23:18and we'll create double d equals
- 23:22convert dot to double
- 23:26then parenthesis semicolon within the
- 23:28parentheses
- 23:29we will convert c so place that within
- 23:31there
- 23:32and within a writeline statement we will
- 23:34display
- 23:35whatever d is console.writeline
- 23:38d so if your
- 23:42double doesn't contain like a decimal
- 23:44portion it's not going to display
- 23:46like point zero but if i were to add
- 23:48like
- 23:490.1 to the end of this then it will
- 23:51display a decimal portion
- 23:55so 123.1 and the data type
- 23:58of d let's follow this with the get
- 24:02type method so the data type of d
- 24:05is in fact a double okay now let's
- 24:08convert an integer to a string
- 24:10we'll need int what about e e equals
- 24:13three two one and i'll create string
- 24:17f equals convert dot
- 24:20to string are you beginning to see a
- 24:23pattern here
- 24:24then parenthesis semicolon then pass in
- 24:27e
- 24:27so place that within the parenthesis and
- 24:30we will display whatever
- 24:31f is console.writeline
- 24:35the value stored within f and that will
- 24:38be
- 24:41three two one so a string we can't use
- 24:44for any arithmetic but with ins and
- 24:46doubles we can
- 24:47so if we convert an integer to a string
- 24:49we can no longer use this for any math
- 24:52but if we converted a string to an
- 24:53integer then we can use it for any sort
- 24:56of math
- 24:56so let's follow this with get type
- 24:59method
- 25:00f dot get type and the data type
- 25:03of f is in fact a string okay
- 25:06exercise number three let's convert a
- 25:09string
- 25:09to hr hr is just a single character
- 25:14string g equals so a string is always
- 25:18within double quotes what about the
- 25:19dollar sign
- 25:21and a char is always within single
- 25:23quotes we treat strings
- 25:25and chars differently you can store a
- 25:27single character within a string
- 25:29however strings are different from chars
- 25:31there's different things that we can do
- 25:33so let's say char what comes after g e f
- 25:36g
- 25:36h char h equals
- 25:40convert dot to
- 25:43char then of course parenthesis
- 25:47semicolon within the parentheses we will
- 25:49place g
- 25:52and then display whatever h is so
- 25:57console.writeline h
- 26:00so h will still be a dollar sign
- 26:04but we treat it different because it's a
- 26:05character there's different things that
- 26:07we can do
- 26:07then let's get the type of whatever h is
- 26:12and h is hr
- 26:15a single character okay last but not
- 26:18least let's convert
- 26:19a string to a boolean string
- 26:23i equals true within double quotes
- 26:27so this is different from true without
- 26:30double quotes
- 26:31because this is a boolean and right now
- 26:33we're getting an error
- 26:34so represents a boolean true or false a
- 26:37string
- 26:38is always within double quotes so we
- 26:40would treat this string
- 26:41different from this boolean even though
- 26:44they say the same thing
- 26:45okay then let's convert this string to a
- 26:48boolean
- 26:49type bool not cool bull
- 26:52j equals convert
- 26:56dot to boolean
- 26:59parentheses semicolon pass in i
- 27:03then we will display whatever j is
- 27:06console.writeline j and this will
- 27:09display
- 27:12true then let's get the type of this
- 27:14variable
- 27:15so j dot get type method
- 27:19and the data type of j is a boolean now
- 27:23all right everybody so that's how to
- 27:25type cast we can convert
- 27:27a value to a different data type and
- 27:29it's really useful when we accept user
- 27:31input because
- 27:32it's always going to be a string and
- 27:34there's different things that we can do
- 27:36with different data types so it may be
- 27:38necessary to convert from one data type
- 27:40to another
- 27:41so if you would like a copy of all this
- 27:43code i will post this to the comment
- 27:44section down below
- 27:45and well that's typecasting in c sharp
- 27:49hey what's going on everybody it's you
- 27:52bro hope you're doing well and in this
- 27:53video i'm going to show you all how we
- 27:55can accept some user input in c
- 27:57sharp so sit back relax and enjoy the
- 28:00show
- 28:02all right here's how we can accept some
- 28:04user input now when we do accept user
- 28:06input
- 28:07it's of the string data type so let's
- 28:09create a variable
- 28:10to hold and store our input let's create
- 28:13a variable
- 28:14named name and we will ask the user for
- 28:17what their name is
- 28:18now to read some user input type
- 28:22console.readline
- 28:24then parentheses semicolon so when the
- 28:26user
- 28:27types in something then hits enter we
- 28:29will take
- 28:30that user input and store it within this
- 28:32variable name
- 28:33which we can then use for something so
- 28:35within a writeline statement
- 28:37let's use the user's name for something
- 28:39hello
- 28:40plus name and let's take a look
- 28:45okay now to enter in some user input
- 28:47we'll have to type this into the console
- 28:49directly
- 28:50however it is good practice to let the
- 28:52user know what we want them to type in
- 28:54exactly because
- 28:55how's the views are supposed to know
- 28:57that they're supposed to type in their
- 28:58name right here
- 28:59so what we'll do is preceding our
- 29:01readline statement let's create a prompt
- 29:03to tell the user what we want them to
- 29:04type in
- 29:05so within a rightline statement let's
- 29:07say what's
- 29:08your name and then try this again
- 29:14okay what's your name now the user knows
- 29:16that they have to type in their name so
- 29:18type in your first name hit enter hello
- 29:21whatever your first name is
- 29:22okay this time let's accept a user's age
- 29:26and then we'll typecast the user's age
- 29:28into an integer variable
- 29:30so let's copy what we have paste it
- 29:34console.writeline what's your
- 29:37edge and we'll create a new variable of
- 29:40the int data type
- 29:41named age now we'll need to cast this
- 29:44user input into
- 29:45an integer so to do that we would type
- 29:48convert dot 2 and 32
- 29:52parenthesis semicolon now take this
- 29:55statement without the semicolon
- 29:58cut it and then paste it within the
- 30:00parentheses so what we're doing
- 30:02is accepting some user input then
- 30:04converting it
- 30:05to an integer that we're storing within
- 30:07this variable edge
- 30:08and then we will display the user's age
- 30:12console.writeline
- 30:13u r plus age plus
- 30:18years old then let's run this again
- 30:23so our program is actually going to wait
- 30:25at each prompt until the user types
- 30:27something in
- 30:28so what's your name bro let's say that
- 30:30i'm 21 i'll hit enter
- 30:32hello bro you are 21 years old
- 30:35now one issue that you may run into is
- 30:37if the user types in something that's
- 30:39non-numeric and we try and convert it
- 30:42so let's try this again what's your name
- 30:44bro
- 30:45and instead of an age what if i type in
- 30:47a word like pizza
- 30:48this will cause an exception an
- 30:50exception will interrupt our program
- 30:52and this specific exception is a format
- 30:56exception input string was not in a
- 30:58correct format
- 30:59so there will be a future video on
- 31:02handling exceptions which will prevent
- 31:03the program from pausing and being
- 31:05interrupted
- 31:06but that's a future video in the
- 31:08meantime we'll just have to be sure that
- 31:10we're entering in the correct user input
- 31:12well okay then everybody that's how to
- 31:14accept some user input in c
- 31:16sharp i will post this code to the
- 31:18comment section down below
- 31:19and well yeah that's how to accept user
- 31:22input in c
- 31:23sharp hey everyone we're going to cover
- 31:26some basic arithmetic
- 31:27in c sharp so sit back relax and enjoy
- 31:30the show
- 31:33everyone i thought we could just cover
- 31:34some basic arithmetic real quick
- 31:36let's say we have a variable friends and
- 31:38i will assign this a value of what about
- 31:40five
- 31:41and within a writeline statement i will
- 31:43display the value stored within friends
- 31:45so of course
- 31:46friends will have a value of five but
- 31:49what if i need to
- 31:50update this like increment it i could
- 31:52say friends
- 31:53equals friends and we make a friend so
- 31:56we could say friends equals friends plus
- 31:58one
- 31:58because remember the value stored within
- 32:00friends is five
- 32:02this is no different from saying friends
- 32:03equals five plus one which would be six
- 32:06so let me display the value stored
- 32:08within friends and of course
- 32:09that's six of course you can change this
- 32:11to a different number too you could say
- 32:13friends equals friends plus two then the
- 32:16amount of friends that we have
- 32:17is seven so there is a shortcut of
- 32:20writing this too because this can be
- 32:22somewhat tedious
- 32:23what you could say is friends plus
- 32:25equals
- 32:26then the amount that you want to
- 32:27increment this variable by
- 32:29so if i need to increment friends by two
- 32:32i could say friends
- 32:33plus equals two and that would do the
- 32:35same thing that's like a shortcut
- 32:37so of course that would be seven so
- 32:39there's a third way to increment and
- 32:42decrement two
- 32:42this method is found within loops you
- 32:44could say friends
- 32:46plus plus however this will only
- 32:48increment a
- 32:49variable by one so if friends is
- 32:51initially five
- 32:52then we say plus plus well then friends
- 32:54will equal six
- 32:55so there's three different ways to
- 32:57increment a variable
- 32:58this way is found within loops this is
- 33:00the standard way and that's kind of the
- 33:02shortcut this line right here
- 33:04okay so let's decrement friends so we
- 33:06could say friends
- 33:08equals friends minus one or we could say
- 33:12friends minus equals one or
- 33:15friends minus minus so if we combine all
- 33:19these the amount of friends that we have
- 33:20is two let's work on some multiplication
- 33:24if we need to double the amount of
- 33:26friends that we have we would say
- 33:27friends equals friends times
- 33:31we use an asterisk for multiplication
- 33:33and programming
- 33:34times two then the amount of friends
- 33:36that we have
- 33:38is 10 and the shortcut would be
- 33:43friends asterisk equals 2
- 33:46and that would do the same thing now
- 33:49division
- 33:50let's say that steve breaks up our group
- 33:52of friends
- 33:53so we could say friends equals friends
- 33:55divided by two and for division we use a
- 33:58forward slash
- 33:59now pay attention to this our current
- 34:01number of friends is five but what if we
- 34:03divide this by two
- 34:05well you would think that we get 2.5
- 34:07right well you're wrong we end up with
- 34:092.
- 34:09that's because friends is an integer we
- 34:12can only store whole numbers
- 34:13we can't store that decimal portion that
- 34:150.5
- 34:16so if this was a double well then that
- 34:19would be 2.5
- 34:21so that's referred to as integer
- 34:23division and that's something you have
- 34:24to look out for
- 34:25so if friends equals friends divided by
- 34:282
- 34:28well then the shortcut would be friends
- 34:31forward slash
- 34:32equals 2 and that would do the same
- 34:33thing and then remember i'm storing this
- 34:36within an integer so we will truncate
- 34:38that decimal portion and we end up with
- 34:402.
- 34:41and our last operator for this topic is
- 34:43the modulus
- 34:45also known as the remainder operator now
- 34:48for
- 34:48this example i'm going to change the
- 34:49amount of friends that we have to 10
- 34:51so we can find the remainder of any
- 34:53division but i'm going to store this
- 34:55within a new
- 34:56variable int remainder
- 34:59and to find the remainder we could say
- 35:01friends
- 35:02then modulus which is represented by a
- 35:04percent sign
- 35:05than 3 so 10 divided by 3
- 35:08will have a remainder and we will store
- 35:10that remainder within a new variable
- 35:13so let's pretend that we have a total of
- 35:15ten people and we have to break up into
- 35:17groups of three so there's going to be
- 35:20one person remaining
- 35:21and i will just display that remainder
- 35:23of one but if this was in groups of two
- 35:26so friends modulus two well ten divides
- 35:29by two evenly
- 35:30so there will be no remainder and the
- 35:32value will be zero
- 35:34so if you need to find the remainder you
- 35:36can always use this modulus
- 35:38the remainder operator and it's also
- 35:40pretty good for finding if numbers are
- 35:42even or odd too so that's always a plus
- 35:44as well
- 35:45alright everybody so that is some basic
- 35:47arithmetic i will post this code to the
- 35:49comment section down below
- 35:50and well that's some basic arithmetic in
- 35:53c sharp
- 35:54hey everyone in this video i'm going to
- 35:56show you all a few useful methods of the
- 35:59math class
- 36:00so sit back relax and enjoy the show
- 36:05alright everybody so here's a few useful
- 36:07methods of the math class that you might
- 36:09be interested in
- 36:10let's begin with creating a double
- 36:12variable named x
- 36:13and i'll assign a value of three now the
- 36:16first useful method
- 36:17is the pow method that will raise a
- 36:20value to a certain power
- 36:22and we'll store this within a new
- 36:24variable let's say a
- 36:25double a equals then to access the math
- 36:28class type
- 36:30math with a capital m dot and here's a
- 36:33whole bunch of methods they have access
- 36:34to
- 36:35so we are looking for the pow method
- 36:38right here
- 36:38then add a set of parentheses semicolon
- 36:41now within the parentheses the first
- 36:42value is your base so the base will be x
- 36:45and that is three
- 36:46and what power do we want to raise x to
- 36:49let's raise x to the second power
- 36:51and then display whatever our result is
- 36:53a so within the right line statement
- 36:55i'm going to display a so here is the
- 36:58result
- 36:583 to the power of 2 is
- 37:01drum roll please 9.
- 37:04so if we need 3 to the power of 3 that
- 37:07would be
- 37:08you know x whatever you're working with
- 37:10that's the base
- 37:11comma 3. so 3 to the power of 3
- 37:15is 27. okay let's try square root i'll
- 37:19turn this line into a comment
- 37:20and we'll create double b equals
- 37:24then again to access methods from the
- 37:25math class type
- 37:27math with a capital m dot and we are
- 37:30looking for
- 37:30square root that is all the way down
- 37:34here
- 37:34s q r t parenthesis semicolon within the
- 37:38parentheses
- 37:39we will find the square root of x
- 37:43and then store it within b console.write
- 37:47line
- 37:48b
- 37:50and that would be 1.73 something
- 37:53something something something
- 37:55so that is the square root method let's
- 37:57move on
- 37:58let's create a double c and we'll find
- 38:01the absolute value of x
- 38:04so math dot a b s
- 38:08so the absolute value is how far away
- 38:11something is
- 38:11from zero so if you have a negative
- 38:13number like negative three
- 38:15well it's going to turn that into a
- 38:17positive number
- 38:19so the absolute value of negative 3 oh
- 38:22we need to display c
- 38:24so the absolute value of negative 3 is
- 38:27in fact
- 38:273 so that is the absolute value method
- 38:31let's move on rounding so double
- 38:35let's see we're on d equals math.round
- 38:38and we can round a number
- 38:40pay attention to the capitalization too
- 38:42okay
- 38:43now let's change x to 3.14
- 38:47and then we will round x so place that
- 38:49within the parentheses of the round
- 38:51method
- 38:52and we will display d
- 38:56and 3.14 rounded is 3.
- 38:59now you can always round a number up by
- 39:02using the ceiling method
- 39:03let's create double e equals
- 39:07math.ceiling all right then we will
- 39:10place
- 39:11x within this method and display
- 39:14e so 3.14
- 39:18rounded up using the ceiling method is
- 39:214.
- 39:22alternatively there is a floor method
- 39:24that will always round down
- 39:26and i'm just going to copy this so
- 39:28double
- 39:30f math dot floor and
- 39:33let's change this to 3.99
- 39:36and display f
- 39:39so 3.99 rounded down is three
- 39:43okay now we'll need two variables for
- 39:46this next example
- 39:48so let's create double y equals
- 39:52what about five okay so we have the
- 39:55max method we can find the maximum value
- 39:58of two values or variables so
- 40:01double g equals math.max
- 40:06and within the parentheses we want to
- 40:08compare x and y
- 40:10and separate each of these with a comma
- 40:13so this will find
- 40:14the max between x and y and store the
- 40:17maximum number within
- 40:18g and then we should display g
- 40:22so the maximum between x and y is y
- 40:26and that contains five then we have min
- 40:30so double h math.min
- 40:36console.writeline h so the minimum
- 40:39between
- 40:40x and y is x 3.99
- 40:44all right everybody so those are a few
- 40:46useful methods of the math class
- 40:48i will post this code to the comments
- 40:49section down below and well
- 40:51yeah those are a few useful methods of
- 40:53the math class
- 40:55hey what's going on everybody it's bro
- 40:57hope you're doing well and in this video
- 40:58i'm going to explain how we can generate
- 41:00some random numbers in c
- 41:01sharp so sit back relax and enjoy the
- 41:04show
- 41:07all right people here's how to generate
- 41:09some random numbers
- 41:10just so you know these are not true
- 41:11random numbers but rather
- 41:13pseudo random numbers and they're fairly
- 41:15darn close so to create some random
- 41:17numbers we'll need to instantiate a
- 41:19random object
- 41:20type random with an uppercase r random
- 41:23with lowercase r
- 41:24equals new random parentheses semicolon
- 41:28so the name of our random object is
- 41:30random really you couldn't name this
- 41:32anything but i usually just call it
- 41:33random
- 41:34and we can use this object to generate
- 41:36random numbers and there's a few
- 41:38built-in methods of this random object
- 41:40to access them add a dot and we'll focus
- 41:43on
- 41:43the next method and next double method
- 41:46next we'll generate a random whole
- 41:48integer so we'll want to specify a range
- 41:51this by itself will generate a random
- 41:53number between i believe
- 41:54zero and just over two billion so we'll
- 41:57probably want to set a range
- 41:58let's pretend that we're rolling a six
- 42:00sided dice i need a random number
- 42:02between
- 42:03one and six so i can set that range
- 42:05within the parentheses
- 42:06so the minimum would be one comma and
- 42:09the max would be
- 42:10technically seven because the second
- 42:12number is going to be exclusive
- 42:14so this will give us a random number
- 42:16between one and
- 42:18six just specify the range and we'll
- 42:20store this within a variable
- 42:22int num equals random.next a random
- 42:25number between
- 42:26one and a six all right then let's
- 42:29display this
- 42:30console.writeline num and we should have
- 42:33a random number between
- 42:34one and six
- 42:38and we got six all right then you can
- 42:40always make adjustments to this too like
- 42:42you can add
- 42:42i don't know 100 and that should give us
- 42:45a number between
- 42:46101 and 106. so that's always an option
- 42:50too
- 42:51okay now what if you need to roll a
- 42:5320-sided dice like we're playing
- 42:54dungeons and dragons we use polyhedral
- 42:57dice
- 42:57so that would be 1 comma 21 and this
- 43:00will give us a random number between
- 43:021 and 20 and we rolled an 8.
- 43:05now there is a next double method which
- 43:08will generate a random
- 43:09decimal number so let's say double num
- 43:13equals random dot next double
- 43:18and the random number will be between 0
- 43:21and
- 43:211. so we have 0.701
- 43:26something something something something
- 43:28so if you need a random
- 43:29decimal or random double number you can
- 43:31use random.next double
- 43:34otherwise if you need a random hold
- 43:36integer you're better off using the next
- 43:38method of random
- 43:40now if you need let's say three random
- 43:42numbers we can keep on reusing the same
- 43:44random object we only need to
- 43:45instantiate it once
- 43:47let's say we're going to roll three dice
- 43:49so three
- 43:50six sided dice let's copy what we have
- 43:53and paste it twice
- 43:54so we have num1 num2 num3
- 43:58then let's display these
- 44:00console.writeline num1
- 44:02num2 and num3
- 44:06so we will have three random numbers and
- 44:09the result
- 44:10is three six and four well okay then
- 44:13everybody that
- 44:14is how to generate random numbers in c
- 44:16sharp
- 44:17i will post this code to the comment
- 44:19section down below if you can give this
- 44:21video a thumbs up
- 44:22drop a random comment down below and
- 44:24well that's how to generate random
- 44:25numbers
- 44:26in c sharp hey what's going on people
- 44:29it's
- 44:29bro hope you're doing well and in this
- 44:31video we're going to create a small
- 44:32program to find the hypotenuse of a
- 44:34right triangle
- 44:35in c sharp so sit back relax and enjoy
- 44:38the show
- 44:41everyone i thought for practice we could
- 44:43create a small program to find
- 44:45the hypotenuse of a right triangle this
- 44:47will be more or less just for practice
- 44:49so we'll need some prompts
- 44:51let's ask for sides a and b let's begin
- 44:54with a
- 44:55enter side a and then we'll store
- 44:59the user's input within a double double
- 45:02a equals and we'll convert the user
- 45:05input
- 45:06to a double convert to
- 45:09double then within the parenthesis
- 45:15console.read line
- 45:17then another set of parenthesis make
- 45:19sure there is a semicolon at the end too
- 45:21okay then for side b we can just copy
- 45:24what we have
- 45:25change any instance of a to b enter
- 45:28side b double b
- 45:32and we'll have to calculate whatever c
- 45:34is double
- 45:35c math dot square root
- 45:40then we need to multiply a times a
- 45:46that's effectively a to the power of two
- 45:49plus
- 45:51b times b and
- 45:54let's display the result
- 45:58console.writeline
- 46:00the hypotenuse
- 46:04is plus c
- 46:09and let's give it a go okay enter
- 46:12side a how about three enter side b four
- 46:16the hypotenuse is five well everybody
- 46:18that was
- 46:19a very small practice program if you
- 46:22would like a copy of this code i will
- 46:24post this to the comment section down
- 46:25below
- 46:26if you can give this video a thumbs up
- 46:28drop a random comment down below
- 46:30and well that is a very small sample
- 46:32program
- 46:33in c sharp hey what's going on everybody
- 46:37it's you bro hope you're doing well and
- 46:38in this video i'm going to show you all
- 46:40some useful string methods in c
- 46:42sharp so sit back relax and enjoy the
- 46:44show
- 46:47all right everybody here's a few useful
- 46:49string methods that you may be
- 46:51interested in
- 46:52so to begin we'll need a string to work
- 46:53with or a variable containing a string
- 46:56so why don't we create a variable named
- 46:58full name to store your own full name
- 47:01now to access some methods available to
- 47:04strings we'll need
- 47:04to type a string or a variable
- 47:06containing a string
- 47:08then follow this with a dot and there
- 47:09should be a prompt for
- 47:11a whole bunch of methods that we have
- 47:13access to i'll show you some of the more
- 47:15useful ones
- 47:16like two upper full name dot to upper
- 47:18then to invoke this
- 47:20add a set of parentheses then a
- 47:22semicolon to finish this statement but
- 47:24then we'll need to reassign this
- 47:26full name equals full name to upper
- 47:29then we can display our full name and
- 47:31when i run this
- 47:32my name should all be uppercase
- 47:35just like that okay now alternatively
- 47:38there's
- 47:39two lower it's the same process as
- 47:41before
- 47:42but change the method to lower
- 47:46and my name should all be lowercase okay
- 47:49so that's
- 47:49two upper and two lower let's move on
- 47:52for this next example we'll need a phone
- 47:54number
- 47:55so let's create a string named phone
- 47:58number equals and make up some phone
- 48:01number and be sure to include some
- 48:03dashes
- 48:03one two three four five six seven eight
- 48:06nine zero
- 48:07okay what i'm going to do is replace
- 48:09these dashes with a different character
- 48:12so to do that we'll need to use the
- 48:14replace method
- 48:15so phone number dot replace
- 48:20so within the set of parentheses we'll
- 48:22need the character we're going to
- 48:24replace
- 48:25and another character we're going to
- 48:27replace the old character with
- 48:29so i would like to replace all dashes
- 48:32with a forward slash so separate each of
- 48:35these characters with a comma
- 48:38so this will replace my phone number the
- 48:40dash is within the phone number
- 48:42with forward slashes phone number equals
- 48:44phone number dot replace
- 48:46and let's display our phone number with
- 48:49a right line statement
- 48:51so all of these dashes have been
- 48:53replaced with forward slashes
- 48:55now if you replace these with no
- 48:57characters just an empty set
- 48:59of quotes that will effectively remove
- 49:02those dashes within the phone number
- 49:04which could have some uses too so that
- 49:06is the replace
- 49:07method available to strings next we have
- 49:10the insert method we can insert
- 49:12a character or a string at a given index
- 49:14within a pre-existing string
- 49:16let's say i would like to take my full
- 49:18name and turn it into some sort of
- 49:20username
- 49:21by preceding my first name with the at
- 49:23sign
- 49:24so to do that i can use the insert
- 49:26method i'll create a new
- 49:28variable for this example let's say
- 49:29string username
- 49:31equals full name dot insert
- 49:34and with the insert method we will list
- 49:36an index
- 49:38and what we would like to insert so
- 49:40computers they always start at zero
- 49:42the beginning would be zero that would
- 49:43be the first index
- 49:45so at the beginning index zero i would
- 49:47like to insert
- 49:48an at sign and this will create a new
- 49:51variable called username
- 49:53so i will display that within a
- 49:55rightline statement
- 49:57console.writeline username and my
- 50:00username is now at whatever my full name
- 50:03was
- 50:04now this doesn't necessarily have to be
- 50:06a single character we could
- 50:07say like mr dot and precede
- 50:11my full name with mr and my username is
- 50:15now mr bro code okay so that is the
- 50:18insert
- 50:18method what i'm about to show you next
- 50:21technically
- 50:21isn't a method it's a property but it's
- 50:23found within the same place as methods
- 50:25for
- 50:26strings so to access the length property
- 50:29of a string type a string dot link
- 50:33and you do not add a set of parenthesis
- 50:35afterwards that is done with methods
- 50:37so the length property will return the
- 50:40given amount of characters within a
- 50:41string
- 50:42so i'm going to display this within a
- 50:44rightline statement
- 50:45if you need this you can always like
- 50:47assign this to a variable or something
- 50:49but that might be overkill for this
- 50:51lesson so console.writeline
- 50:53the length of my full name and my full
- 50:56name
- 50:56has eight characters in it one potential
- 51:00use of the length property of strings
- 51:02is that let's say that somebody's
- 51:03creating a username and there's a max 12
- 51:05characters
- 51:06well you can get the length of whatever
- 51:07they type in and check to see if it's
- 51:09more than 12 characters
- 51:10this next method is the substring method
- 51:13and it can be a little complex
- 51:15basically speaking we can take a section
- 51:17from an original string
- 51:19and create an entirely new string but we
- 51:21have to specify
- 51:22a position and how many characters you
- 51:24would like to extract
- 51:26from that given position so we'll create
- 51:29two new strings
- 51:30from full name first name and last name
- 51:32using the substring method
- 51:34so let's declare string first name
- 51:38equals full name dot
- 51:41substring okay so within the substring
- 51:45we're going to list
- 51:46an index and the amount of characters
- 51:48afterwards we would like to include
- 51:50within our substring
- 51:51so i would like to take the first three
- 51:54characters from my full name
- 51:55so the index computers always start with
- 51:57zero of the beginning of my string
- 51:59would be zero and i'm going to take
- 52:01three characters
- 52:02after this index so if i was to display
- 52:06my first name now it's going to be
- 52:09bro the first three characters of my
- 52:11full name
- 52:12now let's do the same thing with last
- 52:14name so let's copy what we have
- 52:17change first name to last name but now
- 52:20we have a different index
- 52:22so i would like my substring to begin
- 52:24where the c is
- 52:25so let's count what the index is going
- 52:27to be this would be 0
- 52:291 2 3 four so let's change
- 52:32zero to four and how many characters
- 52:35would we like to include within our
- 52:36substring
- 52:38one two three four so four
- 52:41comma four and that is my last name so i
- 52:43will display that within
- 52:45a writeline statement first name and
- 52:47last name
- 52:49so that is the substring method we can
- 52:51take a section or a slice
- 52:53from an original string and create an
- 52:55entirely new string from it
- 52:57all right everybody those are a few
- 52:59useful string methods
- 53:01but not all of them so if you would like
- 53:03a complete list you can always type a
- 53:05string
- 53:06dot and there should be a pop-up for all
- 53:08of the different string methods you can
- 53:09access
- 53:10and if you highlight one of these
- 53:11there's a description of what they do
- 53:13exactly
- 53:14i only showed you a few of the more
- 53:15useful ones for beginners
- 53:17so if you can give this video a big fat
- 53:19thumbs up drop a random comment down
- 53:21below and well
- 53:22those are some useful string methods in
- 53:25c sharp
- 53:28all right people if statements an if
- 53:31statement
- 53:32is a basic form of decision making if
- 53:34some condition that we specify
- 53:36is true we can execute a block of code
- 53:39if it's not true we can do something
- 53:40else entirely
- 53:42so let's write a small program where we
- 53:44will ask a user to enter in their edge
- 53:46and depending on their age we can write
- 53:48an if statement that checks to see if
- 53:50somebody is 18 years or older
- 53:52let's say that a user is signing up for
- 53:54a credit card and you have to be 18
- 53:55years or older
- 53:56so if their age is greater than or equal
- 53:59to 18
- 54:00we will display a message that says
- 54:02you're now signed up if they're less
- 54:03than 18 we can do something else
- 54:06so to write an if statement we just
- 54:07write if then parentheses to hold
- 54:10some sort of condition which we would
- 54:11like to evaluate followed by a set of
- 54:13parentheses
- 54:14so if some condition that we set within
- 54:16the parentheses is true
- 54:18we will execute this block of code if
- 54:20it's not true we skip over it entirely
- 54:22so let's check to see if age is greater
- 54:24than or equal to 18
- 54:26then we will display something console
- 54:29that right line you are now signed up
- 54:34and let's try it please enter your age
- 54:37let's say that i'm 21 i hit enter
- 54:39boom you are now signed up now what if i
- 54:41enter in an edge that's less than 18
- 54:43let's say that i'm 12 years old and i'm
- 54:45trying to sign up for a credit card
- 54:46well nothing appears to happen and
- 54:48that's normal so if
- 54:50this condition evaluates to be true we
- 54:52execute this block of code
- 54:54if it's false we skip over it entirely
- 54:56as if it never existed
- 54:58now we can take a different course of
- 54:59action if our above condition
- 55:02is false we can perform some other block
- 55:04of code
- 55:05so let's display a message that says you
- 55:06must be 18 years or older to sign up
- 55:09you must be 18 plus to sign
- 55:12up so if this condition is true we do
- 55:15this
- 55:15if it's false we do this so let's try it
- 55:18again
- 55:19please enter your age let's say that i'm
- 55:2012 i hit enter you must be 18 plus to
- 55:23sign up
- 55:24so if this is true do this if it's not
- 55:27true
- 55:27do this now between the if and else
- 55:30statements you can add an else if
- 55:31statement
- 55:32if there's more things they want to
- 55:33check before reaching the else block
- 55:35so the else block is kind of like a last
- 55:38resort if all of buff statements
- 55:40evaluate to be false
- 55:42let's check to see if somebody's age is
- 55:45less than zero else if age is less than
- 55:48zero
- 55:49then we will display a custom message
- 55:51such as
- 55:53you haven't been born yet
- 55:57please enter your age i am negative 12
- 56:00years old
- 56:01you haven't been born yet with these
- 56:03else if statements you can add as many
- 56:05as you want between
- 56:06if and else let's add another so i will
- 56:08copy what we have
- 56:10let's check to see if somebody's age is
- 56:12greater than 100
- 56:13if it is then we'll say i don't know
- 56:15you're too old to sign up
- 56:16that's kind of mean i guess but it
- 56:18serves the purpose of this example
- 56:20so you are too old to sign
- 56:23up now we're going to run into one issue
- 56:27okay please enter your age i am 101
- 56:29years old
- 56:30you are now signed up now the reason
- 56:33that this block of code executed and not
- 56:35this one is that we're just going down
- 56:37an order starting from the top
- 56:39since age which was 101 is technically
- 56:42greater than or equal to 18
- 56:44this condition evaluated to be true and
- 56:47we executed
- 56:48this block of code and skipped
- 56:49everything else after it and continued
- 56:51on to the rest of the program
- 56:53so if we first want to check to see if
- 56:55somebody's age is greater than
- 56:56100 we should probably put that within
- 56:59the if statement and check that first
- 57:01because age is technically greater than
- 57:03or equal to 18 if we set age to be
- 57:06101 else if we will check to see if age
- 57:09is greater than or equal to 18
- 57:11then we just have to change these lines
- 57:13of text around
- 57:15so you are too old to sign up so the
- 57:18order of
- 57:19these if statements and else if
- 57:20statements does matter
- 57:22and let's try it again okay please enter
- 57:24your age i am 101 years old
- 57:26you are too old to sign up all right now
- 57:29in this next example let's ask a user
- 57:31for their name instead of their age
- 57:33let's change int age to string name
- 57:36and we do not need to convert this
- 57:38because it's already a string
- 57:40so to check to see if somebody does not
- 57:42enter in some user input
- 57:43we can check to see if our variable name
- 57:48equals an empty set of quotes so this is
- 57:51an empty string
- 57:52now the reason that we're using two
- 57:53equal signs is that this is a comparison
- 57:56operator
- 57:57if you just use one that is an
- 57:59assignment operator
- 58:00c sharp thinks you're attempting to
- 58:02assign name with an empty string
- 58:05so we're comparing if these two are
- 58:07equal so that's why we use
- 58:09double equals so if name is equal to an
- 58:11empty string
- 58:12that means that somebody skipped over
- 58:14entering in some user input
- 58:16so let's yell at them you did
- 58:19not enter your name so
- 58:22if our name is not empty we can display
- 58:26maybe hello whatever the user's name is
- 58:28so
- 58:30hello plus name
- 58:34and let's try it
- 58:37okay please enter your name i'm just
- 58:38going to hit enter you did not enter
- 58:40your name
- 58:41let's run this again and take it
- 58:43seriously this time please enter your
- 58:45name
- 58:45bro hello bro now there's another way in
- 58:48which we could
- 58:49write this so instead of using this
- 58:51double equal sign we can use
- 58:53exclamation point equal sign that means
- 58:56not equals
- 58:57if name does not equal an empty string
- 58:59then we will display
- 59:01hello name else we will display
- 59:05you did not enter your name so we're
- 59:07checking to see if name is
- 59:08not equal to an empty string and if
- 59:10that's true display hello
- 59:12name okay please enter your name i'll
- 59:15hit enter
- 59:16you did not enter your name let's try it
- 59:18again please enter your name
- 59:20bro hello bro all right everybody those
- 59:23are if statements they're a very basic
- 59:26form of decision making we can check to
- 59:27see if some condition
- 59:29is true if it is true then we can do
- 59:31something if not we can do something
- 59:32else
- 59:33so if you would like a copy of both
- 59:35examples from this video i will post
- 59:37that code to the comment section down
- 59:39below
- 59:39and well those are if statements and see
- 59:42you sharp
- 59:43hey what's going on everybody it's you
- 59:45bro hope you're doing well
- 59:47and in this video i'm going to explain
- 59:49switches in c-sharp
- 59:50so sit back relax and enjoy the show
- 59:55all right everybody switches a switch is
- 59:58an efficient alternative
- 59:59to writing many else if statements i
- 1:00:01wrote a small program
- 1:00:03we'll ask a user what day is it today
- 1:00:05like what day of the week
- 1:00:06and we'll store their input within a
- 1:00:08variable named day
- 1:00:10if day equals monday we'll write it's
- 1:00:12monday and we have else if statements
- 1:00:14for every day of the week that follows
- 1:00:16after and an else block at the end
- 1:00:18day is not a day they type in something
- 1:00:20that's obviously not a day
- 1:00:22using else if statements back to back
- 1:00:24like this really isn't that efficient
- 1:00:26and if a veteran programmer sees you do
- 1:00:28something like this
- 1:00:29they'll probably call you a noob a few
- 1:00:31else if statements is okay but
- 1:00:33this is somewhat excessive so let's
- 1:00:35rewrite this program
- 1:00:36so that we use a switch instead here's
- 1:00:39how to create one we'll keep our prompt
- 1:00:40too
- 1:00:41so switch parentheses curly braces
- 1:00:44within the parentheses we will place
- 1:00:47what we would like to examine
- 1:00:48we will examine our day variable against
- 1:00:51many cases
- 1:00:52and if there is a match then we will
- 1:00:54execute some block of code
- 1:00:56so to write a case write case then some
- 1:00:59value
- 1:01:00like monday then end this with a colon
- 1:01:03so anything indented after our case is
- 1:01:06part of the block of code for this case
- 1:01:09so if we have a match if day equals case
- 1:01:12monday then let's write it's monday
- 1:01:17then on the next line add a break to
- 1:01:19exit the switch
- 1:01:20okay so that's one case let's write a
- 1:01:23case for each
- 1:01:24day of the week and i'll just fast
- 1:01:25forward this footage
- 1:01:31and our switch is now complete although
- 1:01:33i did not add that line from the else
- 1:01:35statement quite yet we'll take care of
- 1:01:37that a little bit later
- 1:01:38so let's run this just to test it so
- 1:01:41what day is it today let's say that it's
- 1:01:42sunday
- 1:01:43enter it's sunday nice one thing that
- 1:01:46we're missing although it's optional
- 1:01:48is a default case if there's no matches
- 1:01:50what we want to do
- 1:01:52so currently there is no default case
- 1:01:54within the switch
- 1:01:55so what day is it today if i write
- 1:01:57something incorrect like
- 1:01:58pizza well the switch technically
- 1:02:00doesn't do anything
- 1:02:01so if there are no matching cases let's
- 1:02:04do something
- 1:02:05and we type default colon space if there
- 1:02:07are no matching cases we will perform
- 1:02:10this block of code at the end so let's
- 1:02:12write something
- 1:02:13day plus is
- 1:02:16not a day and then break
- 1:02:21and let's try it again so this is kind
- 1:02:23of like our else statement
- 1:02:24okay what day is it today this time
- 1:02:27let's say it's
- 1:02:28birthday birthday is not a day
- 1:02:32all right everybody those are switches
- 1:02:34they're efficient alternatives to using
- 1:02:37many else if statements
- 1:02:38if you would like a copy of this code i
- 1:02:40will post this to the comment section
- 1:02:42down below
- 1:02:43if you can give this video a thumbs up
- 1:02:45drop a random comment down below
- 1:02:47and those are switches in c sharp
- 1:02:52all right logical operators we can use
- 1:02:54logical operators to check to see if
- 1:02:56more than one condition
- 1:02:58is true or false we'll use two logical
- 1:03:01operators
- 1:03:02and as well as or and is represented by
- 1:03:04two ampersands
- 1:03:06and or is represented by two vertical
- 1:03:08bars we'll create a small program
- 1:03:10to ask the user what the temperature is
- 1:03:12outside and depending on what the user
- 1:03:13types in will display a custom message
- 1:03:16so we'll need to work with ranges of
- 1:03:18temperatures so let's begin we'll ask
- 1:03:20the user
- 1:03:20for some input so console.writeline
- 1:03:24what's the temperature outside
- 1:03:27and you can write this in fahrenheit or
- 1:03:29celsius but i'll use celsius
- 1:03:31okay then we'll store our input within a
- 1:03:34double
- 1:03:34variable named temp short for
- 1:03:36temperature and we'll need to use
- 1:03:39console.read
- 1:03:40line and then we'll need to convert this
- 1:03:43to a double
- 1:03:44using convert dot 2 double method so
- 1:03:47convert
- 1:03:47dot 2 double then within the parenthesis
- 1:03:51place your redline method within here
- 1:03:56okay now we need to check to see if our
- 1:03:59temperature falls within a certain range
- 1:04:01using an if statement and using the and
- 1:04:03logical operator
- 1:04:04that will help us with that so let's
- 1:04:06check to see if our temp
- 1:04:08is greater than or equal to 10 degrees
- 1:04:11celsius and if our temperature
- 1:04:14is less than or equal to what about 25
- 1:04:18so both of these conditions must be true
- 1:04:21in order for us to execute this
- 1:04:23if statement if one of them is false we
- 1:04:26will skip over this if statement
- 1:04:27so this condition needs to be true and
- 1:04:30this condition needs to be true so our
- 1:04:33temperature has to fall within
- 1:04:34that range so we will display a custom
- 1:04:37message
- 1:04:38like it's warm outside it's not hot
- 1:04:42it's not cold it's just warm let's try
- 1:04:44it
- 1:04:45okay so what if the temperature was like
- 1:04:4935 degrees celsius well nothing appears
- 1:04:52to happen we did not execute
- 1:04:54this if statement even though this
- 1:04:56condition is true
- 1:04:57this condition must also be true because
- 1:04:59we're using the and logical operator
- 1:05:02so let's try this again and type in a
- 1:05:04temperature between that range
- 1:05:05what's the temperature outside let's say
- 1:05:07that it's 15 degrees celsius
- 1:05:10it's warm outside now using the or
- 1:05:12logical operator
- 1:05:14only one of these conditions needs to be
- 1:05:16true in order for this entire expression
- 1:05:18to evaluate to be true unlike with the
- 1:05:21analogical
- 1:05:22operator both conditions must be true so
- 1:05:24let's check for some extreme
- 1:05:25temperatures using an else if statement
- 1:05:28else if temp let's say is less than or
- 1:05:32equal to
- 1:05:33negative 50 or temp
- 1:05:36is greater than or equal to 50
- 1:05:40then we will display do
- 1:05:43not go outside
- 1:05:46let's try it okay what's the temperature
- 1:05:49outside
- 1:05:50let's say that it's negative 52 degrees
- 1:05:52celsius do not go outside
- 1:05:54so even though this condition is true
- 1:05:57and
- 1:05:57this one is false using the or logical
- 1:06:00operator
- 1:06:00only one of these conditions needs to be
- 1:06:02true in order for our entire expression
- 1:06:05to evaluate to be true so let's do the
- 1:06:07same thing with a
- 1:06:08very high temperature this time what's
- 1:06:11the temperature outside let's say that
- 1:06:13it's like a kajillion degrees celsius
- 1:06:15maybe our sun explodes or something do
- 1:06:18not go outside
- 1:06:19well okay then everybody those are
- 1:06:21logical operators we can use them to
- 1:06:23check to see if more than one condition
- 1:06:25is true or false so if you would like a
- 1:06:28copy of this code i will post this to
- 1:06:30the comment section down below and well
- 1:06:32those are logical operators in c sharp
- 1:06:38all right while loops a while loop will
- 1:06:40repeat some code while some condition
- 1:06:42remains true we'll write a small program
- 1:06:45where we will ask a user to enter in
- 1:06:47their name
- 1:06:47if they do not enter in anything we will
- 1:06:49keep on prompting them to enter their
- 1:06:51name they have to enter something
- 1:06:53in order to escape the loop so this is
- 1:06:55what we'll do to begin with
- 1:06:56let's create a prompt to ask a user for
- 1:06:58their name
- 1:06:59i'll use a write statement instead of a
- 1:07:01writeline statement
- 1:07:03and let's say enter your name
- 1:07:06and then we will declare a string
- 1:07:08variable named name
- 1:07:13the console.readline and then store that
- 1:07:15within variable name
- 1:07:17and then at the end of our program we'll
- 1:07:19say hello
- 1:07:20plus name okay so after running this
- 1:07:24what if a user doesn't type in anything
- 1:07:26like they just had enter
- 1:07:28hello and then there's nothing here so
- 1:07:30what if we
- 1:07:31try and force the user to enter in
- 1:07:33something we can write this code within
- 1:07:35a while loop
- 1:07:36so after we accept user input for the
- 1:07:39first time
- 1:07:39let's check that user input and we'll
- 1:07:42use a while loop
- 1:07:43our condition will be while name is
- 1:07:45equal to
- 1:07:46an empty string we will prompt the user
- 1:07:49again
- 1:07:49to enter in something for their name and
- 1:07:52we do not need to declare
- 1:07:53name twice name equals console.readline
- 1:07:56so let's try this again our condition is
- 1:07:58we will continue
- 1:07:59this while loop this section of code as
- 1:08:02long as name
- 1:08:03equals an empty string if they just hit
- 1:08:05enter so let's try it
- 1:08:06okay enter your name i'm going to hit
- 1:08:08enter enter your name no
- 1:08:10enter your name no and to your name no
- 1:08:12okay i'll type in something
- 1:08:14hello bro so with a while loop you'll
- 1:08:17need some way to eventually
- 1:08:18exit the while loop otherwise that is
- 1:08:20known as an infinite loop
- 1:08:22and here's an example of an infinite
- 1:08:23loop i'm going to say
- 1:08:25while one is equal to one
- 1:08:28and then let's display something else
- 1:08:30such as
- 1:08:33help i'm stuck in
- 1:08:36an infinite loop so basically we have no
- 1:08:40way to
- 1:08:41change this condition so it's always
- 1:08:43going to be true
- 1:08:44and we will repeat this code forever so
- 1:08:46let's try it again
- 1:08:50okay enter your name help i'm stuck in
- 1:08:53an infinite loop
- 1:08:55so with a while loop you'll eventually
- 1:08:57want some way to eventually
- 1:08:58exit the while loop now to optimize this
- 1:09:01code we don't technically need
- 1:09:03this right statement here because we
- 1:09:04will ask that within the while loop
- 1:09:07and i'm going to declare string name and
- 1:09:10assign this
- 1:09:11an empty string so this would work the
- 1:09:13same as before as well
- 1:09:15so enter your name i do not type in
- 1:09:17anything and we are prompted again to
- 1:09:19type in our name
- 1:09:20and i'll type in something well okay
- 1:09:22then everybody those are while loops
- 1:09:24they will repeat
- 1:09:25some code while some condition remains
- 1:09:28true
- 1:09:29and it will do so infinitely until this
- 1:09:32condition becomes false
- 1:09:34if you would like a copy of this code i
- 1:09:35will post this to the comment section
- 1:09:37down below
- 1:09:38if you can give this video a thumbs up
- 1:09:40drop a random comment down below
- 1:09:41and well those are wild loops in c sharp
- 1:09:47okay
- 1:09:48for loops a for loop will repeat some
- 1:09:51code a
- 1:09:52finite amount of times unlike with while
- 1:09:55loops a while loop will execute an
- 1:09:57infinite amount of times as long as its
- 1:09:59condition remains true
- 1:10:01for loops are limited so let's create a
- 1:10:03for loop to i don't know
- 1:10:05count to ten so let's create a for loop
- 1:10:07by typing four
- 1:10:08parentheses curly braces and within the
- 1:10:11for loop the parentheses of the for loop
- 1:10:14there's three separate statements we're
- 1:10:16going to fill in the first
- 1:10:17is that we need some sort of counter or
- 1:10:19index to keep track of which iteration
- 1:10:22we're on within the for loop
- 1:10:23so let's declare an index int index
- 1:10:26and i will assign zero to begin with so
- 1:10:29people usually just shorten
- 1:10:31index to simply i for short
- 1:10:34so that is the first statement the
- 1:10:36second statement is our condition
- 1:10:38when do we want to stop so let's stop
- 1:10:41when
- 1:10:41i is greater than or equal to 10.
- 1:10:44so we'll just say i is less than 10.
- 1:10:48so it's kind of like we'll continue this
- 1:10:50for loop as long as
- 1:10:51i is less than 10 and once we hit 10
- 1:10:54we'll stop
- 1:10:55and then the third statement is how much
- 1:10:57do we want to
- 1:10:58increment or decrement our index by so
- 1:11:01you can write
- 1:11:02i plus equals one to increment by one or
- 1:11:05another variation is just
- 1:11:07i plus plus so after each iteration
- 1:11:09we'll increment i
- 1:11:11our counter our index by one and during
- 1:11:14each iteration
- 1:11:14let's display whatever i is so after
- 1:11:17running this code
- 1:11:18we will execute this for loop ten times
- 1:11:21and we will count
- 1:11:22the numbers zero through nine so if we
- 1:11:25want 1 through 10 we can change our
- 1:11:27index
- 1:11:27to 1. into i equals 1 and we will
- 1:11:31continue this for loop as long as i
- 1:11:33is less than or equal to 10 and then
- 1:11:36our for loop will execute a total of ten
- 1:11:39times and count the numbers one through
- 1:11:41ten
- 1:11:42and you can also skip iterations too
- 1:11:44like we can increment
- 1:11:45our index i by two i plus equals
- 1:11:49two
- 1:11:52now this for loop is going to execute
- 1:11:54five times we're incrementing our index
- 1:11:57by two during each iteration
- 1:11:59let's change this to three i plus equals
- 1:12:02three
- 1:12:04and this will execute four times one 4 7
- 1:12:0710. okay let's do something a little bit
- 1:12:10different let's count down let's pretend
- 1:12:12that we're counting down to a new year
- 1:12:14so we'll start at 10 and count down to
- 1:12:16one then display happy new year
- 1:12:18so four parentheses curly braces
- 1:12:22we'll set index i to equal 10 to begin
- 1:12:25with
- 1:12:26we'll continue this as long as i is
- 1:12:28greater than
- 1:12:29zero then we will decrement i by one
- 1:12:33during each iteration so you can use
- 1:12:35minus minus or
- 1:12:36minus equals one either variation works
- 1:12:39okay so
- 1:12:40after each iteration we will display i
- 1:12:43then when we exit our for loop we will
- 1:12:46display happy new year within a
- 1:12:47rightline statement
- 1:12:48so happy new year
- 1:12:52let's try it okay 10 9 8 7
- 1:12:556 5 4 3 2 1 happy new year
- 1:12:59so everybody those are for loops they
- 1:13:01will repeat
- 1:13:02some code a finite amount of times
- 1:13:05unlike with a while loop a while loop
- 1:13:07will execute
- 1:13:08some code infinitely as long as its
- 1:13:10condition remains true
- 1:13:12a for loop is limited in that regard
- 1:13:14well everybody
- 1:13:15those are for loops if you can give this
- 1:13:18video a big fat thumbs up
- 1:13:19drop a random comment down below and
- 1:13:21well those are for loops
- 1:13:23in c sharp hey what's going on everybody
- 1:13:25it's you bro hope you're doing well
- 1:13:27and in this video we're going to use
- 1:13:29nested loops to create a program to draw
- 1:13:31a rectangle for us made out of a certain
- 1:13:33symbol that we choose
- 1:13:34so sit back relax and enjoy the show
- 1:13:39all right welcome back so nested loops a
- 1:13:42nested loop is a
- 1:13:44loop that's inside of another loop there
- 1:13:47is an outer loop
- 1:13:48and an inner loop and both of these
- 1:13:50combined are nested
- 1:13:51loops so the uses they vary they're used
- 1:13:54a lot in sorting algorithms so i thought
- 1:13:56let's create a program where we will
- 1:13:58draw a rectangle
- 1:14:00we'll have the outer loop in charge of
- 1:14:02counting the rows
- 1:14:03and the inner loop in charge of counting
- 1:14:05the columns and we will let the user
- 1:14:07choose a symbol that they would like to
- 1:14:09create their rectangle of
- 1:14:10so we'll need to accept some user input
- 1:14:13so console.writeline
- 1:14:15let's ask how many rows how many
- 1:14:18rows actually let's make this a write
- 1:14:23statement
- 1:14:24okay and then we will store this within
- 1:14:26int rows
- 1:14:28equals then we will need to convert some
- 1:14:30user input
- 1:14:31convert 2 and 32
- 1:14:34because user input's always a string
- 1:14:38then within the parenthesis console.read
- 1:14:44okay let's ask for columns
- 1:14:47so let's copy this paste it
- 1:14:51how many columns int
- 1:14:55columns then let's ask the user for a
- 1:14:59symbol to use
- 1:15:05what symbol or character
- 1:15:09and let's use a string i guess you can
- 1:15:12use a char as well
- 1:15:14string symbol and we do not need to
- 1:15:16convert this because it's already a
- 1:15:18string
- 1:15:19okay we'll have an outer loop in charge
- 1:15:21of counting the rows
- 1:15:23and the inner loop is in charge of
- 1:15:24counting the columns
- 1:15:27so four then we need our index
- 1:15:30by the way it doesn't matter if you use
- 1:15:32a while loop or a for loop
- 1:15:34it's just the concept of having a loop
- 1:15:36inside of another loop i just so happen
- 1:15:37to be using
- 1:15:38a for loop for the outer loop and a for
- 1:15:41loop for the inner loop
- 1:15:43so into i equals zero
- 1:15:47we will continue this as long as i is
- 1:15:50less than
- 1:15:51our rows because we're letting the user
- 1:15:53choose how many rows
- 1:15:55then we will increment i by 1 during
- 1:15:58each iteration
- 1:15:59now we need an inner for loop and let's
- 1:16:01just copy this
- 1:16:03now one naming convention with the index
- 1:16:06of the inner for loop since i is already
- 1:16:08taken
- 1:16:09what people usually use for an index for
- 1:16:11the inner loop is
- 1:16:12j because j comes after i i guess
- 1:16:15so into j equals zero j
- 1:16:18is less than columns than
- 1:16:22j plus plus okay now what we'll do
- 1:16:25within
- 1:16:25the inner for loop is display our symbol
- 1:16:29so console dot right make sure you use
- 1:16:32right and not right line we will display
- 1:16:35our symbol
- 1:16:37so this inner loop is in charge of the
- 1:16:39columns
- 1:16:40the outer loop is in charge of the rows
- 1:16:42basically how this is going to work
- 1:16:44is that once we enter our outer for loop
- 1:16:47we will immediately enter our inner for
- 1:16:49loop in order to complete
- 1:16:51one iteration of the outer for loop we
- 1:16:54have to finish
- 1:16:55all iterations of the inner for loop and
- 1:16:58once we finish
- 1:16:59all iterations of the inner for loop we
- 1:17:01can complete
- 1:17:02one iteration of the outer for loop and
- 1:17:05during the next iteration
- 1:17:06we have to do it all over again so on
- 1:17:08the next iteration we would have to
- 1:17:10complete
- 1:17:10all the iterations for the inner for
- 1:17:12loop so
- 1:17:14this will display a rectangle made out
- 1:17:16of a certain symbol
- 1:17:18however there is one thing missing and
- 1:17:19i'll show you how many rows
- 1:17:21let's say four how many columns five and
- 1:17:24what symbol
- 1:17:25uh what about the i don't know that sign
- 1:17:29all right so this ended up in one long
- 1:17:32row so after we complete our iterations
- 1:17:35for the inner for loop we should
- 1:17:36probably move down to the next row
- 1:17:38kind of like a typewriter we finish one
- 1:17:40line then we move down to the next row
- 1:17:42so let's use an empty rightline
- 1:17:44statement just to move down to the next
- 1:17:45line
- 1:17:46so if we try this again okay four rows
- 1:17:50five columns let's use an at sign there
- 1:17:53we go there's a rectangle we have
- 1:17:55one two three four five columns
- 1:17:58and one two three four four rows
- 1:18:01so basically a nested loop is really
- 1:18:03just a loop that's inside of another
- 1:18:05loop
- 1:18:06when you'll encounter it it really
- 1:18:07varies but they're used a lot in sorting
- 1:18:09algorithms
- 1:18:10and this was a small program just to
- 1:18:13practice
- 1:18:13working with nested loops alright so if
- 1:18:16you want a copy of this code i will post
- 1:18:18this to the comment section down below
- 1:18:19if you liked this video give it a thumbs
- 1:18:21up drop a random comment down below
- 1:18:23and that is an example of nested loops
- 1:18:26in c
- 1:18:26sharp hey everyone it's you bro hope
- 1:18:30you're doing well
- 1:18:30and in this video we're going to create
- 1:18:32a number guessing game
- 1:18:33in c sharp so sit back relax and enjoy
- 1:18:37the show
- 1:18:40all right let's create a number guessing
- 1:18:42game we will randomly generate some
- 1:18:44random number between
- 1:18:45a set minimum and maximum and we will
- 1:18:48let the user guess as to what the number
- 1:18:50is
- 1:18:50if the number is too high or too low we
- 1:18:52will give a hint
- 1:18:53and the user can keep on guessing and
- 1:18:55we'll keep track of how many attempts it
- 1:18:57takes
- 1:18:58so we'll need a random object to
- 1:19:00generate some random numbers
- 1:19:02random random equals new random and
- 1:19:04there's a few other things we'll declare
- 1:19:06as well at the top
- 1:19:07boolean play again
- 1:19:11so if the user would like to play again
- 1:19:13we can keep this as true
- 1:19:15if they would like to quit we can set
- 1:19:16this to be false
- 1:19:18int min this will be the minimum value
- 1:19:21let's have the user guess a number
- 1:19:23between 1 and 100
- 1:19:26int max but you can feel free to adjust
- 1:19:29these
- 1:19:30max equals 100 and
- 1:19:33guess i will declare this but not yet
- 1:19:36assign a value
- 1:19:38and number
- 1:19:41and int guesses
- 1:19:44okay we'll create a while loop and we
- 1:19:46will let the user decide if they would
- 1:19:48like to play again
- 1:19:50so our condition while play again
- 1:19:53equals true but since this itself is a
- 1:19:57boolean variable you don't technically
- 1:19:58need
- 1:19:59this equals true portion you could just
- 1:20:01say while play again
- 1:20:03because this by itself would evaluate to
- 1:20:05be true or false
- 1:20:07now if our user decides they would like
- 1:20:09to play again we should probably reset
- 1:20:11everything
- 1:20:12so let's take our guess and set the
- 1:20:15sequel to zero
- 1:20:17guesses this is the number of attempts
- 1:20:20that equals zero as well and let's
- 1:20:23generate a
- 1:20:24new random number number equals random
- 1:20:27dot next and within the parentheses of
- 1:20:31the next method we can list
- 1:20:33our minimum and maximum but remember
- 1:20:35that this number is
- 1:20:37exclusive but since we have these
- 1:20:38variables we can place these here
- 1:20:40instead
- 1:20:41so min and max so this should be max
- 1:20:45plus one
- 1:20:46this will generate a random number
- 1:20:47between one and one hundred
- 1:20:49because like i said this number is
- 1:20:51exclusive
- 1:20:52okay then we will keep on asking the
- 1:20:55user to type in a number like guess
- 1:20:57again
- 1:20:58so we'll need a nested while loop while
- 1:21:01and our condition is guess
- 1:21:05does not equal our number that is
- 1:21:08randomly generated
- 1:21:10so let's ask the user to type in a
- 1:21:12number
- 1:21:16guess a number
- 1:21:19between
- 1:21:22and this next part is going to get a
- 1:21:23little funky plus
- 1:21:26min plus i'll add a dash
- 1:21:31plus max
- 1:21:34plus a colon
- 1:21:38okay then we will take variable guess
- 1:21:42equals and we'll need to convert some
- 1:21:44user input convert
- 1:21:46to int 32 and within the parentheses
- 1:21:52console dot read line
- 1:21:58all right then we will display whatever
- 1:22:01our guess is
- 1:22:02console.writeline
- 1:22:05guess colon space plus
- 1:22:09guess and now we need to check our guess
- 1:22:12versus the number and we can use
- 1:22:16an if else if statement
- 1:22:19if and the condition is
- 1:22:23guess is greater than number
- 1:22:26we will display a hint console that
- 1:22:29right line
- 1:22:32let's say that guess
- 1:22:36plus is too
- 1:22:39high
- 1:22:43elsif
- 1:22:46and the condition is guess
- 1:22:49is less than number
- 1:22:53let's copy this guess is
- 1:22:56too low and then
- 1:23:00if guess equals number we escape the
- 1:23:02while loop
- 1:23:03oh and we do need to increment our
- 1:23:05guesses by one
- 1:23:07after each guess so let's place that
- 1:23:10right about here right before we
- 1:23:14do another iteration of our while loop
- 1:23:16guesses
- 1:23:17plus plus to increment our guesses by
- 1:23:20one
- 1:23:21now when we exit the while loop that
- 1:23:23means that we eventually got the right
- 1:23:25number
- 1:23:26so we will display the number with a
- 1:23:28rightline statement
- 1:23:30console.writeline number
- 1:23:33colon space plus
- 1:23:36number then we will display
- 1:23:40you win console.writeline
- 1:23:43you win
- 1:23:46and let's display how many guesses it
- 1:23:49took how many attempts
- 1:23:51guesses call in space plus
- 1:23:54guesses then we should probably
- 1:23:58let the user decide if they would like
- 1:23:59to play again and actually let's declare
- 1:24:02one more variable string response
- 1:24:06and then we will assign this within the
- 1:24:09while loop
- 1:24:10let's do that here we'll reset it
- 1:24:14response equals an empty string
- 1:24:20okay then we'll have the user type in if
- 1:24:24they would like to play again
- 1:24:26would you like to play
- 1:24:30again y slash n
- 1:24:33so they would have to type in either y
- 1:24:35or n y to play again
- 1:24:37for yes and for no
- 1:24:40response equals
- 1:24:44console dot read
- 1:24:47line
- 1:24:51and let's make this uppercase in case
- 1:24:53they type in something that's lowercase
- 1:24:55response equals response dot to
- 1:24:59upper
- 1:25:03so if our response
- 1:25:08is equal to a capital y
- 1:25:12play again equals true
- 1:25:18else play again
- 1:25:21equals false then when we
- 1:25:25escape our outer while loop that means
- 1:25:27we are done playing this game
- 1:25:29so let's find where that ends right here
- 1:25:33okay let's say thanks for playing oh
- 1:25:36looks like i typed that already
- 1:25:37so just type this in i guess all right
- 1:25:42we're good so let's run this
- 1:25:46guess a number between 1 and 100 what
- 1:25:48about 50 that's right in the middle
- 1:25:50right
- 1:25:5050 is too high what about 25 25
- 1:25:54is too high what about 12 12 is too high
- 1:25:57this is a very low number i guess
- 1:25:59what about six six is too high g's all
- 1:26:02right
- 1:26:02three three is too low our number is
- 1:26:05between three
- 1:26:06and six how about four
- 1:26:09four is too low five all right finally
- 1:26:13so it took seven guesses let's play
- 1:26:15again i think i can do better
- 1:26:17guess a number between one and one
- 1:26:19hundred let's go with 50 again
- 1:26:2150 is too low so something between 50
- 1:26:24and 100 let's go with 75
- 1:26:2675 is too high uh what about
- 1:26:3163
- 1:26:3363 is too high uh 57
- 1:26:3857 is too low how about 60.
- 1:26:42all right 60. that took five guesses
- 1:26:45this time
- 1:26:46would you like to play again no thanks
- 1:26:48for playing
- 1:26:49well everybody that was a very simple
- 1:26:52number guessing game in csharp i'll post
- 1:26:54all of this code to the comment section
- 1:26:56down below if you would like a copy for
- 1:26:58yourself
- 1:26:59if you can give this video a thumbs up
- 1:27:01feel free to drop a random comment down
- 1:27:02below
- 1:27:03and well that is a very simple number
- 1:27:06guessing game
- 1:27:06in c sharp hey what's going on everybody
- 1:27:09it's you bro hope you're doing well
- 1:27:11and in this video we're going to create
- 1:27:12a game of rock paper scissors
- 1:27:14in c-sharp so sit back relax and enjoy
- 1:27:17the show
- 1:27:20all right everybody let's create a game
- 1:27:22of rock paper scissors what i like to do
- 1:27:24is declare all the things that we'll
- 1:27:26need at the top of our program
- 1:27:28we'll need a random object to generate a
- 1:27:30random number for the computer's choice
- 1:27:32so we'll say that one equals rock two
- 1:27:35equals paper
- 1:27:36and three equal scissors so we'll need
- 1:27:38to create a random object
- 1:27:40random random equals new random and
- 1:27:42we'll create a
- 1:27:43boolean variable named play again and i
- 1:27:47will set this
- 1:27:48to equal true so if the player decides
- 1:27:51to
- 1:27:51exit they would not like to play another
- 1:27:53game we can set this to be false
- 1:27:55and we'll need a string variable to hold
- 1:27:58the player's choice
- 1:27:59i will declare this but not yet assign
- 1:28:01it so this will store
- 1:28:02rock paper or scissors and we'll need a
- 1:28:05string variable for the computer's
- 1:28:07choice as well
- 1:28:08okay that's all of the different
- 1:28:10variables that we'll need so let's
- 1:28:12create a while loop
- 1:28:13while play again equals
- 1:28:17true now if we're working with boolean
- 1:28:19variables we technically don't need
- 1:28:21this equals true portion because this by
- 1:28:24itself would evaluate to be true or
- 1:28:26false
- 1:28:27so you can just say while play again and
- 1:28:29that would work as well
- 1:28:30and then if the player decides to quit
- 1:28:32we can change that to false
- 1:28:33okay now let's ask for some user input
- 1:28:36console
- 1:28:36without right enter
- 1:28:40rock paper
- 1:28:43or scissors
- 1:28:47and we will store the user's input
- 1:28:49within string
- 1:28:51variable player player equals console.rt
- 1:28:56line now strings are case sensitive what
- 1:29:00i'm going to do is take the player's
- 1:29:02input and make it all uppercase just so
- 1:29:04that everything is consistent
- 1:29:06player equals player dot
- 1:29:09two upper i guess you could make this
- 1:29:12all lowercase two
- 1:29:15okay now let's display the player's
- 1:29:17choice with a right line statement
- 1:29:20so what if the player decides to choose
- 1:29:22something that isn't rock paper or
- 1:29:23scissors
- 1:29:24we would want some way to enforce that
- 1:29:31so let's say that i choose the gun and
- 1:29:34that's a valid response so we should
- 1:29:36probably prevent the player from doing
- 1:29:37so
- 1:29:38so what we'll do is use a nested while
- 1:29:41loop
- 1:29:42so while then surround this section of
- 1:29:44code within
- 1:29:45a set of curly braces and we will
- 1:29:47continue to prompt the user to type in
- 1:29:49something that is valid a valid choice
- 1:29:52so this is our condition while player
- 1:29:55does not equal rock
- 1:29:59and i'm just going to copy this
- 1:30:03player does not equal paper
- 1:30:07and player
- 1:30:10does not equal scissors
- 1:30:14so we'll need to assign player and
- 1:30:16computer a value
- 1:30:17let's do that at the top of our while
- 1:30:19loop for play again
- 1:30:21so player equals an empty string
- 1:30:24and computer equals an empty string so
- 1:30:28this is kind of nice when we do play
- 1:30:29another round we can reset player and
- 1:30:31computer
- 1:30:33okay let's try this
- 1:30:38enter rock paper or scissors i pick the
- 1:30:41gun
- 1:30:42enter rock paper or scissors how about a
- 1:30:44bomb
- 1:30:45fist nope okay so we have to choose
- 1:30:48either rock paper or scissors
- 1:30:49what about rock cool so that is a valid
- 1:30:52response
- 1:30:54okay so let's get rid of this right line
- 1:30:55statement we won't really need it
- 1:30:57anymore
- 1:30:58now we'll need to generate a random
- 1:30:59number for the computer player
- 1:31:01one two or three and i will use a switch
- 1:31:06so we will place the random number that
- 1:31:09is generated within the parenthesis
- 1:31:11we could say like i don't know
- 1:31:14int random num
- 1:31:18equals random dot
- 1:31:21next and the range is going to be one
- 1:31:24through four so remember that this
- 1:31:27number is exclusive
- 1:31:28this will generate a random whole number
- 1:31:30between one and
- 1:31:31three okay then what i'm going to do is
- 1:31:34technically we don't need this portion
- 1:31:36like we could place random num within
- 1:31:38switch
- 1:31:39but what i like to do is kind of shorten
- 1:31:41and condense my code so i'm going to
- 1:31:43take
- 1:31:43this method of random.next and place it
- 1:31:46within the switch itself
- 1:31:47and that would work as well so let's
- 1:31:49examine some cases
- 1:31:51case 1 that will be rock
- 1:31:55computer equals rock
- 1:31:58all uppercase then break
- 1:32:02and we'll need case two in case the
- 1:32:04random number is two
- 1:32:05case two computer
- 1:32:09equals paper
- 1:32:13then break and
- 1:32:16case three
- 1:32:20computer equals
- 1:32:24scissors
- 1:32:28then break what i'm going to do after
- 1:32:31our switch
- 1:32:32is display the player's choice along
- 1:32:34with the computer's random choice
- 1:32:36so let's use a redline statement so
- 1:32:38player colon space
- 1:32:41plus variable player then let's create
- 1:32:44another right line for the computer
- 1:32:49computer plus computer
- 1:32:54let's run this
- 1:32:58okay i will choose rock
- 1:33:02player rock computer scissors let's play
- 1:33:04again
- 1:33:05i will choose paper player paper
- 1:33:08computer paper
- 1:33:09and scissors player scissors computer
- 1:33:13scissors okay now we just need to
- 1:33:15examine our choices and see who wins
- 1:33:18that's the next step
- 1:33:19now what we'll do at this point is
- 1:33:21create a switch to examine
- 1:33:22the player's choice versus the
- 1:33:24computer's choice and we can decide who
- 1:33:26wins
- 1:33:27so let's create a switch and we will
- 1:33:29examine
- 1:33:30the variable player against many
- 1:33:33different cases
- 1:33:35so the first case will be rock
- 1:33:41and for now we'll just break we'll fill
- 1:33:42this in a little bit later
- 1:33:44case paper
- 1:33:49break and case
- 1:33:53scissors
- 1:33:57and break all right we're going to
- 1:34:00check to see what the computer picked so
- 1:34:04if computer
- 1:34:08is equal to rock
- 1:34:12then it's a draw so let's write
- 1:34:20it's a draw i think i'm missing a break
- 1:34:24statement
- 1:34:26all right then else if
- 1:34:31computer equals paper
- 1:34:34that means that we lose else if computer
- 1:34:38equals paper then we will write
- 1:34:44you lose
- 1:34:47else technically we don't need a
- 1:34:50condition because the only choice left
- 1:34:52is
- 1:34:53scissors for the computer
- 1:34:57else you win
- 1:35:00so let's copy these statements paste it
- 1:35:03underneath paper
- 1:35:06so if we choose paper and the computer
- 1:35:09chooses rock
- 1:35:10that means that we win so let's change
- 1:35:12this to you
- 1:35:14win if we choose paper and the computer
- 1:35:18chooses paper that means
- 1:35:20it's a draw
- 1:35:24else you lose
- 1:35:28let's copy this again paste it
- 1:35:31underneath scissors
- 1:35:33so if we pick scissors and the computer
- 1:35:35picks rock
- 1:35:36you lose
- 1:35:41if we choose scissors the computer picks
- 1:35:43paper then we win
- 1:35:46you win else
- 1:35:53it's a draw all right let's test it
- 1:35:58all right enter rock paper scissors
- 1:36:00let's go with rock
- 1:36:02rock paper you lose let's choose paper
- 1:36:05this time
- 1:36:05paper scissors you lose again and let's
- 1:36:08try scissors
- 1:36:11scissors rock you lose man i kind of
- 1:36:13suck at this game
- 1:36:15let's try it again paper paper rock okay
- 1:36:18i finally won one
- 1:36:19cool now at this point we're going to
- 1:36:22let the player decide if they would like
- 1:36:23to play another round so place
- 1:36:25this code at the end of our outer while
- 1:36:27loop so i'm just going to follow the
- 1:36:28dots here
- 1:36:30so that would go right here all right so
- 1:36:33let's use a write statement
- 1:36:38would you like to play
- 1:36:41again y slash
- 1:36:45n so y for yes
- 1:36:48and for no actually i think i'm going to
- 1:36:51create another variable so let's do so
- 1:36:53at the top
- 1:36:54string answer
- 1:36:59and we should probably reset this to
- 1:37:03answer equals an empty string
- 1:37:06okay so we will add one more variable
- 1:37:10okay answer equals
- 1:37:15console dot read
- 1:37:18line then let's make it uppercase
- 1:37:22in case the player enters in something
- 1:37:24that is lowercase
- 1:37:25we'll still count it answer equals
- 1:37:28answer
- 1:37:28dot to upper
- 1:37:34then let's check to see what the player
- 1:37:35enters in
- 1:37:37if answer is equal to y
- 1:37:43then play again
- 1:37:46equals true
- 1:37:50else play again
- 1:37:54equals false
- 1:37:59all right then outside of the while loop
- 1:38:02we will display a goodbye message
- 1:38:06maybe we'll say thanks for playing
- 1:38:10all right let's try it one last time
- 1:38:16okay enter rock paper scissors always
- 1:38:19choose rock
- 1:38:21player rock computer paper you lose
- 1:38:23would you like to play again
- 1:38:24yes let's pick rock again rock paper you
- 1:38:28lose okay maybe you don't always pick
- 1:38:30rock would you like to play again
- 1:38:31no because this is a dumb game thanks
- 1:38:33for playing
- 1:38:34well okay then everybody that is a game
- 1:38:37of rock paper scissors
- 1:38:39if you would like a copy of this code i
- 1:38:40will post this to the comment section
- 1:38:42down below
- 1:38:43if you can smash that like button leave
- 1:38:45a random comment down below
- 1:38:46and well that's a game of rock paper
- 1:38:49scissors for c-sharp
- 1:38:54alright people well i thought for
- 1:38:56practice we could create a sort of
- 1:38:57calculator program we can pick two
- 1:38:59numbers
- 1:39:00we can add them subtract them multiply
- 1:39:02them and divide them
- 1:39:03so let's get started this will be just a
- 1:39:05very simple project
- 1:39:07i'm going to write just calculator
- 1:39:09program
- 1:39:10when we begin this program and then
- 1:39:13maybe
- 1:39:13i don't know format it a little bit
- 1:39:15let's add some
- 1:39:17i don't know dashes or something just to
- 1:39:18kind of make this look nice
- 1:39:20this will all be within the console okay
- 1:39:23so we'll need to declare
- 1:39:24num1 num2 and result these will be
- 1:39:27doubles double num1 i'll set this equal
- 1:39:31to
- 1:39:31zero let's create num2
- 1:39:36and result double result
- 1:39:40and i will set that equal to zero as
- 1:39:42well
- 1:39:43okay we will need to ask for some user
- 1:39:45input
- 1:39:47console the right line enter
- 1:39:50number one colon space and
- 1:39:54then we will assign num1 equals
- 1:39:58so user input's always a string we'll
- 1:39:59need to convert this to
- 1:40:01a double convert dot to double
- 1:40:07and within the parenthesis we need
- 1:40:10console.read
- 1:40:12line method okay let's do the same thing
- 1:40:15for num2
- 1:40:17enter number two num2
- 1:40:21equals convert.2double pass in
- 1:40:24console.readline now we need the user to
- 1:40:27type in an option do they want to add a
- 1:40:29subtract multiply or divide
- 1:40:33so let's type enter and option
- 1:40:41console.writeline again what about
- 1:40:44plus colon space add
- 1:40:50then minus colon space
- 1:40:54subtract
- 1:40:57asterisk for multiply
- 1:41:04and forward slash for divide
- 1:41:07you can add other options if you want to
- 1:41:10okay let's take a look at this so far
- 1:41:12so we have calculator program enter
- 1:41:15number one what about 3.14
- 1:41:18enter number two uh let's say
- 1:41:22i don't know five okay a couple things
- 1:41:25i'm going to change real quick i'm going
- 1:41:26to change
- 1:41:27this right line to just right
- 1:41:30then i'm going to add a tab escape
- 1:41:32sequence
- 1:41:34that is backslash t for tab
- 1:41:38just so that it looks better
- 1:41:43okay let's continue so i'm going to
- 1:41:46create a switch that will read our user
- 1:41:48input
- 1:41:49switch console.read
- 1:41:54line and we need
- 1:41:57matching cases case
- 1:42:01plus so if somebody would like to add
- 1:42:04we will take our result equals
- 1:42:07num1 plus num 2.
- 1:42:13console.writeline and i'll do some
- 1:42:16string interpolation
- 1:42:19your result colon space
- 1:42:25num1 plus
- 1:42:28num2 equals
- 1:42:33plus result and then we need to break
- 1:42:37out of this case
- 1:42:39okay let's do the same thing with minus
- 1:42:43case minus result equals
- 1:42:47num1 minus num2
- 1:42:50num1 minus num2
- 1:42:53and that's all we need then we need
- 1:42:56multiply
- 1:42:58case asterisk result
- 1:43:01equals num1 times num2
- 1:43:06replace minus with asterisk
- 1:43:09and then divide
- 1:43:14case forward slash for division result
- 1:43:17equals num1 divided by num2
- 1:43:21and replace the asterisk here
- 1:43:25and then we should probably add a
- 1:43:26default case just in case somebody
- 1:43:28doesn't pick one of these options
- 1:43:30default
- 1:43:34console.writeline that
- 1:43:37was not a valid
- 1:43:40option and then break
- 1:43:45enter number one 3.14
- 1:43:48enter number two uh let's say 6.9
- 1:43:53and i would like to multiply these so
- 1:43:56i will add an asterisk
- 1:43:593.14 times 6.9 equals 21.666
- 1:44:03all right i'm just going to change one
- 1:44:05thing i'm going to add
- 1:44:07one line right before we read our user's
- 1:44:10input for
- 1:44:10what operation they would like to use
- 1:44:14so let's add console dot
- 1:44:17right now right line enter
- 1:44:21an option
- 1:44:24then let's try it again 3.14
- 1:44:29multiplied by 6.9 is
- 1:44:3421.666 okay now what if somebody would
- 1:44:37like to continue using this calculator
- 1:44:39so what we'll do is place all of this
- 1:44:41code within a do while loop
- 1:44:43i'm not sure if i discussed this in the
- 1:44:45past but a do while loop will always
- 1:44:47execute a body of code once
- 1:44:49and then it checks the condition at the
- 1:44:51end
- 1:44:53so let's add that here so we have
- 1:44:56do at the top then we need our while
- 1:44:59with condition
- 1:45:02while all right now before this
- 1:45:06let's ask for some user input
- 1:45:09console.writeline
- 1:45:13would you like to continue
- 1:45:21capital y equals yes
- 1:45:24capital n equals no
- 1:45:30and then we will read some user input so
- 1:45:32within
- 1:45:33the while loop the condition this will
- 1:45:35be
- 1:45:38console.readline
- 1:45:41dot 2 upper
- 1:45:44so in case they type in lowercase y or n
- 1:45:47we'll just change it to uppercase via
- 1:45:50the two upper method
- 1:45:52and let's check to see if this is equal
- 1:45:54to y
- 1:45:55if it is then we'll reset our num1 num2
- 1:45:59and result variables and do this all
- 1:46:00over again
- 1:46:02and if they would not like to continue
- 1:46:04let's write
- 1:46:06buy okay let's try this again
- 1:46:10what is point one four divided by two so
- 1:46:13enter number one three point one four
- 1:46:14enter number two two and let's divide
- 1:46:17these
- 1:46:18the result is one point five seven let's
- 1:46:21type
- 1:46:22y for yes all right then we can do this
- 1:46:24all over again
- 1:46:25what is 5 divided by 3.14
- 1:46:30so i will divide and the result is
- 1:46:331.59 something something something would
- 1:46:36you like to continue i'm going to type
- 1:46:38and for no
- 1:46:39bye so yeah that's a very simple
- 1:46:42calculator program in c sharp i will
- 1:46:45post this code in the comment section
- 1:46:46down below
- 1:46:47if you can give this video a thumbs up
- 1:46:48leave a random comment down below
- 1:46:50and subscribe if you'd like to become a
- 1:46:52fellow bro
- 1:46:53hey what's going on everybody it's you
- 1:46:55bro hope you're doing well
- 1:46:56and in this video i'm going to explain a
- 1:46:58raise in c sharp so
- 1:47:00sit back relax and enjoy the show
- 1:47:04all right everybody so arrays think of
- 1:47:07an array
- 1:47:08as a variable but it can store multiple
- 1:47:11values not just one so i have the string
- 1:47:14variable named car and it contains the
- 1:47:16name of a car bmw
- 1:47:18so what we could do is turn this
- 1:47:20variable into an array
- 1:47:21so it can store multiple values and this
- 1:47:24is all you have to do
- 1:47:25after the data type add a set of square
- 1:47:27brackets
- 1:47:28and then surround all of the values with
- 1:47:30a set of curly braces
- 1:47:32boom you got yourself an array and
- 1:47:34that's all there is to it so
- 1:47:36you can add multiple values to this
- 1:47:38array named car
- 1:47:39and actually i'm going to rename this as
- 1:47:41cars so it's plural so that we know that
- 1:47:43there's multiple values within here
- 1:47:45so let's separate each value with a
- 1:47:48comma
- 1:47:49so let's say we have a bmw a mustang
- 1:47:53as well as a corvette
- 1:47:56okay then let's display what cars is
- 1:47:58with a right line statement
- 1:48:00console the right line cars
- 1:48:05so this is what appears we have the data
- 1:48:07type of our array instead of the values
- 1:48:09within the array
- 1:48:11so if you need to access an element that
- 1:48:14is an item from an array
- 1:48:15you need to add after the variable name
- 1:48:17well the array name
- 1:48:19a set of square brackets and then the
- 1:48:21index
- 1:48:22so computers they always start with zero
- 1:48:24the first element would be
- 1:48:25zero so place that number within the
- 1:48:27square brackets
- 1:48:29cars at index zero and the first element
- 1:48:32would be bmw so let's display the other
- 1:48:36elements
- 1:48:37let's just copy what we have okay let's
- 1:48:40display
- 1:48:40cars at index 1 that will be this value
- 1:48:44because
- 1:48:44this is index zero
- 1:48:49and that is mustang
- 1:48:52then the next value cars at index
- 1:48:55two
- 1:48:59and that is corvette so let's take a
- 1:49:02look to see what happens if i attempt to
- 1:49:03access an element that doesn't exist
- 1:49:06so cars at index three so that would be
- 1:49:10something beyond corvette
- 1:49:16so we'll actually run into an error we
- 1:49:19ran into
- 1:49:20an index out of range exception so
- 1:49:22arrays do have a
- 1:49:24fixed size you have to make sure that
- 1:49:25you're not accessing an element
- 1:49:27that doesn't exist now if you need to
- 1:49:29update an element within an array all
- 1:49:31you have to do is type
- 1:49:33the array name in this case it would be
- 1:49:35cars straight brackets and
- 1:49:37list the index so if i need to change
- 1:49:39the first element within my array
- 1:49:42i will say my arrand name cars at index
- 1:49:44zero
- 1:49:45equals let's place a tesla there instead
- 1:49:49and then run this
- 1:49:52so instead of a bmw in the first
- 1:49:55position
- 1:49:56index zero we have a tesla so if you
- 1:49:58need to access or update an element
- 1:50:01within an array
- 1:50:02type the array name straight brackets
- 1:50:03and then the index
- 1:50:05and remember that these always start
- 1:50:06with zero so what we've done here
- 1:50:08to display the elements of an array is
- 1:50:10that we wrote a writeline statement for
- 1:50:13each individual
- 1:50:14element so a much better approach would
- 1:50:16be to iterate over
- 1:50:18our array using a for loop and later on
- 1:50:21i'll explain the for
- 1:50:22each loop so to display all of the
- 1:50:24elements of our array easily
- 1:50:26let's create a for loop so we'll say
- 1:50:29into i
- 1:50:30equals zero will continue this as long
- 1:50:32as i
- 1:50:33is less than now arrays have a length
- 1:50:35property so type the name of the array
- 1:50:38dot link then i
- 1:50:41plus plus to increment our index okay
- 1:50:44with our writeline statement
- 1:50:46let's display cars straight brackets
- 1:50:50and the index is going to be i so i
- 1:50:54is zero for the first iteration then
- 1:50:56during the next iteration it will be one
- 1:50:58then two and then it will stop because
- 1:51:01this statement will no longer be true
- 1:51:03okay let's try this again
- 1:51:06so we have tesla mustang and corvette so
- 1:51:09an easy way to display the elements of
- 1:51:11an array
- 1:51:12is to write a for loop and set the index
- 1:51:15equal to i
- 1:51:16whatever you have right here but we'll
- 1:51:18have to discuss the
- 1:51:20for each loop coming up in a later video
- 1:51:22one thing that you should know with
- 1:51:23arrays
- 1:51:24is that they have a fixed size if you
- 1:51:26would like to declare an array and then
- 1:51:28assign values later
- 1:51:29you'll need to declare that array with a
- 1:51:31fixed size of how many elements you're
- 1:51:33planning to place within that array
- 1:51:35so that would look something like this
- 1:51:37let's turn this line into a comment
- 1:51:39and this is how to declare an array so
- 1:51:42type the
- 1:51:43data type square brackets the name of
- 1:51:46the array
- 1:51:46equals new the data type again square
- 1:51:49brackets
- 1:51:50and then within this set of square
- 1:51:52brackets we will list a size
- 1:51:54so if i plan on adding no more than
- 1:51:57three elements to this array
- 1:51:58i can place three here but you can make
- 1:52:00this a larger size to accommodate more
- 1:52:02values
- 1:52:03and if you don't use those values
- 1:52:05they'll just remain empty no big deal
- 1:52:07and this will work the same as before
- 1:52:09and then we'll just have to update some
- 1:52:11values
- 1:52:12so cars at index 0 1 and
- 1:52:15zero is a tesla one is a mustang
- 1:52:18and two is a corvette and this will
- 1:52:21pretty much work the same as before
- 1:52:23instead of just in one step declaring
- 1:52:25and assigning an array
- 1:52:27we have done it in two steps we declared
- 1:52:29the array and then assigned some values
- 1:52:31later
- 1:52:32well okay then everybody that is an
- 1:52:34array it's a variable that can store
- 1:52:36multiple values and they have a fixed
- 1:52:39size so that's something you need to pay
- 1:52:41attention to
- 1:52:42if you would like a copy of this code i
- 1:52:44will post this to the comment section
- 1:52:45down below
- 1:52:46and well those are arrays in c sharp
- 1:52:50oh yeah all right everybody
- 1:52:53for each loops a for each loop is a
- 1:52:56simpler
- 1:52:57and more elegant way to iterate over an
- 1:52:59array
- 1:53:00however it's less flexible compared to a
- 1:53:03standard for loop
- 1:53:04i have an array of names of vehicles
- 1:53:06named cars
- 1:53:07we have a bmw mustang and corvette then
- 1:53:10we can use
- 1:53:11a standard for loop to iterate and
- 1:53:13display the elements of this array
- 1:53:16so this works fine however it's a lot of
- 1:53:18work to write all of this
- 1:53:19another approach although it's less
- 1:53:21flexible is to create a
- 1:53:23for each loop this will iterate once for
- 1:53:25each element within my array
- 1:53:27so type for each parenthesis curly
- 1:53:30braces
- 1:53:30and within the parentheses we list the
- 1:53:32data type of each element
- 1:53:34then we need a name for each element
- 1:53:36that we're working with
- 1:53:37since we're working with cars let's call
- 1:53:39each element a car
- 1:53:41string car in the name of the array
- 1:53:45cars string car in cars then i will with
- 1:53:48the right line statement
- 1:53:49display whatever car is so this is kind
- 1:53:52of like a nickname for the element that
- 1:53:54we're currently working with
- 1:53:56so let's get rid of this for loop
- 1:53:57because we don't need it anymore
- 1:53:59and use our for each loop instead
- 1:54:03and this works the same as before one
- 1:54:06downside with a for each loop is that
- 1:54:08it's not flexible with a standard for
- 1:54:10loop we can iterate forwards
- 1:54:12backwards or even skip iterations so use
- 1:54:15whatever loop is more appropriate for
- 1:54:17your situation
- 1:54:18well everybody that is a for each loop
- 1:54:21it's a
- 1:54:22simpler and more elegant way to iterate
- 1:54:24over an array
- 1:54:25however it's not as flexible if you just
- 1:54:27need something simple to display the
- 1:54:29elements of an array
- 1:54:30you can easily use a for each loop all
- 1:54:33right everybody so that's a for each
- 1:54:34loop if you can give this video
- 1:54:36a thumbs up drop a random comment down
- 1:54:37below and well those are four each loops
- 1:54:40in c-sharp
- 1:54:44all right people methods a method
- 1:54:46performs a
- 1:54:47section of code whenever it's called
- 1:54:49also known as
- 1:54:51invoking a method and the benefit of
- 1:54:53using a method
- 1:54:54is that it lets us reuse code without
- 1:54:56writing it
- 1:54:57multiple times here's a scenario let's
- 1:54:59say that we have to write a program
- 1:55:01where we need to
- 1:55:02sing happy birthday to our friend three
- 1:55:04times because we would like to annoy him
- 1:55:06one way in which we could do that
- 1:55:08without writing a method is that we
- 1:55:10would need to display as
- 1:55:11output you know the lyrics to happy
- 1:55:13birthday i just made my own version of
- 1:55:15it so
- 1:55:15happy birthday to you happy birthday to
- 1:55:17you happy birthday dear you
- 1:55:20happy birthday to you if i need to sing
- 1:55:22happy birthday three times
- 1:55:24i would need to copy and paste this code
- 1:55:27two additional times
- 1:55:28and that would allow me to effectively
- 1:55:30sing happy birthday three
- 1:55:31times so after running this we have all
- 1:55:34of our text
- 1:55:35repeated three times now a better way of
- 1:55:38writing this would be to use a method
- 1:55:40where we only have to write this once
- 1:55:41and then we can keep on reusing it
- 1:55:43yeah let's delete all this outside of
- 1:55:45our main method
- 1:55:46that will end right here we need to
- 1:55:48create a new method and the way we do
- 1:55:50that
- 1:55:50is that for this specific case we have
- 1:55:52to write static because we're invoking
- 1:55:55a method from our main method which is
- 1:55:57static so we need static but not always
- 1:56:00void and then a unique and descriptive
- 1:56:02name of this method what does it do
- 1:56:04exactly
- 1:56:05so let's call this method sing happy
- 1:56:09birthday then a set of parentheses then
- 1:56:11a set of curly braces
- 1:56:13and we have ourselves a method named
- 1:56:15saying happy birthday
- 1:56:16and when we call this method what kind
- 1:56:18of code do we want to execute
- 1:56:20so let's copy and paste the lyrics to
- 1:56:23happy birthday and then place it within
- 1:56:25this method now to invoke a method or
- 1:56:27called method
- 1:56:28we have to type the name of the method
- 1:56:31followed by a set of parentheses
- 1:56:32and i like to think of the parentheses
- 1:56:34kind of like a pair of telephones
- 1:56:36they're communicating with each other
- 1:56:38that's how i think of it at least and
- 1:56:39then a semicolon to end
- 1:56:41the statement so after running this this
- 1:56:43will execute sing happy birthday
- 1:56:45once now if i need to sing happy
- 1:56:48birthday two additional times
- 1:56:49i can just call this method two
- 1:56:51additional times sing happy birthday
- 1:56:53saying happy birthday sing happy
- 1:56:55birthday and i do not need all of this
- 1:56:57text i only need to write it once
- 1:56:58and then i can just keep on reusing it
- 1:57:01and here we have
- 1:57:03the lyrics to my happy birthday song
- 1:57:05repeated three times
- 1:57:07let's move on to level two we're going
- 1:57:09to create a string
- 1:57:10variable named name string name equals
- 1:57:13and make up some name
- 1:57:16now what if we attempt to use this
- 1:57:18variable within our method
- 1:57:20so replace you with name happy birthday
- 1:57:24dear name okay here's the situation
- 1:57:27the name name does not exist in the
- 1:57:30current context
- 1:57:31methods don't have access to any
- 1:57:33variables within another method
- 1:57:36so anything within the main method this
- 1:57:38singh happy birthday method cannot see
- 1:57:40so we need to pass what are known as
- 1:57:42arguments over to this method in order
- 1:57:44to use
- 1:57:45these values or variables so when you
- 1:57:48invoke a method and you need to send
- 1:57:50arguments over to a method we just list
- 1:57:52whatever we would like to send over to
- 1:57:54this method within the parentheses
- 1:57:56so i would like to pass name as an
- 1:57:59argument to this method
- 1:58:00however we need a matching set of
- 1:58:02arguments and parameters
- 1:58:04parameters are kind of like what this
- 1:58:06method is expecting to receive
- 1:58:08and to create some parameters we list
- 1:58:11the data type of what we're expecting
- 1:58:13followed by some sort of name so let's
- 1:58:15just say name
- 1:58:17and now we can use this variable name
- 1:58:19that is declared within the main method
- 1:58:21and we now have our name variable
- 1:58:23displayed within our happy birthday song
- 1:58:25and here's how to send more than one
- 1:58:27argument to a method let's create an
- 1:58:29integer variable
- 1:58:30named age and we will display the user's
- 1:58:32edge within our happy birthday song
- 1:58:35so separate each argument with a comma
- 1:58:38we will pass name as well as age to
- 1:58:41our happy birthday method but remember
- 1:58:44we need a matching
- 1:58:45set of arguments and parameters so we
- 1:58:48need to add
- 1:58:48a age parameter so separate each with a
- 1:58:52comma
- 1:58:52then list the data type of this variable
- 1:58:54first int
- 1:58:56and then a name we'll just call it edge
- 1:58:59and now we can use this edge variable
- 1:59:01within our method
- 1:59:02so let's create a line where we will
- 1:59:04display the person's edge
- 1:59:06so console that right line
- 1:59:09u r
- 1:59:13plus age plus
- 1:59:17years old and let's try this again
- 1:59:22happy birthday to you happy birthday to
- 1:59:24you happy birthday dear bro
- 1:59:26you are 21 years old happy birthday to
- 1:59:29you
- 1:59:29i guess my happy birthday song could use
- 1:59:31some work now these parameters they
- 1:59:33don't necessarily need
- 1:59:34the same exact name as the arguments
- 1:59:36that we pass in let's rename
- 1:59:38name as birthday boy
- 1:59:41and replace edge with years
- 1:59:44old okay then replace any instance of
- 1:59:47name with birthday boy
- 1:59:49and age with years old and this will
- 1:59:52work the same
- 1:59:54so you can rename these parameters
- 1:59:57what's important is the data type
- 1:59:59and the order of the values that you
- 2:00:01pass in
- 2:00:02all right everybody well those are
- 2:00:04methods they perform
- 2:00:05a section of code whenever they are
- 2:00:07called
- 2:00:08also known as invoking a method and the
- 2:00:11benefit of using methods
- 2:00:12is that it allows us to reuse code
- 2:00:15without writing it
- 2:00:16multiple times we only had to write the
- 2:00:19lyrics to happy birthday once
- 2:00:20then if we had to sing happy birthday
- 2:00:23you know more than one time
- 2:00:24we can just keep on invoking this method
- 2:00:26however many times that we need
- 2:00:28so those are methods if you can give
- 2:00:30this video a thumbs up
- 2:00:32drop a random comment down below and
- 2:00:34well yeah those are methods
- 2:00:35in c sharp
- 2:00:39oh yeah the return keyword now the
- 2:00:42return keyword
- 2:00:43returns data back to the place where a
- 2:00:45method is invoked
- 2:00:47so let's create a method to multiply two
- 2:00:49numbers together
- 2:00:50something simple so when we declare a
- 2:00:52method we'll have to make sure that we
- 2:00:53do it outside of our main method
- 2:00:55so right here so we will say static
- 2:00:59void and we will name this method
- 2:01:02multiply and we'll need to pass
- 2:01:05in two numbers as arguments let's say
- 2:01:07that the data type
- 2:01:09will be double double x and double
- 2:01:12y okay now we would like to multiply
- 2:01:15these two numbers together
- 2:01:17and we will store them within double
- 2:01:19variable z
- 2:01:20double z equals x times y
- 2:01:24now to return data back to the place
- 2:01:26where it's invoked
- 2:01:28we can use this return keyword and what
- 2:01:30kind of data would we like to return
- 2:01:32let's return whatever z is now there's
- 2:01:35one change that we need to make
- 2:01:37we need to replace this void keyword
- 2:01:39with the data type
- 2:01:41of what we're returning exactly so since
- 2:01:43we're returning a double
- 2:01:45we'll change void to double if you're
- 2:01:47not returning anything
- 2:01:48you can just keep that keyword as void
- 2:01:51now we can use this multiply method
- 2:01:52to well multiply two numbers together so
- 2:01:55let's write a program where we will ask
- 2:01:56a user for some user input
- 2:01:58multiply two numbers together then
- 2:02:00return the result and display it
- 2:02:02so let's accept some user input and
- 2:02:04first we should probably declare the
- 2:02:06variables that we'll need
- 2:02:07double x double y and
- 2:02:11double let's call this result and we
- 2:02:13will ask the user to enter
- 2:02:15in number one
- 2:02:19and then assign x equal and we'll need
- 2:02:22to convert
- 2:02:23the user's input to a double because
- 2:02:26it's always a string when we accept user
- 2:02:28input
- 2:02:28x equals convert dot
- 2:02:32to double
- 2:02:36then out of side of parenthesis within
- 2:02:37the parenthesis console.read
- 2:02:41line and we'll do the same thing for
- 2:02:44variable y enter in number two
- 2:02:48and then change x to y now if we need to
- 2:02:52assign
- 2:02:52results a value we can use this multiply
- 2:02:55method
- 2:02:56so type multiply add a set of
- 2:02:58parenthesis to invoke it
- 2:03:00but we'll need to pass two matching
- 2:03:02arguments because we have
- 2:03:03two parameters set up we need to pass in
- 2:03:06two
- 2:03:07double values or variables so we will
- 2:03:09pass
- 2:03:10x and y as arguments to this method
- 2:03:13multiply
- 2:03:14so this return keyword will return
- 2:03:17whatever z
- 2:03:17is back to the spot in where this method
- 2:03:20is invoked
- 2:03:21and we will store the result within this
- 2:03:23variable named result
- 2:03:24and then we should probably display it
- 2:03:27let's display
- 2:03:28result and well that's it let's try it
- 2:03:32enter in number one have you always
- 2:03:34wondered what
- 2:03:36pi times 420 is well we're about to find
- 2:03:38out
- 2:03:39it is 1 318.8
- 2:03:42so yeah that is the return keyword it
- 2:03:44returns data back to the place where a
- 2:03:46method is invoked
- 2:03:47and if you would like you can actually
- 2:03:49shorten this even further we don't
- 2:03:50necessarily need to
- 2:03:52store the result within you know a new
- 2:03:54variable we could just say return
- 2:03:56x times y and that would work the same
- 2:03:58too
- 2:03:59that is the return keyword it returns
- 2:04:01data back to the place where
- 2:04:02a method is invoked if you're able to
- 2:04:05give this video a thumbs up
- 2:04:06leave a random comment down below and
- 2:04:08well yeah that's the return keyword in c
- 2:04:10sharp let's go bros
- 2:04:14okay it looks like we're talking about
- 2:04:16method overloading now methods they can
- 2:04:18actually share the same name
- 2:04:19but you need a different set of
- 2:04:21parameters a method's name
- 2:04:23plus its parameters equals a method's
- 2:04:26signature
- 2:04:27and methods must have a unique method
- 2:04:29signature
- 2:04:30let's take this method for example
- 2:04:32called multiply so this returns
- 2:04:34a times b so what if i would like to
- 2:04:36multiply more than two numbers
- 2:04:38like three numbers well this method is
- 2:04:41only set up for
- 2:04:42two arguments so one thing that we could
- 2:04:44do is method overload
- 2:04:46so we can use the same name like we can
- 2:04:48reuse it but we need
- 2:04:49different parameters so to solve this
- 2:04:51problem i'm going to create a second
- 2:04:53multiply method
- 2:04:55and this will have a different set of
- 2:04:56parameters giving it a different
- 2:04:58method signature so let's create a
- 2:05:01method that has
- 2:05:02three parameters a b and c return
- 2:05:06a times b times c and you can see that
- 2:05:09that problem went away and this should
- 2:05:10work so 2 times 3 times 4
- 2:05:13equals 24 and i can just keep on
- 2:05:16creating more methods
- 2:05:17so basically that's method overloading
- 2:05:19methods can share the same name
- 2:05:21but they need different parameters if
- 2:05:23they do share the same name
- 2:05:25a method's name plus its parameters
- 2:05:27equals the method signature
- 2:05:29and methods must have a unique signature
- 2:05:32so yeah that's method overloading if you
- 2:05:34would like a copy of this code i'll post
- 2:05:36this to the comment section down below
- 2:05:37if you can smash that like button drop a
- 2:05:39random comment down below and well yeah
- 2:05:41that's method overloading in c
- 2:05:43sharp oh yeah ladies and gentlemen
- 2:05:48do you know what's better than
- 2:05:49overloaded methods the params keyword
- 2:05:52now a methods parameters can take a
- 2:05:55variable number of arguments if we use
- 2:05:57the params keyword and the parameter
- 2:05:59type must be a
- 2:06:01single dimensional array what's the
- 2:06:03benefit of this
- 2:06:04let me explain so let's say that we have
- 2:06:06an online store we have a method to
- 2:06:08check
- 2:06:08out where we will basically sum up a
- 2:06:10customer's prices
- 2:06:12and return a total and we'll tell the
- 2:06:14customer you have to pay this total what
- 2:06:16if the person
- 2:06:16checking out has a variable number of
- 2:06:19items that they would like to buy
- 2:06:20well we would have to set up a bunch of
- 2:06:22different methods like
- 2:06:24what if they only buy one item well
- 2:06:25there's a checkout method
- 2:06:27that accepts one argument another
- 2:06:29checkout method that accepts two
- 2:06:31arguments then three
- 2:06:32and then we continue on and on and on so
- 2:06:34yeah don't do this
- 2:06:35a better solution would be to use this
- 2:06:37params keyword where we only need a
- 2:06:39single method and this one single method
- 2:06:42can accept a
- 2:06:43varying number of arguments we don't
- 2:06:44need like several copies of the same
- 2:06:46method then
- 2:06:47so to use the params keyword precede our
- 2:06:50data type with the word params
- 2:06:53then after the data type follow this
- 2:06:55with a set of square brackets
- 2:06:57now this parameter a is now an array i'm
- 2:07:00going to name this as something a little
- 2:07:02more descriptive like
- 2:07:03prices because we're working with prices
- 2:07:06so we need to iterate over the elements
- 2:07:08of an array
- 2:07:09and create a total so i'm going to
- 2:07:11create a local variable
- 2:07:13named double total so you can have
- 2:07:16variables with the same name
- 2:07:18but they need to be within a different
- 2:07:19scope so these are in different methods
- 2:07:22so they are not visible to each other
- 2:07:24double total i will set the sql to zero
- 2:07:27to begin with
- 2:07:28and we'll use a for each loop to iterate
- 2:07:31over the elements of this array
- 2:07:33for each double
- 2:07:36price in prices
- 2:07:40total equals total plus
- 2:07:43whatever price we're working with but
- 2:07:45i'll shorten this
- 2:07:46to plus equals price then at the end
- 2:07:50we will return total and this should
- 2:07:53work the same
- 2:07:54okay the total price is 24.74
- 2:07:59but there's some formatting that we'll
- 2:08:00have to do in a future video and then i
- 2:08:02can just keep on adding items to my
- 2:08:04you know fictional shopping cart like i
- 2:08:06would like to buy a bottle of water
- 2:08:08for a dollar and a t-shirt for
- 2:08:1110 and 25 cents
- 2:08:15so you see by using this params keyword
- 2:08:18a single method can accept
- 2:08:20a varying amount of arguments so method
- 2:08:22overloading
- 2:08:23isn't always necessary if you're working
- 2:08:25with a lot of different arguments you're
- 2:08:27not really sure how many arguments are
- 2:08:28going to be passed in
- 2:08:30okay then everybody that is the params
- 2:08:32keyword it can be applied to
- 2:08:33a method parameter so that it can accept
- 2:08:36a variable number of arguments
- 2:08:38but the parameter type must be a single
- 2:08:41dimensional array
- 2:08:42and that is the params keyword if you
- 2:08:44can help me out by smashing that like
- 2:08:46button
- 2:08:46drop a random comment down below and
- 2:08:48well yeah that's the params keyword
- 2:08:50in c-sharp hey
- 2:08:54well it looks like we're on exceptions
- 2:08:56today an exception
- 2:08:57is an error that occurs during execution
- 2:09:00so here's an example i have a small
- 2:09:02program
- 2:09:03where we will ask a user to type in two
- 2:09:05numbers divide those numbers then
- 2:09:06display the result
- 2:09:08so what if i type in something that's
- 2:09:10not a number i would like to divide
- 2:09:11the number 5 by the word pizza we'll
- 2:09:14encounter an exception
- 2:09:16and this interrupts our program we
- 2:09:17encountered a format exception
- 2:09:20input string was not in a correct format
- 2:09:22so any code that is considered dangerous
- 2:09:25where it might cause an exception
- 2:09:27we can surround with a try block so i
- 2:09:29would consider this code to be dangerous
- 2:09:31so type try then a set of curly braces
- 2:09:34surround any code that is considered
- 2:09:36dangerous with
- 2:09:37the set of curly braces now if we have a
- 2:09:39try block we also need a catch block
- 2:09:41so after the try block add catch and the
- 2:09:44catch block catches and handles
- 2:09:46exceptions when they occur
- 2:09:47but we need to specify what kinds of
- 2:09:50exceptions we would like to catch and
- 2:09:51handle
- 2:09:52so catch parentheses curly braces and
- 2:09:55this is kind of like a parameter so i
- 2:09:57would like to catch
- 2:09:58and handle any format
- 2:10:01exceptions then add e so
- 2:10:04if we encounter that same exception
- 2:10:06again instead of our program being
- 2:10:08interrupted we can do something else
- 2:10:09instead
- 2:10:10so let's write a message and let the
- 2:10:11user know to type in only numbers enter
- 2:10:15only numbers please
- 2:10:18and let's try that again enter number
- 2:10:21one
- 2:10:22five enter number two pizza enter only
- 2:10:25numbers please
- 2:10:26and this did not interrupt our program
- 2:10:29that's the important thing
- 2:10:30now these try and catch blocks they will
- 2:10:32only catch and handle
- 2:10:33format exceptions what if somebody
- 2:10:35accidentally divides by zero or
- 2:10:37intentionally does so
- 2:10:38so we do not have a catch block set up
- 2:10:41for that we
- 2:10:42can add multiple catch blocks so let's
- 2:10:44catch any
- 2:10:45divide by zero exceptions so catch
- 2:10:47parentheses curly braces
- 2:10:49and the exception we would like to catch
- 2:10:51is divide by
- 2:10:52zero exception then add e so if somebody
- 2:10:56attempts to divide by zero
- 2:10:57let's display a message you can't divide
- 2:11:01by zero idiot but this only occurs with
- 2:11:05integer division so let's turn our
- 2:11:07doubles
- 2:11:08x and y into integers and we'll have to
- 2:11:12change
- 2:11:12this cast as well so change to double
- 2:11:15to and 32.
- 2:11:20so if we attempt to divide a number by
- 2:11:23zero which we can't mathematically do
- 2:11:25this is what happens enter number one
- 2:11:27five enter number two
- 2:11:28zero you can't divide by zero idiot and
- 2:11:31this will not interrupt our program
- 2:11:33now you can add a catch block that just
- 2:11:35catches everything but it's not
- 2:11:37considered good practice to add by
- 2:11:38itself
- 2:11:39so sometimes i'll add a like catch all
- 2:11:41block just in case if there's any
- 2:11:43exceptions i do not anticipate
- 2:11:45so at the end we can add exception e but
- 2:11:48it's difficult to let the user know what
- 2:11:50went wrong exactly because
- 2:11:51it'll just print when any exception
- 2:11:53occurs something
- 2:11:54went wrong so this will catch everything
- 2:11:58let's remove the first two catch blocks
- 2:12:00so let's take
- 2:12:02five divided by the word pizza something
- 2:12:04went wrong
- 2:12:05now catch exception e does catch
- 2:12:07everything but it's considered poor
- 2:12:09practice to have this by itself
- 2:12:11because basically you're shrugging when
- 2:12:13something goes wrong you should let the
- 2:12:15user know exactly what went wrong
- 2:12:17so it's better practice to catch
- 2:12:19specific exceptions first
- 2:12:21then at the end you can add you know
- 2:12:23exception e in case there's anything you
- 2:12:24don't anticipate
- 2:12:26by the way i changed x and y back to
- 2:12:28doubles now
- 2:12:29the last thing we need to talk about is
- 2:12:31the finally block
- 2:12:32the finally block is optional it always
- 2:12:35executes regardless if an exception is
- 2:12:37caught or not
- 2:12:38so at the end let's create a finally
- 2:12:40block type finally
- 2:12:41curly braces if there's anything you'd
- 2:12:43like to do after
- 2:12:45you're trying catch block regardless if
- 2:12:47an exception occurs or not
- 2:12:48you can place that here what people
- 2:12:50typically use the finally block for
- 2:12:52is to close any open files if they need
- 2:12:55to reset anything
- 2:12:57but for this example let's just print
- 2:12:58something so let's display
- 2:13:00a thank you message thanks for visiting
- 2:13:05if we do not encounter any exceptions
- 2:13:07this finally block will still execute
- 2:13:09five divided by one is five thanks for
- 2:13:11visiting
- 2:13:12now if we do encounter and catch an
- 2:13:15exception
- 2:13:16well that finally block is still going
- 2:13:18to execute as well
- 2:13:20thanks for visiting so yeah those are
- 2:13:22exceptions
- 2:13:23they are errors that occur during
- 2:13:25execution they interrupt the normal flow
- 2:13:27of our program
- 2:13:28any code that is considered dangerous we
- 2:13:31should surround with a try block
- 2:13:33and catch any exceptions when they occur
- 2:13:35and finally can be added
- 2:13:37and it will execute regardless if an
- 2:13:39exception is caught or not
- 2:13:40so yeah those are exceptions i will post
- 2:13:43this code in the comment section down
- 2:13:44below
- 2:13:45if you can smash that like button drop a
- 2:13:47random comment down below
- 2:13:48and well yeah those are exceptions in c
- 2:13:51sharp
- 2:13:55all right welcome back we're talking
- 2:13:56about the conditional operator which is
- 2:13:58represented by a question mark
- 2:14:00so the conditional operator is used in
- 2:14:03conditional assignment
- 2:14:04if a condition is true or false and we
- 2:14:07follow this formula
- 2:14:08we have some sort of condition or
- 2:14:10expression then we add a question mark
- 2:14:12kind of like we're asking a question
- 2:14:14is this true if it is do x if not do y
- 2:14:18so we'll write a simple program to check
- 2:14:20the temperature outside
- 2:14:22so we'll write this using an if-else
- 2:14:23statement first and then later on we'll
- 2:14:25use the conditional operator and take a
- 2:14:27look at the differences
- 2:14:28so let's say we have a double named
- 2:14:31temperature
- 2:14:32and i'll set this equal to 20 degrees
- 2:14:35celsius
- 2:14:36as well as a string message i will
- 2:14:39declare this but not yet assign it
- 2:14:41so what if we would like to assign our
- 2:14:43variable message
- 2:14:44a custom message based on the
- 2:14:46temperature well if we're using
- 2:14:48if else statements that would look like
- 2:14:49this so we need a condition
- 2:14:51if temperature is greater than or equal
- 2:14:56to let's say
- 2:14:5615 degrees celsius then we will assign
- 2:15:00message a string message
- 2:15:03equals it's
- 2:15:07warm outside
- 2:15:10else message
- 2:15:13equals it's cold outside
- 2:15:17and then at the end i would like to
- 2:15:19display my message within a writeline
- 2:15:21statement console that write line
- 2:15:24message and this will work just fine
- 2:15:27no issues so my temperature is currently
- 2:15:2920
- 2:15:30and this will print the message it's
- 2:15:32warm outside now there is a way to
- 2:15:34shorten this and that's by using a
- 2:15:36conditional operator
- 2:15:37so if we were to take that approach we
- 2:15:39would have to follow this formula
- 2:15:41so let me just copy this we need our
- 2:15:43condition our condition
- 2:15:44is temperature is greater than or equal
- 2:15:47to
- 2:15:4715. question mark like we're asking a
- 2:15:50question
- 2:15:51if it's true then do this so we will
- 2:15:53take
- 2:15:54its warm outside in place of x
- 2:15:58if not then we do this it's cold outside
- 2:16:02but these are returning values so we
- 2:16:04need to assign them to something
- 2:16:06so we will say message equals
- 2:16:10this is our condition temperature
- 2:16:12greater than or equal to 15
- 2:16:13question mark if that's true assign this
- 2:16:17if not assign that and that's all there
- 2:16:19is to it and we don't really need all of
- 2:16:20this anymore
- 2:16:22and then i will display my message
- 2:16:23within a redline statement
- 2:16:25and this will do the same thing it's
- 2:16:28warm outside
- 2:16:29you could even take this a step further
- 2:16:30too you could place just this within a
- 2:16:32right line statement and get rid of the
- 2:16:34message completely
- 2:16:35we don't even really need that message
- 2:16:36variable
- 2:16:38so yeah that's the conditional operator
- 2:16:40you can use this in conditional
- 2:16:42assignment if you need to assign
- 2:16:44some variable if some condition that you
- 2:16:46set is true or false
- 2:16:47it's kind of like a shortcut so yeah
- 2:16:49that's the conditional operator
- 2:16:51in c sharp
- 2:16:55all right what's going on people string
- 2:16:57interpolation string interpolation
- 2:16:59allows us to insert variables or values
- 2:17:02into
- 2:17:03a string literal we precede a string
- 2:17:06literal with a dollar sign
- 2:17:08and then any placeholders within a
- 2:17:10string literal
- 2:17:11that is interpolated we can use to
- 2:17:14insert values or variables within so
- 2:17:16here's an example of where
- 2:17:17string interpolation could be useful
- 2:17:20let's say we have three variables
- 2:17:21first name last name and age let's
- 2:17:24declare those
- 2:17:25string first name make up a first name
- 2:17:31string last name make up a
- 2:17:34last name and then an age
- 2:17:39okay so what if we would like to display
- 2:17:41somebody's first name last name and
- 2:17:43their age
- 2:17:43let's use a standard string literal
- 2:17:45first and we'll look at the differences
- 2:17:48so if i would like to display something
- 2:17:50like
- 2:17:51hello plus first name
- 2:17:56plus i'll add a space plus
- 2:18:00last name and then maybe add like a
- 2:18:04period at the end well i would have to
- 2:18:07concatenate all these separate strings
- 2:18:09together
- 2:18:10and then let me display age two
- 2:18:12console.writeline
- 2:18:14you are plus
- 2:18:18age plus years old
- 2:18:22so this works fine but an easier way is
- 2:18:24to use string interpolation
- 2:18:26so what we're going to do is create a
- 2:18:28string literal
- 2:18:30and then precede this with a dollar sign
- 2:18:32and then write a string normally
- 2:18:35hello wherever you would like to insert
- 2:18:37a value or variable
- 2:18:39add a set of curly braces as a
- 2:18:41placeholder
- 2:18:42and i would like to insert first name
- 2:18:44here then add a space
- 2:18:46and here within my string literal i will
- 2:18:49insert
- 2:18:50last name and then maybe add a period to
- 2:18:53the end
- 2:18:54and let's do the same thing with our
- 2:18:55second line so we need a string literal
- 2:18:58a double set of quotes precede this with
- 2:19:00a dollar sign
- 2:19:02you are curly braces
- 2:19:05age years old and this will do the exact
- 2:19:10same thing
- 2:19:11however it's less work
- 2:19:14so hello first name last name you are
- 2:19:16age years old
- 2:19:17another thing that you can do too is
- 2:19:19that when you display one of these
- 2:19:20values
- 2:19:21you can allocate some room after the
- 2:19:23variable name add a comma
- 2:19:25and then how many spaces you would like
- 2:19:27to allocate let's say that when i
- 2:19:28display my age i would like to allocate
- 2:19:3110 spaces worth of room to display my
- 2:19:33edge
- 2:19:34and this is what that looks like you are
- 2:19:38then i have 10 spaces including the two
- 2:19:41digits in my age
- 2:19:42and if this was negative this would be
- 2:19:45left aligned
- 2:19:48so my age is all the way over here then
- 2:19:50i have eight spaces afterwards
- 2:19:52so yeah that's string interpolation it
- 2:19:54allows us to insert variables into a
- 2:19:57string
- 2:19:58literal we precede a string literal with
- 2:20:00a dollar sign
- 2:20:02and then curly braces with then as
- 2:20:04placeholders for values or variables
- 2:20:07so yeah that's string interpolation in c
- 2:20:09sharp if you found this video helpful
- 2:20:11please be sure to smash that like button
- 2:20:13leave a random comment down below and
- 2:20:15subscribe if you'd like to become a
- 2:20:16fellow bro
- 2:20:20alright what's going on
- 2:20:21multi-dimensional arrays so
- 2:20:23just a fair warning this is a difficult
- 2:20:25topic if you want you can skip this
- 2:20:27video you won't hurt my feelings
- 2:20:28but if you'd like to know what a
- 2:20:29multi-dimensional array is well here we
- 2:20:31go
- 2:20:32so a multi-dimensional array is an array
- 2:20:35of arrays
- 2:20:36so i have three standard arrays i have
- 2:20:38different car manufacturers
- 2:20:39ford chevy and toyota so each array has
- 2:20:43three different kinds of cars a
- 2:20:45multi-dimensional array is good if you
- 2:20:46need like some sort of grid or matrix of
- 2:20:49data
- 2:20:50so what i would like to do is to create
- 2:20:51an array of arrays to simulate maybe
- 2:20:53like a parking lot to park all of these
- 2:20:55cars
- 2:20:56so to create a multi-dimensional array
- 2:20:58this will be a two-dimensional array
- 2:21:00we need to list the data type of what
- 2:21:02we're storing strings
- 2:21:04then a set of straight brackets and
- 2:21:07then within the straight brackets add a
- 2:21:09comma
- 2:21:11so now i'm going to name this array
- 2:21:13let's say
- 2:21:14parking lot
- 2:21:17equals and then we add our curly braces
- 2:21:19much like what we do with standard
- 2:21:21arrays
- 2:21:22however i'm going to add each array
- 2:21:24within the set of curly braces
- 2:21:26and then separate each with a comma
- 2:21:33so this can be somewhat difficult to
- 2:21:35read i like to kind of condense it all
- 2:21:38and just make it look nice
- 2:21:42so you can see that this resembles sort
- 2:21:44of like a grid with rows and columns
- 2:21:47and that visual is going to help us so
- 2:21:49to access one of these elements
- 2:21:51we need to list an index for the row and
- 2:21:54an index for the column
- 2:21:55so let's say i would like to change this
- 2:21:57explorer to something else
- 2:21:59so in order to access this element i'm
- 2:22:02going to type the name of our
- 2:22:03multi-dimensional array
- 2:22:05straight brackets and then i need a row
- 2:22:08and column
- 2:22:09so these always start with zero this
- 2:22:11would be row zero
- 2:22:12row one row two i need to access
- 2:22:15row one so that would be zero then comma
- 2:22:18for column and then i need to count the
- 2:22:21row number
- 2:22:22zero one two so that would be
- 2:22:25zero comma two for this element within
- 2:22:28my
- 2:22:28two-dimensional array so i'm going to
- 2:22:30change this to something else
- 2:22:32so let's say that this is a ford fusion
- 2:22:36and then let's display these elements we
- 2:22:37can use a for each loop
- 2:22:40for each the data type is string
- 2:22:43let's say car in parking lot
- 2:22:48and then i will just iterate through
- 2:22:49these console.writeline
- 2:22:52card so this will iterate nine times
- 2:22:55one for each element within our
- 2:22:57two-dimensional array
- 2:23:00so we have a mustang f-150 then fusion
- 2:23:03so we updated
- 2:23:04explorer to fusion so let's update maybe
- 2:23:07this corolla to something else
- 2:23:09so let's count the row number that would
- 2:23:11be zero
- 2:23:12one two parking
- 2:23:15lot two and the column number is
- 2:23:19zero and let's say that this is a tacoma
- 2:23:25and try this again okay we have
- 2:23:28instead of a corolla a tacoma
- 2:23:32now if you would like to display this as
- 2:23:34like a grid we can use
- 2:23:35nested for loops for that so let's get
- 2:23:38rid of this for each loop
- 2:23:39i'll just turn this all into one giant
- 2:23:41comment
- 2:23:43okay so to display this as like a grid
- 2:23:45we'll use
- 2:23:46nested for loops the outer loop will be
- 2:23:49four
- 2:23:50and i equal zero we will continue this
- 2:23:53as long as i
- 2:23:55is less than parking lot
- 2:23:58dot get length method and then pass in
- 2:24:02the dimension so pass in 0 for the first
- 2:24:05dimension
- 2:24:07i plus plus and then we need a nested
- 2:24:10for loop
- 2:24:12so change i to j get
- 2:24:15length one so this represents the second
- 2:24:18dimension remember that these always
- 2:24:20start with zero
- 2:24:21and then change i to j plus plus so
- 2:24:25during each iteration of the inner for
- 2:24:27loop let's change this from
- 2:24:29right line to right i will display
- 2:24:33parking lot straight brackets
- 2:24:37i comma j then i'm just going to add a
- 2:24:40space to separate
- 2:24:41each car and then when we escape our for
- 2:24:44loop we
- 2:24:45need to go down to the next row so i'll
- 2:24:47use an empty right line statement
- 2:24:49so by using nested for loops we can
- 2:24:51display our
- 2:24:53two-dimensional array as sort of a grid
- 2:24:55or matrix
- 2:24:57so we have our mustang f-150 our fusion
- 2:25:00corvette camaro silverado tacoma camry
- 2:25:04and rav4
- 2:25:05so if you ever need like a grid or
- 2:25:07matrix of data you can always use a
- 2:25:09multi-dimensional
- 2:25:10array and these are kind of the steps to
- 2:25:12set it up
- 2:25:13so yeah those are multi-dimensional
- 2:25:15arrays if you found this video helpful
- 2:25:17please be sure to smash that like button
- 2:25:19leave a random comment down below and
- 2:25:21subscribe if you'd like to become a
- 2:25:22fellow bro
- 2:25:25alright classes a class is really just a
- 2:25:28bundle of related code that you would
- 2:25:30like to organize together
- 2:25:32it can be used as a blueprint to create
- 2:25:34objects with object-oriented programming
- 2:25:37but we'll discuss that in the next video
- 2:25:40so my main method
- 2:25:41is located within a class named program
- 2:25:44the point of this class is well to group
- 2:25:46my program together
- 2:25:47but there are classes too that have
- 2:25:49certain functions for example the math
- 2:25:51class the purpose of the math class
- 2:25:53is to contain a bunch of useful methods
- 2:25:55related to mathematics
- 2:25:57and let's take a look at this
- 2:26:00so this class is named math and all
- 2:26:03that's in here is a bunch of methods
- 2:26:04related to
- 2:26:05mathematics so that's kind of the point
- 2:26:07of this class but we can create our own
- 2:26:09classes too
- 2:26:10now to create a class we can either do
- 2:26:12so outside of our class program
- 2:26:14or create a class within a separate
- 2:26:16c-sharp file i'll show you both ways
- 2:26:19so to create a class make sure that
- 2:26:20we're not writing this within our class
- 2:26:22program so that ends here
- 2:26:24type class and let's create a class to
- 2:26:27group a bunch of useful messages
- 2:26:29together like message
- 2:26:31methods class messages curly braces
- 2:26:35and let's create a few methods related
- 2:26:37to messages
- 2:26:38so this is going to be more of like a
- 2:26:40utility class
- 2:26:42void hello and when we use this method
- 2:26:45let's display a message like hello
- 2:26:50welcome to the program and let's create
- 2:26:54maybe two more
- 2:26:55void hello void what about waiting like
- 2:26:59we're waiting for something
- 2:27:03i am waiting for something
- 2:27:08and what about goodbye void bye
- 2:27:14bye
- 2:27:16thanks for visiting
- 2:27:19if you would like to create a class
- 2:27:20within a separate c-sharp
- 2:27:22file i would recommend opening your
- 2:27:24solution explorer if you don't have that
- 2:27:25open you can find it here
- 2:27:26go to view solution explorer
- 2:27:30right click on your namespace add
- 2:27:34class
- 2:27:37class and then i will name this messages
- 2:27:42add okay then i'm going to take
- 2:27:46my code from within this class cut it
- 2:27:50delete my class and then paste it within
- 2:27:54this class messages so you can either
- 2:27:58write your class within
- 2:27:59the same c sharp file or a different one
- 2:28:02if i would like to use some of these
- 2:28:03methods found within
- 2:28:04the messages class there's one of two
- 2:28:06things that i need to do
- 2:28:07i either need to create an object from
- 2:28:09this class
- 2:28:10or i need to precede my class definition
- 2:28:14with the word
- 2:28:14static followed by everything within my
- 2:28:17messages class
- 2:28:19so we'll talk about creating objects in
- 2:28:20the next video so for now we'll just
- 2:28:22have to
- 2:28:23use the static keyword and i'll create a
- 2:28:25separate video on this too
- 2:28:27and we should now be able to use these
- 2:28:28methods found within my messages class
- 2:28:31so if i need to display a hello message
- 2:28:34i would type the name of the class
- 2:28:36that would be messages then dot to
- 2:28:39access the
- 2:28:40members however these are not visible so
- 2:28:42we need to make them public
- 2:28:44and that'll be a separate video on axis
- 2:28:46modifiers so i would like this to be a
- 2:28:48public method
- 2:28:49so precede static with public
- 2:28:53and we should now be able to see those
- 2:28:55methods so type messages again
- 2:28:57dot and here are those methods i would
- 2:28:59like to use the hello method
- 2:29:01and that will display my message hello
- 2:29:04welcome to the program
- 2:29:05i also have that waiting message
- 2:29:09messages dot and that was waiting
- 2:29:13as well as goodbye messages
- 2:29:16dot bye so hello welcome to the program
- 2:29:20i am waiting for something bye thanks
- 2:29:22for visiting
- 2:29:23so basically a class is just a bundle of
- 2:29:26related code
- 2:29:28and it can be used as a blueprint to
- 2:29:30create objects
- 2:29:31otherwise you can use a class more or
- 2:29:33less as a utility class
- 2:29:35but when you define your class you'll
- 2:29:36have to precede the class definition
- 2:29:38with the word static
- 2:29:40and to make this public and accessible
- 2:29:42you would have to precede each method
- 2:29:44with
- 2:29:44public and static so yeah those are
- 2:29:47classes they're really just a bundle of
- 2:29:49related code
- 2:29:50you can use them as a utility class or
- 2:29:53use them as a blueprint to create
- 2:29:55objects
- 2:29:55which we'll discuss in another video if
- 2:29:58you found this video helpful please help
- 2:29:59me by smashing that like button
- 2:30:01leave a random comment down below and
- 2:30:03well yeah those are classes
- 2:30:05in c sharp
- 2:30:09all right objects an object is an
- 2:30:12instance of a class
- 2:30:13that means that we can use a class as a
- 2:30:16type of blueprint to create objects with
- 2:30:18object oriented programming
- 2:30:20so look around you right now you're
- 2:30:21surrounded by objects next to me i have
- 2:30:23a microphone a monitor a mouse
- 2:30:27and a cup of coffee so we can use code
- 2:30:29to mimic
- 2:30:30real world objects objects can have
- 2:30:33fields and
- 2:30:34methods these are defining
- 2:30:35characteristics and
- 2:30:37actions that an object can perform they
- 2:30:40describe what an object has
- 2:30:41and what an object can do and in today's
- 2:30:44video we will be instantiating human
- 2:30:46objects but will need the help of a
- 2:30:48human class
- 2:30:49as a type of blueprint to design what
- 2:30:51sorts of fields and methods that all
- 2:30:53humans should have
- 2:30:55so outside of our class program you can
- 2:30:57either do this within the same
- 2:30:58c-sharp file or a separate one within
- 2:31:01your solution explorer
- 2:31:02i'll keep it all within the same c-sharp
- 2:31:04file this time let's create a class
- 2:31:06named human then curly braces so let's
- 2:31:09add some fields
- 2:31:10fields are kind of like the defining
- 2:31:12attributes of what all humans should
- 2:31:14have
- 2:31:15let's say that humans have a name and an
- 2:31:17age
- 2:31:18so i'll define those much like what we
- 2:31:20do with variables
- 2:31:21string name i will declare this but not
- 2:31:23yet assign it
- 2:31:24as well as int edge
- 2:31:27so these are fields they're defining
- 2:31:29characteristics of what an object has
- 2:31:32now let's create some methods what kinds
- 2:31:33of actions can humans perform
- 2:31:35they can eat and they can sleep
- 2:31:38void eat and then i will display
- 2:31:41something when we invoke this method
- 2:31:43let's say name plus
- 2:31:47is eating and then let's create a
- 2:31:51sleep method void sleep
- 2:31:55name plus is sleeping
- 2:31:58okay now this is what we need to do to
- 2:32:00instantiate an object from a class
- 2:32:03type the name of the class human we need
- 2:32:05a unique name for this human object
- 2:32:08let's call this human object just human
- 2:32:10one
- 2:32:11human human one equals new human again
- 2:32:15add a set of parentheses and a semicolon
- 2:32:18there we go we now have a human object
- 2:32:20named human
- 2:32:22one now there's just one problem the
- 2:32:24fields and methods of this human object
- 2:32:26are not visible
- 2:32:27so if i type human one dot we can't
- 2:32:30access the name
- 2:32:32and age fields as well as the eat and
- 2:32:34sleep methods
- 2:32:35so normally i wouldn't recommend doing
- 2:32:37this because this isn't secure but just
- 2:32:39for learning purposes
- 2:32:41let's precede our fields and methods
- 2:32:43with the axis modifier of public
- 2:32:46so that these are publicly accessible
- 2:32:48from another class
- 2:32:49so if we type human one dot we should be
- 2:32:52able to access our name
- 2:32:54and age fields as well as the eat and
- 2:32:57sleep methods
- 2:32:58so let's give humanoid a name
- 2:33:01human1.name
- 2:33:02and let's assign human1 a first name of
- 2:33:06rick like from rick and morty and let's
- 2:33:08give him an age too
- 2:33:09human one dot h and he will be 65 years
- 2:33:13old
- 2:33:14and we should be able to use the eat and
- 2:33:17sleep methods now
- 2:33:19humanone dot
- 2:33:22eat and then human
- 2:33:25one dot sleep
- 2:33:29so after invoking these two methods eat
- 2:33:32and sleep this will display
- 2:33:35rick is eating and rick is sleeping so
- 2:33:37congratulations we have successfully
- 2:33:39instantiated a human object
- 2:33:42and the nice thing about classes is that
- 2:33:44we can keep on using this class to
- 2:33:45create more humans if we need more than
- 2:33:47one
- 2:33:48so this time let's create a second human
- 2:33:50so follow this formula the name of the
- 2:33:52class
- 2:33:53human then we need a unique name for
- 2:33:55this human object
- 2:33:56human one is taken so let's name the
- 2:33:58second human human to
- 2:34:00equals new human and then we can assign
- 2:34:03some of the fields
- 2:34:04of our human class so we need a name and
- 2:34:07an age
- 2:34:08so human 2's name will be morty
- 2:34:13and his age will be 16 and this time
- 2:34:16let's have human 2 use the eat and
- 2:34:19sleep method so rick is eating
- 2:34:22rick is sleeping morty is eating morty
- 2:34:25is sleeping
- 2:34:26so that's basically what an object is
- 2:34:28it's an instance of a class
- 2:34:30and a class can be used as a blueprint
- 2:34:32to create objects
- 2:34:33and objects can have fields and methods
- 2:34:36fields are what an object has
- 2:34:38methods are what an object can do think
- 2:34:40of it that way
- 2:34:41so yeah those are objects they're an
- 2:34:43instance of a class
- 2:34:45and a class can be used as a blueprint
- 2:34:46to create objects
- 2:34:48so if this video helped you out help me
- 2:34:50out by smashing that like button
- 2:34:52leave a random comment down below and as
- 2:34:54always subscribe if you'd like to become
- 2:34:56a fellow bro
- 2:35:00all right constructors a constructor is
- 2:35:02a
- 2:35:03special method found within a class it
- 2:35:05has the same name as the class name
- 2:35:07in the last video we created a human
- 2:35:09class but it's missing a constructor
- 2:35:11if we do not explicitly create a
- 2:35:13constructor there is a hidden one behind
- 2:35:15the scenes that is automatically called
- 2:35:17and it is used to create
- 2:35:18an object for us so let's explicitly
- 2:35:21create a constructor
- 2:35:22so you type public because we want this
- 2:35:25to be publicly accessible
- 2:35:26and then it has the same name as the
- 2:35:28class name then add a side of
- 2:35:30parentheses then a set of curly braces
- 2:35:32this works just like a method and we can
- 2:35:34even set up parameters
- 2:35:36so let's set up name and age parameters
- 2:35:38so we need to list the data type
- 2:35:40string name and int edge now
- 2:35:43in order to create a human object we
- 2:35:45have to pass a matching set of arguments
- 2:35:47we need to pass in a name and an age
- 2:35:50well a string to function as a name and
- 2:35:52an integer to function as an edge
- 2:35:54so we don't manually need to assign
- 2:35:56these fields anymore
- 2:35:57we can just pass these values as
- 2:35:59arguments so with human one human one's
- 2:36:01name was rick
- 2:36:02and his age was 65 so i will pass these
- 2:36:05as arguments
- 2:36:06rick and 65 and you can see that that
- 2:36:09red underline went away
- 2:36:10so let's do the same thing for human too
- 2:36:13morty and his age 16
- 2:36:14and we no longer need to manually assign
- 2:36:17those fields
- 2:36:17however within the constructor we'll
- 2:36:19need to do so so
- 2:36:21type this dot the name of the field we
- 2:36:23would like to assign
- 2:36:25equals the name of the parameter and in
- 2:36:28this case they have the same name
- 2:36:30this dot name equals name and this
- 2:36:33dot age equals
- 2:36:37whatever this is age and this will work
- 2:36:40the same as before but we did not need
- 2:36:42to
- 2:36:42manually assign those values to those
- 2:36:45fields so after calling
- 2:36:46the eaton sleep method for both human
- 2:36:49one and human too
- 2:36:50this will display rick is eating rick is
- 2:36:52sleeping morty is eating morty's
- 2:36:54sleeping
- 2:36:55so that's kind of what a constructor is
- 2:36:56it's a special method found within a
- 2:36:58class
- 2:36:59and it's automatically called when we
- 2:37:00instantiate an object if we do not
- 2:37:03explicitly create
- 2:37:04a constructor but if we do we can
- 2:37:06manually assign
- 2:37:07some values to fields of an object let's
- 2:37:10try a different example this time
- 2:37:11instead of humans let's create a car
- 2:37:13class we will instantiate some car
- 2:37:15objects so let's delete our current
- 2:37:17class and create a new one
- 2:37:18class car then curly braces
- 2:37:22cars will have let's say four fields
- 2:37:26a make a model a year and a color
- 2:37:30string make string
- 2:37:33model int
- 2:37:37year and string
- 2:37:40color then to create a constructor this
- 2:37:44will have the same name as the class
- 2:37:46name precede this with public car
- 2:37:49parentheses curly braces and again this
- 2:37:52is a special kind of method and we can
- 2:37:54set up arguments and parameters
- 2:37:56so we have string make
- 2:38:00string model
- 2:38:03integer
- 2:38:06and string color
- 2:38:10and then when we receive arguments we
- 2:38:12can assign them to these fields
- 2:38:14this dot make
- 2:38:18equals make
- 2:38:21this dot model equals model
- 2:38:25this dot year equals year
- 2:38:29and this dot color equals
- 2:38:33color then let's create maybe a drive
- 2:38:35method
- 2:38:38public void drive
- 2:38:43and we will display something
- 2:38:46u drive the
- 2:38:50plus make plus a lot of space to
- 2:38:53separate make and model
- 2:38:55plus model okay now let's instantiate
- 2:38:58some car
- 2:38:59objects so we type the name of the class
- 2:39:02car then we need a unique name or
- 2:39:04identifier for this car
- 2:39:06let's call this first car car one equals
- 2:39:08new
- 2:39:09car parenthesis semicolon but to
- 2:39:11instantiate a car object we have to pass
- 2:39:13a
- 2:39:14matching set of arguments to these
- 2:39:16parameters we have to pass
- 2:39:18a make model year and color so car one
- 2:39:21let's say is a ford mustang ford
- 2:39:26mustang the year will be 2022
- 2:39:30and the color is red and then let's have
- 2:39:34car one
- 2:39:34use the drive method so type car one
- 2:39:38dot drive then a set of parentheses to
- 2:39:41invoke it
- 2:39:42u drive the ford mustang now let's
- 2:39:45create cartoon
- 2:39:46it's the same process as before but we
- 2:39:49need a unique name or identifier
- 2:39:51car car 2 equals new car this will be
- 2:39:54a chevy corvette
- 2:39:58and the year will be 20 21 and the color
- 2:40:01is blue
- 2:40:04and then let's use the drive method of
- 2:40:06car 2
- 2:40:07car 2 drive you drive the ford mustang
- 2:40:11you drive the chevy corvette
- 2:40:13so yeah that's a constructor it's a
- 2:40:15special method
- 2:40:16found within a class and it has the same
- 2:40:18name as the class name
- 2:40:20class car and the constructor name is
- 2:40:22car as well and it can be used to assign
- 2:40:24arguments to fields when creating an
- 2:40:26object there's a few other things you
- 2:40:28can do with constructors too but we'll
- 2:40:30discuss that in future videos
- 2:40:31so yeah those are constructors if you
- 2:40:34can smash that like button
- 2:40:35leave a random comment down below and
- 2:40:37well yeah those are constructors
- 2:40:39in c sharp oh yeah let's do this
- 2:40:43all right the static modifier the static
- 2:40:46modifier is used to declare a
- 2:40:48static member one which belongs to the
- 2:40:51class itself
- 2:40:52rather than any one specific object i
- 2:40:54have a class named carr cars have
- 2:40:57a field named model and a constructor to
- 2:41:00assign
- 2:41:00a model when we instantiate a car object
- 2:41:03and i have two car objects car one and
- 2:41:05car two car one is a mustang
- 2:41:07car two is a corvette here's the
- 2:41:09situation let's say that we're going to
- 2:41:11have a race but we need to keep track of
- 2:41:13how many cars are instantiated that are
- 2:41:15entering
- 2:41:16our race one way in which we can do that
- 2:41:18is to create a static field to keep
- 2:41:20track of how many cars are created
- 2:41:22so let me show you what this looks like
- 2:41:24with a non-static field first
- 2:41:26so let's declare an integer variable
- 2:41:29named number
- 2:41:30of cars to keep track of how many cars
- 2:41:32we create
- 2:41:34so i will declare this but not yet
- 2:41:36assign it
- 2:41:37and within my constructor i will
- 2:41:39increment number of cars by one
- 2:41:41remember that with constructors you're
- 2:41:43not limited to
- 2:41:44only assigning values to fields you can
- 2:41:46do like any sort of code that you want
- 2:41:48remember that it's just another kind of
- 2:41:50method okay so every time we instantiate
- 2:41:53a car object we will increment
- 2:41:54number of cars by one and then let's
- 2:41:57display a number of cars
- 2:41:59so we need to access this in a
- 2:42:01non-static way by typing
- 2:42:03the name of a car object like car 1 dot
- 2:42:05followed by the name of the field
- 2:42:07but it looks like we need to make this
- 2:42:08public
- 2:42:10public and number of cars car one
- 2:42:14dot number of cars let's do the same
- 2:42:16thing for
- 2:42:17car two car two dot number of cars
- 2:42:20now you would think that the number of
- 2:42:22cars would be two
- 2:42:23right and that's where you're wrong
- 2:42:25number of cars
- 2:42:26for both car 1 and car 2 are both one
- 2:42:29because each car object has their own
- 2:42:32copy of the number of cars field and
- 2:42:35within the constructor when we
- 2:42:37increment each copy of number of cars
- 2:42:40well it's only ever going to be a
- 2:42:41maximum of one
- 2:42:43so one way in which we could fix that is
- 2:42:45to change
- 2:42:46this field to a static field after
- 2:42:49public type
- 2:42:50static public static int number of cars
- 2:42:54and we can no longer access this field
- 2:42:56in a non-static way
- 2:42:58by typing the name of an object followed
- 2:43:00by the name of the field
- 2:43:02we would have to access this field in a
- 2:43:04static way
- 2:43:05by typing the name of its class followed
- 2:43:08by the name of the field
- 2:43:09so type the name of the class car
- 2:43:11followed by you know dot
- 2:43:13then the name of the field so this field
- 2:43:16now belongs to the car class
- 2:43:18and no one object has complete ownership
- 2:43:21of it it's kind of like they're all
- 2:43:22sharing the same variable and let's try
- 2:43:25this again
- 2:43:26so the number of cars that we have
- 2:43:28created
- 2:43:29is two now just to test everything let's
- 2:43:32create a third car we have a third racer
- 2:43:34within our race
- 2:43:36so car 3 will be a what about a lambo
- 2:43:39and we now have created three cars
- 2:43:42so again by preceding this field with
- 2:43:44the static modifier
- 2:43:46the class now owns it we can also apply
- 2:43:48the static modifier
- 2:43:50to a method as well public static
- 2:43:53and we'll create a method to begin our
- 2:43:55race let's call this
- 2:43:56start race
- 2:44:00so we don't want each object to be able
- 2:44:02to start the race on their own terms
- 2:44:04right
- 2:44:05so it would be better if the car class
- 2:44:07itself has a start race method
- 2:44:10so then if we would like to begin our
- 2:44:11race we can access it in a static way
- 2:44:14the race has begun
- 2:44:18then if i need to invoke this method i
- 2:44:21type
- 2:44:21the name of the class car dot
- 2:44:25the name of the static method start race
- 2:44:28then a set of parentheses to invoke it
- 2:44:30and this should now begin our race
- 2:44:32the race has begun and then you can also
- 2:44:36apply the static modifier to a class
- 2:44:38itself but then you can't instantiate
- 2:44:40objects from this class you can see that
- 2:44:42we're getting errors
- 2:44:43cannot declare a variable of static type
- 2:44:46cart
- 2:44:46so that's kind of like the deal with the
- 2:44:48math class
- 2:44:49in order to use a method of the math
- 2:44:51class we wouldn't create like a map
- 2:44:53object right like
- 2:44:54math math 1 equals new math right
- 2:44:57and then we wouldn't type math1 dot
- 2:45:00round to rounded number
- 2:45:01it's a lot easier just to use directly
- 2:45:04the math class and type
- 2:45:06math dot round so that's kind of the
- 2:45:09idea behind
- 2:45:09a static class you can't create objects
- 2:45:12from a static class
- 2:45:14and we kind of learned about that in the
- 2:45:15video on classes
- 2:45:17well in conclusion the static modifier
- 2:45:20can be applied to a field
- 2:45:22a method or a class itself anything that
- 2:45:25is declared static
- 2:45:26now belongs to the class and no one
- 2:45:29object has
- 2:45:30ownership of it so that is the static
- 2:45:33modifier
- 2:45:34if this video helped you out help me out
- 2:45:36by smashing that like button drop a
- 2:45:37random comment down below and well
- 2:45:39yeah that's the static modifier in
- 2:45:41c-sharp
- 2:45:45all right overloaded constructors by
- 2:45:48using overloaded constructors this is a
- 2:45:50technique to create
- 2:45:51multiple constructors within the same
- 2:45:53class much like that video on overloaded
- 2:45:55methods
- 2:45:56so with overloaded constructors multiple
- 2:45:59constructors can have the same name
- 2:46:01but they need a different set of
- 2:46:02parameters because
- 2:46:04a method's name plus its parameters
- 2:46:06equals a unique method signature
- 2:46:08and each method must have their own
- 2:46:10unique signature
- 2:46:12so let's create a class named pizza
- 2:46:17and there's going to be four fields
- 2:46:19let's have string bread
- 2:46:20for the type of bread we would like for
- 2:46:22our pizza like is it flat bread
- 2:46:24stuffed crust plain
- 2:46:28a field for sauce cheese
- 2:46:31and topping
- 2:46:35then let's create a constructor
- 2:46:38public pizza
- 2:46:41and then we need to set up the
- 2:46:43parameters
- 2:46:45string bread string sauce
- 2:46:49string cheese and
- 2:46:52string topping and then we'll need to
- 2:46:54assign these
- 2:46:56this dot bread equals bread
- 2:46:59this dot sauce equals sauce
- 2:47:03this dot cheese equals cheese
- 2:47:07and this dot topping equals
- 2:47:11topping all right then let's construct a
- 2:47:15pizza object
- 2:47:18pizza let's call this pizza all lower
- 2:47:20case
- 2:47:21equals new pizza then we need to pass
- 2:47:25in something for the bread the sauce the
- 2:47:27cheese and the topping
- 2:47:28because we can't construct a pizza
- 2:47:31object without
- 2:47:32bread sauce cheese and a topping we need
- 2:47:35all four
- 2:47:36in order to construct a pizza object so
- 2:47:39let's pass in something for the bread
- 2:47:41let's say i would like a stuffed crust
- 2:47:43pizza stuffed crust
- 2:47:45for the sauce let's say red sauce
- 2:47:49and the cheese what about mozzarella i
- 2:47:52don't remember how to spell mozzarella
- 2:47:55mozzarella i'm taking a guess here
- 2:47:58and a topping pepperoni
- 2:48:02so we can construct a pizza object just
- 2:48:05fine right
- 2:48:06but what if we would like a pizza
- 2:48:08without any toppings like a plain cheese
- 2:48:10pizza
- 2:48:10so i'm going to get rid of that topping
- 2:48:12well we can't construct a pizza without
- 2:48:14a topping we need to add something
- 2:48:16because we only have one constructor set
- 2:48:18up and we need to pass in something for
- 2:48:20a topping
- 2:48:21so one way in which we can solve this
- 2:48:23problem is to have multiple constructors
- 2:48:26overloaded constructors we can pass in a
- 2:48:29varying amount of arguments
- 2:48:30so if we would like a cheese pizza
- 2:48:32without any toppings
- 2:48:33we can use that specific constructor
- 2:48:35that will construct a
- 2:48:37plain cheese pizza so let's copy this
- 2:48:39constructor
- 2:48:40they can have the same name but they
- 2:48:42need a different set of parameters
- 2:48:44so i'm going to remove this topping
- 2:48:46parameter
- 2:48:47and we will not assign this dot topping
- 2:48:50equals topping
- 2:48:51and we can now create a plain cheese
- 2:48:54pizza
- 2:48:54and let's do the same thing with i don't
- 2:48:56know maybe somebody doesn't want cheese
- 2:48:58on their pizza
- 2:49:01so pizza with only bread and sauce
- 2:49:07and then another pizza with only bread
- 2:49:10i'm not even really sure if it's
- 2:49:11considered a pizza then maybe it's
- 2:49:12garlic bread or something
- 2:49:13but i don't know though we can now
- 2:49:15create a pizza object that has
- 2:49:17no toppings no cheese and no sauce
- 2:49:21so by using overload constructors we can
- 2:49:23construct objects with
- 2:49:25varying amounts of fields maybe we would
- 2:49:27like toppings
- 2:49:28maybe we don't well we have a choice now
- 2:49:31so yeah that's overloaded constructors
- 2:49:33it's a technique to create
- 2:49:34multiple constructors with a different
- 2:49:37set of parameters
- 2:49:38they can share the same name but you
- 2:49:39need a different set of parameters
- 2:49:42because a constructor's name plus its
- 2:49:44parameters
- 2:49:45equals a constructor's signature and
- 2:49:47each constructor needs a unique
- 2:49:49signature so yeah those are overloaded
- 2:49:52constructors in c
- 2:49:53sharp if this video helped you out help
- 2:49:55me out by smashing that like button
- 2:49:57leave a random comment down below and
- 2:49:58well yeah those are overloaded
- 2:50:00constructors
- 2:50:01in c sharp
- 2:50:06all right what's going on people
- 2:50:07inheritance so inheritance is this
- 2:50:10programming concept
- 2:50:11of one or more child classes receiving
- 2:50:14fields methods
- 2:50:15etc from a common parent kind of like
- 2:50:18how people
- 2:50:19inherit genes from their parents well
- 2:50:21with programming we can kind of do
- 2:50:22something similar
- 2:50:23parent classes can have children classes
- 2:50:26and anything
- 2:50:27that the parent class has the child
- 2:50:29classes now have
- 2:50:31so here's an example let's create a
- 2:50:32parent class named
- 2:50:34vehicle not any kind of specific vehicle
- 2:50:36just a generic
- 2:50:37vehicle class class vehicle and
- 2:50:41there will be one field let's say public
- 2:50:44and speed and i'll set this equal to
- 2:50:47maybe zero and let's create a method
- 2:50:50public void go all vehicles can go
- 2:50:52somewhere
- 2:50:54and all we'll display is
- 2:50:57this vehicle is moving
- 2:51:00we can inherit this speed field and this
- 2:51:03go method from the vehicle class
- 2:51:05so this vehicle class is going to have
- 2:51:07children so let's create a car class
- 2:51:09a bike class and a boat class so type
- 2:51:12class
- 2:51:13car then to inherit from another class
- 2:51:15type
- 2:51:16after the class name colon and then the
- 2:51:19parent in which you would like to
- 2:51:20inherit from
- 2:51:21so car is going to be the child class
- 2:51:24vehicle will be the parent class
- 2:51:26so car will have access to its own speed
- 2:51:29and go method
- 2:51:30and then you can add anything additional
- 2:51:32within the car class like number of
- 2:51:34wheels
- 2:51:35public and wheels and i'll set this to 4
- 2:51:38for all
- 2:51:38car objects now let's create a bicycle
- 2:51:41class
- 2:51:42it's the same process as before class
- 2:51:45bicycle and this will inherit from the
- 2:51:49vehicle class as well but let's change
- 2:51:51wheels to two and then let's create a
- 2:51:53book class
- 2:51:54class boat and here it's from vehicle
- 2:51:57wheels
- 2:51:58zero because well it's a boat okay so
- 2:52:00let's instantiate a car
- 2:52:02bicycle and boat object so let's do that
- 2:52:05here
- 2:52:06car car equals new car
- 2:52:11bicycle bicycle
- 2:52:14keep on spilling bicycle wrong bicycle
- 2:52:18equals new bicycle
- 2:52:21and boat boat equals new boat
- 2:52:25so if we take a look at this car object
- 2:52:27car
- 2:52:28dot there is a speed field as well as a
- 2:52:31go method even though there's nothing
- 2:52:33within
- 2:52:34this car class besides wheels so since
- 2:52:36car is the child class
- 2:52:38and vehicle is the parent class car has
- 2:52:41access to a go method and a speed field
- 2:52:43even though there's nothing within here
- 2:52:45except a wheels field
- 2:52:46and of course car objects do have a
- 2:52:48wheels field as well
- 2:52:50which we stand within the car class so
- 2:52:52let's just test these
- 2:52:53i'm going to display car dot speed
- 2:52:57car dot speed followed by
- 2:53:02car dot wheels and i will have this car
- 2:53:06use
- 2:53:06its go method car.go
- 2:53:10all right so our car speed is zero
- 2:53:12that's because we set it to be zero
- 2:53:14within the vehicle class
- 2:53:16this car has four wheels which we stated
- 2:53:18within the car class
- 2:53:19and this car class has a go method that
- 2:53:22states this vehicle is moving
- 2:53:24so i bet the same thing applies to our
- 2:53:26bicycle and boat object
- 2:53:28so let's copy these three lines of code
- 2:53:30change car to
- 2:53:31bicycle bicycle.speed bicycle.wheels
- 2:53:35and bicycle.go and the same thing
- 2:53:37applies to both
- 2:53:38boat dot speed boat dot wheels boat dot
- 2:53:41go
- 2:53:43so all three of these different types of
- 2:53:45vehicles each have
- 2:53:46a speed field a wheels field and a go
- 2:53:50method
- 2:53:50so the point of inheritance is that we
- 2:53:52can reuse code
- 2:53:54i mean we could just copy these fields
- 2:53:56and methods and then paste them within
- 2:53:58each of these classes
- 2:53:59but imagine that you have hundreds of
- 2:54:00different classes and we need to change
- 2:54:02like go to start well it's a lot easier
- 2:54:06just to make that change in one place
- 2:54:07than having to manually go through all
- 2:54:09of our classes
- 2:54:10so yeah that's kind of the benefit of
- 2:54:11inheritance we can keep
- 2:54:13anything that these classes have in
- 2:54:15common within a common parent class
- 2:54:17and these classes can simply just
- 2:54:19inherit these fields and methods
- 2:54:21so yeah that's inheritance if you liked
- 2:54:23this video be sure to smash that like
- 2:54:25button
- 2:54:26drop a random comment down below and
- 2:54:28well yeah that's inheritance in c sharp
- 2:54:34alright what's going on abstract classes
- 2:54:36abstract is a keyword that we can apply
- 2:54:39to classes properties and methods but in
- 2:54:42this video we're going to focus on
- 2:54:43abstract classes now you can apply the
- 2:54:46abstract keyword it's a modifier
- 2:54:48to a class and that indicates that this
- 2:54:51class
- 2:54:51is missing components or is an
- 2:54:53incomplete implementation
- 2:54:55so let's take a look we're using
- 2:54:56inheritance here so i have a class
- 2:54:58vehicle vehicle has a speed field as
- 2:55:01well as a go method
- 2:55:03and there's three children classes car
- 2:55:05bicycle and
- 2:55:06boat and these are complete
- 2:55:08implementations
- 2:55:09so car has four wheels and a max speed
- 2:55:11of 500
- 2:55:12bicycle has two wheels a max speed of 50
- 2:55:15and
- 2:55:16boat has zero wheels and a max speed of
- 2:55:17100 i just made up some numbers for the
- 2:55:19max speed
- 2:55:20i can create a car object bicycle object
- 2:55:23and boat object just fine
- 2:55:24these are complete implementations but
- 2:55:27unfortunately i can also create a
- 2:55:29vehicle object
- 2:55:30vehicle vehicle equals new vehicle
- 2:55:33so vehicle is incomplete it's missing a
- 2:55:36number of wheels and a max speed
- 2:55:38so imagine that we're having a race and
- 2:55:40somebody decides to pick
- 2:55:42the vehicle but we want somebody to pick
- 2:55:44a specific
- 2:55:45implementation of a vehicle like a car
- 2:55:48bicycle or boat
- 2:55:49i would like to prevent people from
- 2:55:51creating generic vehicle objects
- 2:55:53so this is kind of like imagine somebody
- 2:55:55picks the invisible vehicle in like a
- 2:55:57racing game or something
- 2:55:59one way in which i can prevent people
- 2:56:00from instantiating vehicle objects
- 2:56:02because it's not finished it's an
- 2:56:04incomplete implementation
- 2:56:06is to precede this class definition with
- 2:56:08this abstract keyword
- 2:56:10so if i precede class with abstract i
- 2:56:13can no longer create a
- 2:56:14vehicle object cannot create an instance
- 2:56:17of the abstract type
- 2:56:18or interface vehicle so that adds a
- 2:56:21little bit of security to our programs
- 2:56:23if there's a class that you do not want
- 2:56:24to be able to instantiate objects from
- 2:56:27you can just precede that class
- 2:56:28definition with the abstract keyword
- 2:56:31and you cannot create an object from
- 2:56:33that class
- 2:56:34and that's basically what the abstract
- 2:56:35modifier does we can indicate that a
- 2:56:38class
- 2:56:38we can also apply this to properties and
- 2:56:40methods as well but that's a video for
- 2:56:42another day
- 2:56:43so when we precede a class definition
- 2:56:46with abstract
- 2:56:47we are indicating that that class is
- 2:56:49missing components
- 2:56:50or is incomplete so we should not be
- 2:56:52able to instantiate
- 2:56:54objects from this class which adds a
- 2:56:56little bit of security so yeah
- 2:56:57that's the abstract keyword if you found
- 2:56:59this video helpful help me out by
- 2:57:01smashing that like button
- 2:57:02leave a random comment down below and
- 2:57:03well yeah that's the abstract modifier
- 2:57:06in c-sharp
- 2:57:10all right what's going on people so in
- 2:57:12this video i'm going to show you all how
- 2:57:14we can create an array of objects
- 2:57:15i have a class car cars have one field a
- 2:57:18model field
- 2:57:19and a constructor to assign an argument
- 2:57:21to this
- 2:57:22model field so let's create some car
- 2:57:24objects car
- 2:57:25car one equals new car then pass in a
- 2:57:29model of your choosing
- 2:57:30let's say that car one will be a mustang
- 2:57:33and let's create maybe two more cars
- 2:57:36car two and car three car two will be
- 2:57:39a corvette and car three will be a lambo
- 2:57:43so we'll need an array to work with too
- 2:57:46so in the past when we've created arrays
- 2:57:48we will list the data type of what we're
- 2:57:50storing within our array like strings
- 2:57:52ins whatever so after the data type add
- 2:57:55a set of
- 2:57:55straight brackets but the data type is
- 2:57:58going to be the
- 2:57:59data type of the objects that we're
- 2:58:00working with since we're working with
- 2:58:02car objects
- 2:58:03we'll change that to car car straight
- 2:58:06brackets
- 2:58:06then we need a name for this array let's
- 2:58:09say garage it's kind of like we're
- 2:58:10parking cars within a garage
- 2:58:12equals new the data type again straight
- 2:58:15brackets
- 2:58:15then a size for this array let's say
- 2:58:18three then we will assign
- 2:58:20some of these indexes with an object so
- 2:58:22let's begin with garage at index
- 2:58:24zero this will equal let's say car one
- 2:58:27it's as if car one is parking within our
- 2:58:30garage at
- 2:58:31this parking spot number of zero then
- 2:58:33let's park
- 2:58:34car two and car three so garage at index
- 2:58:37of one
- 2:58:38equals car two garage at index of two
- 2:58:41equals car three then let's display
- 2:58:43these
- 2:58:44console.right console.writeline garage
- 2:58:47at index of zero so let me just show you
- 2:58:50what this looks like first we're not
- 2:58:51accessing the model quite yet we're just
- 2:58:53displaying whatever's within garage at
- 2:58:55index of zero and what we get is the
- 2:58:58namespace
- 2:58:59followed by the data type of our object
- 2:59:02so if we need
- 2:59:03the model of this object whatever's
- 2:59:04within garage index of zero
- 2:59:06i'm going to follow this with dot model
- 2:59:10and that will display my car's model
- 2:59:13which is a mustang
- 2:59:14then let's do the same thing for garage
- 2:59:16at index of one and two
- 2:59:18so garage index of one garage index of
- 2:59:21two
- 2:59:21then make sure to follow those with dot
- 2:59:23model
- 2:59:24so within our garage between indexes 0
- 2:59:27through
- 2:59:282 we have a mustang corvette and a lambo
- 2:59:31now another way in which we could
- 2:59:32display these is with a for each loop
- 2:59:35for each parentheses curly braces
- 2:59:39and with our for each loop we have to
- 2:59:41list the data type
- 2:59:42of whatever's within our array car
- 2:59:46and let's call each car just car car car
- 2:59:49in garage then during each iteration we
- 2:59:53will display
- 2:59:54car dot model and that will do the same
- 2:59:58thing
- 2:59:58however this time we will iterate
- 3:00:00through our array
- 3:00:02now there's another way in which we
- 3:00:03could write this too we don't need to
- 3:00:05necessarily
- 3:00:06define a size for our array then create
- 3:00:08some named car objects
- 3:00:10and then assign those objects directly
- 3:00:12to each index
- 3:00:13we could just declare an array and then
- 3:00:15immediately instantiate some objects all
- 3:00:17in one step
- 3:00:18so we're going to create a new array car
- 3:00:20straight brackets i will call this
- 3:00:22garage
- 3:00:23equals curly braces and then we will
- 3:00:26pass in
- 3:00:27just new car then you need to pass in a
- 3:00:29model
- 3:00:32and do the same thing with your other
- 3:00:33two cars
- 3:00:36okay this will do the same thing it's
- 3:00:38just written in less steps
- 3:00:41so these are known as anonymous objects
- 3:00:43they don't technically have a name
- 3:00:44so i may or may not make a separate
- 3:00:46video on anonymous objects
- 3:00:48so within our garage we should have a
- 3:00:50mustang corvette
- 3:00:52and a lambo so yeah that's how to create
- 3:00:54an array of objects
- 3:00:56in c sharp
- 3:00:59hey what's going on people in this video
- 3:01:01i'm going to show you all how we can
- 3:01:03pass
- 3:01:03an object as an argument so here's the
- 3:01:05rundown i have a class car
- 3:01:07cars have two fields a model and a color
- 3:01:10and a constructor to assign a model and
- 3:01:12color argument that we receive
- 3:01:14i have one car object car car one equals
- 3:01:17new car
- 3:01:18the model of this car is a mustang and
- 3:01:20the color is red
- 3:01:21so i'm going to create a method that
- 3:01:23will change the color of my car
- 3:01:25but i have to pass in a car object so
- 3:01:28let's create a method to handle that for
- 3:01:30us
- 3:01:31public static void it's not returning
- 3:01:34anything and let's call this method
- 3:01:36change color and we'll need two
- 3:01:39parameters
- 3:01:40a car that we would like to change the
- 3:01:42color of so the data type is going to be
- 3:01:45car as well as a name for this parameter
- 3:01:47let's name whatever car object that we
- 3:01:49receive as just
- 3:01:51simply car and we need a color as well
- 3:01:53that would be a string
- 3:01:54string color then to change the color of
- 3:01:58this car
- 3:01:59i would type car dot color
- 3:02:02equals whatever color that we receive
- 3:02:05and that's it
- 3:02:06so now when we invoke this method we
- 3:02:08have to pass in a car object as well as
- 3:02:10a color
- 3:02:12so that would be change color and the
- 3:02:15car object
- 3:02:16that i will pass in as an argument is
- 3:02:18car 1 the name of the object
- 3:02:20as well as a new color let's color this
- 3:02:22car silver
- 3:02:23and then let's display our cars color
- 3:02:26and
- 3:02:26model console.right
- 3:02:30car one dot color
- 3:02:34plus a lot of space plus car one
- 3:02:38dot model and this should change the
- 3:02:40color of our car
- 3:02:41to a silver mustang so to pass an object
- 3:02:45as an argument
- 3:02:46you need to make sure that you have the
- 3:02:47right parameter set up you type
- 3:02:49the data type of this object followed by
- 3:02:52a name for this parameter
- 3:02:54then when you invoke this method you
- 3:02:55have to pass in the name of the object
- 3:02:57let's try something a little bit
- 3:02:58different let's create a method to
- 3:03:00copy an object so let's get rid of this
- 3:03:03change color method but we'll keep our
- 3:03:05car object
- 3:03:07so we'll create a method to return a car
- 3:03:10object so public static
- 3:03:14instead of void the data type is the
- 3:03:17type that we're returning we're
- 3:03:18returning a car
- 3:03:20object so replace void with car
- 3:03:23then we need a name for this method
- 3:03:25let's call this copy
- 3:03:26so we will pass in a car object the data
- 3:03:29type
- 3:03:29is car and i will name this argument
- 3:03:32that we receive
- 3:03:33as car now one thing that we can do to
- 3:03:36copy a car object
- 3:03:37is return a new
- 3:03:40car and then we'll need to pass in
- 3:03:43a model and a color so the model
- 3:03:47of this car is car dot model and the
- 3:03:50color
- 3:03:51is car dot color
- 3:03:54now if i would like to copy car 1 and
- 3:03:57create a second car
- 3:03:58a copy of it i can type car car 2
- 3:04:02equals copy and then i will pass
- 3:04:05this object as an argument and then
- 3:04:08let's display the fields
- 3:04:09of car2 and i bet it's a red mustang
- 3:04:13oh yeah there we go so we successfully
- 3:04:15created a copy of car one
- 3:04:18by returning a car object so yeah that's
- 3:04:21how to pass objects as
- 3:04:22arguments you just have to list the data
- 3:04:24type of the object
- 3:04:26since we're working with cars we had to
- 3:04:27set up a cart parameter
- 3:04:29then if you would like to return an
- 3:04:31object instead of void you would replace
- 3:04:33void with the data type of the object
- 3:04:35you would like to return
- 3:04:37and if you would like to return a new
- 3:04:39object like what we did with this copy
- 3:04:41method
- 3:04:42you would return a new and then call the
- 3:04:45constructor of whatever object you would
- 3:04:47like to create and return
- 3:04:48so that's why we called the car
- 3:04:50constructor and then we returned that
- 3:04:52new car
- 3:04:53back to this spot here so it's kind of
- 3:04:55like we replaced this method with
- 3:04:58calling the car constructor something
- 3:05:00like that so yeah that's how to pass
- 3:05:02objects as arguments as well as return
- 3:05:05objects from methods too
- 3:05:07so yeah if this video helped you out
- 3:05:08feel free to help me out by smashing
- 3:05:10that like button
- 3:05:11leave a random comment down below and
- 3:05:13subscribe if you'd like to become a
- 3:05:14fellow bro
- 3:05:18right what's going on people we're
- 3:05:19talking about method overriding
- 3:05:21method overriding provides a new version
- 3:05:24of a method
- 3:05:25inherited from a parent class i have a
- 3:05:27class animal this is a parent class
- 3:05:29also known as a super class or base
- 3:05:31class and i have
- 3:05:33two child classes dog inherits from
- 3:05:35animal and
- 3:05:36cat inherits from animal so let's say
- 3:05:38that we have a method within
- 3:05:39our animal class so let's create one
- 3:05:42public
- 3:05:43void speak all animals can speak
- 3:05:48and then when a class that inherits this
- 3:05:50method
- 3:05:51uses this method let's display a message
- 3:05:54such as
- 3:05:55the animal goes
- 3:06:00okay let's create a dog object and a cat
- 3:06:03object
- 3:06:04dog dog equals new dog
- 3:06:08cat cat equals new cat
- 3:06:13dogs since they're inheriting from the
- 3:06:16animal class they have a speak method
- 3:06:18as well as cats cat dot speak
- 3:06:22so when both this dog object and this
- 3:06:24cat object
- 3:06:25invoke their speak methods that they
- 3:06:27inherit from the parent class of animal
- 3:06:30we will execute this method and when my
- 3:06:32dog
- 3:06:33and cat object speak that will display
- 3:06:36the animal goes
- 3:06:37burr okay so we can actually provide
- 3:06:40our own implementation of this method we
- 3:06:43can override this method and do
- 3:06:45something else
- 3:06:46now to overwrite a method it needs the
- 3:06:47same method signature
- 3:06:49the same name and the same parameters so
- 3:06:51if i would like to
- 3:06:52override this speak method within my dog
- 3:06:55class i'm going
- 3:06:56to type public override
- 3:07:01void the same type as the original
- 3:07:03method
- 3:07:04and the same name and signature now to
- 3:07:07create an overriding method
- 3:07:08the method that is inherited must be
- 3:07:11virtual
- 3:07:12abstract or already overridden so i'm
- 3:07:15going to add this modifier
- 3:07:17virtual so we can now override this
- 3:07:19method
- 3:07:20you can also do this with an abstract
- 3:07:22method too
- 3:07:24but the class needs to be abstract as
- 3:07:26well
- 3:07:27and there needs to be no body
- 3:07:30but we'll just stick with virtual in
- 3:07:32this video
- 3:07:35okay so we have our virtual method that
- 3:07:37means that
- 3:07:38this method can be overridden and now we
- 3:07:40can provide
- 3:07:41our own implementation of the speak
- 3:07:43method specifically for
- 3:07:45dogs so let's display a different
- 3:07:47message
- 3:07:48console.rightline the dog
- 3:07:51goes woof so notice that we don't have
- 3:07:55anything within
- 3:07:56our cat class now if both dog and cat
- 3:07:59use their speak methods
- 3:08:01the dog will instead speak and say the
- 3:08:04dog goes woof
- 3:08:05and our cat is still using the method
- 3:08:07that inherits from its parent animal
- 3:08:10if i would like to do the same thing for
- 3:08:12my cat objects i can override this
- 3:08:14method again
- 3:08:15public override void
- 3:08:19speak and then i can provide a different
- 3:08:23implementation
- 3:08:25the cat goes meow
- 3:08:30so after both dog and cat use their
- 3:08:33speak methods
- 3:08:34this displays the dog goes woof the cat
- 3:08:37goes meow
- 3:08:38so by using method overriding we can
- 3:08:40provide a new version of a method
- 3:08:42inherited from a parent class however
- 3:08:45the inheritance method
- 3:08:46must be abstract virtual or
- 3:08:49already overwritten and a method
- 3:08:51overriding is commonly used with
- 3:08:53the tostring method which we still need
- 3:08:55to talk about
- 3:08:56and polymorphism so yeah that's method
- 3:08:59overriding
- 3:09:00if you found this video helpful please
- 3:09:01smash that like button leave a random
- 3:09:03comment down below
- 3:09:04and subscribe if you'd like to become a
- 3:09:06fellow bro
- 3:09:09all right welcome back everybody the two
- 3:09:11string method the two string method
- 3:09:13is an inherent method of objects its job
- 3:09:16is to convert an object
- 3:09:18to its string representation so that it
- 3:09:20is suitable for display
- 3:09:22so i have a class car cars have four
- 3:09:25fields i make a model a year and a color
- 3:09:27and i have a constructor to assign some
- 3:09:30of these fields
- 3:09:31so what we could do if we would like
- 3:09:32some of this information we can override
- 3:09:34the built-in tostring method of objects
- 3:09:37that includes cars 2.
- 3:09:39so let's create a car object car car
- 3:09:42equals new card then i need to pass in a
- 3:09:45make
- 3:09:46model year and color let's say that this
- 3:09:48is a
- 3:09:49chevy corvette
- 3:09:53the year is 2022 and the color
- 3:09:56is blue so within a writeline statement
- 3:10:00if i type the name of my object
- 3:10:02car there's actually a tostring method
- 3:10:05and when we invoke this method let's
- 3:10:07take a look to see what happens
- 3:10:09so this displays the namespace followed
- 3:10:12by
- 3:10:12the type of the object that we're
- 3:10:14working with so we can actually
- 3:10:16override this method so that when we use
- 3:10:18the tostring method
- 3:10:19we can return a string representation of
- 3:10:22this object
- 3:10:23maybe we can display the make model year
- 3:10:25in color somehow
- 3:10:26so we'll need to override this method to
- 3:10:29string
- 3:10:30so let's do that here we're going to
- 3:10:32type
- 3:10:33public then to override a method we have
- 3:10:35to use that override keyword
- 3:10:38then type to string and this might
- 3:10:41auto-generate for you
- 3:10:42so we're returning a string and by
- 3:10:45default this will return
- 3:10:46base to string what we can do instead is
- 3:10:49return something else we can return
- 3:10:51some sort of string representation
- 3:10:53that's suitable for display
- 3:10:55let's say that when i use the tostring
- 3:10:57method of my car object
- 3:10:59i'd like to return a sentence that says
- 3:11:01this is a
- 3:11:02make plus model a chevy corvette so
- 3:11:05let's do something like this
- 3:11:07let's say string
- 3:11:10message equals this
- 3:11:13is a plus make
- 3:11:17then i'll just add a space plus model
- 3:11:20really you can do whatever you want and
- 3:11:22then i'm going to return
- 3:11:23this string or i could take all of this
- 3:11:27copy it and then return
- 3:11:33this is a make plus model i could do
- 3:11:35that too and that would work
- 3:11:37so when i use the tostring method it's
- 3:11:39going to return this sentence
- 3:11:40and insert make and model this is a
- 3:11:43chevy corvette
- 3:11:45one thing you can do too you don't
- 3:11:46necessarily need to directly invoke the
- 3:11:49tostring method
- 3:11:50you can just type the name of the object
- 3:11:52and that will do the same thing
- 3:11:54so this will behind the scenes called
- 3:11:55the tostring method
- 3:11:57so before when we displayed our object
- 3:12:00with a writeline statement it would
- 3:12:01display the namespace
- 3:12:02plus the type of the object but if you
- 3:12:04were to override
- 3:12:06the tostring method you can return some
- 3:12:08sort of sentence or string
- 3:12:10representation of your object
- 3:12:11that is suitable for display so yeah
- 3:12:14that is the
- 3:12:15tostring method it converts an object to
- 3:12:18a string representation
- 3:12:19that is suitable for display and really
- 3:12:21you can type whatever you want here just
- 3:12:23be sure to return a string
- 3:12:24so yeah that is the two string method if
- 3:12:27you found this video helpful please be
- 3:12:28sure to give it a thumbs up drop a
- 3:12:30random comment down below and subscribe
- 3:12:32if you'd like to become a fellow bro
- 3:12:34hey what's going on everybody it's you
- 3:12:36bro hope you're doing well
- 3:12:37and in this video i'm going to explain
- 3:12:39to polymorphism in c sharp
- 3:12:41so sit back relax and enjoy the show
- 3:12:46all right what's going on people
- 3:12:48polymorphism
- 3:12:49polymorphism is a greek word that means
- 3:12:52to have many forms
- 3:12:53with programming objects can be
- 3:12:55identified by more than one type
- 3:12:57they can have many forms for example
- 3:13:00let's say that we have a dog
- 3:13:02a dog could be considered a canine an
- 3:13:04animal
- 3:13:05and an organism so a dog has more than
- 3:13:07just
- 3:13:08one form it has more than one data type
- 3:13:10here's the situation we're going to have
- 3:13:12a race
- 3:13:13we'll enter in a car object bicycle
- 3:13:15object and boat object
- 3:13:17so i have four classes vehicle is the
- 3:13:19base class
- 3:13:20otherwise known as the parent class and
- 3:13:23car bicycle and boat all inherit from
- 3:13:25the vehicle class
- 3:13:26so with my race i need to create a car
- 3:13:28object bicycle object and
- 3:13:30boat object so let's do that car car
- 3:13:34equals new car
- 3:13:37bicycle bicycle equals
- 3:13:40new bicycle
- 3:13:43and boat boat equals new boat
- 3:13:47i would like to place all of these
- 3:13:49objects within an array for my race
- 3:13:51but what's the data type of the array
- 3:13:53going to be so let's try and make an
- 3:13:55array of cars and add our car
- 3:13:57bicycle and boat and let's just see what
- 3:13:59happens i will name this array
- 3:14:01just vehicles equals and then within
- 3:14:05curly braces
- 3:14:06add whatever objects you would like i
- 3:14:08would like to enter my car into the race
- 3:14:10my bicycle and my boat
- 3:14:13okay but we're running into some errors
- 3:14:15here cannot implicitly convert
- 3:14:17bicycle to car so i stated that the
- 3:14:21data type of this array is going to be
- 3:14:23cars i can only enter
- 3:14:25car objects so i have no issue there but
- 3:14:27i cannot enter in
- 3:14:28my bicycle and boat objects into this
- 3:14:31array
- 3:14:31but one way in which we can solve this
- 3:14:33is by using polymorphism
- 3:14:36so we can find what they all have in
- 3:14:38common they all
- 3:14:39identify as vehicles because they all
- 3:14:42inherit from the vehicle class
- 3:14:44these objects have more than one form
- 3:14:46they can be identified by more than one
- 3:14:48type
- 3:14:48so instead of my array being an array of
- 3:14:50cars i'm going to change this
- 3:14:52to vehicle because well these three
- 3:14:55objects
- 3:14:56identify as well a car a bicycle and a
- 3:14:58boat respectively
- 3:15:00but also vehicles because they inherit
- 3:15:02from the vehicle class
- 3:15:03so you can see that this is valid if you
- 3:15:05need to create an array of
- 3:15:07different types of objects you have to
- 3:15:09find what they have in common
- 3:15:10and then you can just use that as a data
- 3:15:12type they are all also considered
- 3:15:14vehicles
- 3:15:15let's move on to part two what i would
- 3:15:17like to do is have all of these vehicles
- 3:15:19use a go method
- 3:15:21so they will all begin the race at the
- 3:15:23same time so let's create a go method
- 3:15:25within each of these classes
- 3:15:27public void go
- 3:15:31and we'll just display the car
- 3:15:35is moving place that within the car
- 3:15:37class
- 3:15:38let's make a go method for bicycles
- 3:15:41change
- 3:15:41car to bicycle
- 3:15:44and then the boat is moving
- 3:15:48okay now to iterate over this array of
- 3:15:50vehicles
- 3:15:51we can use a for each loop for each
- 3:15:54now what's the data type of our vehicle
- 3:15:57array
- 3:15:57well it's vehicles vehicle vehicle
- 3:16:01in vehicles and now i would like
- 3:16:04each vehicle to use its go method
- 3:16:08vehicle.go so each vehicle will use its
- 3:16:11go method
- 3:16:12but there's one more thing that we need
- 3:16:14to do we need a go method within our
- 3:16:17vehicle class
- 3:16:18so all of these methods within our car
- 3:16:21bicycle
- 3:16:21and boat classes all are going to be
- 3:16:24overriding
- 3:16:25a go method found within the vehicle
- 3:16:27class so let's create a
- 3:16:29virtual method public
- 3:16:32virtual void
- 3:16:35go and we don't necessarily need to
- 3:16:39define anything within the go method
- 3:16:42because we're going to be overriding
- 3:16:43this method okay
- 3:16:45then we need to use this override
- 3:16:48modifier
- 3:16:52all right now when we iterate over this
- 3:16:55array of vehicles
- 3:16:56each vehicle will use its go method it's
- 3:16:59as if when we would like to begin our
- 3:17:01race
- 3:17:01all of the vehicles within our array
- 3:17:03will use their go methods
- 3:17:05at the same time the car is moving the
- 3:17:07bicycle is moving the boat is moving
- 3:17:10so yeah that's polymorphism it's a greek
- 3:17:12word that means to have many forms
- 3:17:14objects can be identified by more than
- 3:17:16one type
- 3:17:18and in this example we created an array
- 3:17:20of vehicles
- 3:17:21our car identifies as a car but it also
- 3:17:24identifies as a vehicle
- 3:17:25and objects do identify as objects too
- 3:17:28so cars identify as objects
- 3:17:30and the same thing can apply with
- 3:17:32bicycle and boats in our example
- 3:17:34so yeah that's polymorphism if you found
- 3:17:37this video helpful be sure to smash that
- 3:17:39like button leave a random comment down
- 3:17:40below
- 3:17:41and subscribe if you'd like to become a
- 3:17:43fellow bro
- 3:17:46all right interfaces an interface is
- 3:17:49much like a parent class
- 3:17:51or base class it defines a sort of
- 3:17:54contract
- 3:17:54that all classes inheriting from need to
- 3:17:57follow
- 3:17:58an interface can contain properties
- 3:18:00methods events
- 3:18:01and if there's any of those declared
- 3:18:03within an interface
- 3:18:05anything that inherits from that
- 3:18:06interface needs to be implemented within
- 3:18:09an inheriting class
- 3:18:10an interface declares what a class
- 3:18:13should have
- 3:18:14and the class that it inherits from
- 3:18:16defines how it should do it exactly
- 3:18:19so here's an example we'll create three
- 3:18:21classes
- 3:18:22rabbit hawk and fish and two interfaces
- 3:18:26prey and predator
- 3:18:33alright now let's create two interfaces
- 3:18:36to create an interface you type
- 3:18:38interface and then the name of the
- 3:18:39interface however a common naming
- 3:18:42convention with
- 3:18:43interfaces is that you precede the
- 3:18:45interface name
- 3:18:46with capital i so if we would like a
- 3:18:49prey interface
- 3:18:50we would type i pray and then let's
- 3:18:53create a predator interface
- 3:18:55interface i predator
- 3:18:58now with these interfaces we can pretty
- 3:19:00much declare anything within these
- 3:19:02interfaces but if one of these classes
- 3:19:04would like to inherit from this
- 3:19:06interface they need to implement
- 3:19:08anything declared within them
- 3:19:10so let's say that if you're prey then
- 3:19:13you need a method to flee because you're
- 3:19:15fleeing from predators
- 3:19:17i'm going to declare a flea method void
- 3:19:20flee we will declare it but not
- 3:19:23implement it
- 3:19:24implementing this method is the job of
- 3:19:26the inheriting class
- 3:19:27that would like to use this interface
- 3:19:30let's say that
- 3:19:30rabbits will inherit this interface i
- 3:19:33pray
- 3:19:34colon i pray and in order to do so we
- 3:19:38need to implement
- 3:19:39this method of fleet within the rabbit
- 3:19:41class in order to use it
- 3:19:44public void flea
- 3:19:47and then be sure to add a body to this
- 3:19:49method so when we invoke this method
- 3:19:52let's write the rabbit runs away
- 3:19:57okay now we can create a rabbit object
- 3:20:01rabbit rabbit equals
- 3:20:04new rabbit and i bet there's a flea
- 3:20:08method
- 3:20:08rabbit dot flea
- 3:20:11and let's try it the rabbit runs away
- 3:20:15now let's have our hawk class inherit
- 3:20:17this predator interface
- 3:20:19now with our predator interface let's
- 3:20:21create a method
- 3:20:23to hunt void hunt
- 3:20:26we will declare it but not implemented
- 3:20:28implementing this method is the job of
- 3:20:30the classes inheriting
- 3:20:31this interface so hawk will inherit the
- 3:20:35i predator interface but we need to
- 3:20:38implement
- 3:20:39that hunt method public
- 3:20:42void hunt
- 3:20:46and then let's display the hawk
- 3:20:49is searching for food
- 3:20:52and we can create a hawk object hawk
- 3:20:56hawk equals new hawk
- 3:21:00so let's take a look at our hawk object
- 3:21:03hawk
- 3:21:03dot and there's no flea method because
- 3:21:06we're not implementing
- 3:21:07the prey interface we're implementing
- 3:21:09the predator interface
- 3:21:11so there is a hunt method and not a flea
- 3:21:14method
- 3:21:16hawk.hunt
- 3:21:19the hawk is searching for food now with
- 3:21:21interfaces
- 3:21:22you can inherit more than one unlike
- 3:21:24with standard inheritance
- 3:21:25so fish they could be both prey and
- 3:21:28predators
- 3:21:29to inherit two interfaces after the
- 3:21:31colon you can separate each with a comma
- 3:21:34so fish will inherit the i prey and i
- 3:21:37predator interfaces but now they need to
- 3:21:40implement
- 3:21:40both of these methods flea and hunt so
- 3:21:43let's do so
- 3:21:45public void flea
- 3:21:51the fish swims away
- 3:21:54and now we need to implement that hunt
- 3:21:56method
- 3:21:58public void hunt
- 3:22:06the fish is searching
- 3:22:09for smaller fish
- 3:22:13all right now let's create a fish object
- 3:22:16fish
- 3:22:16fish equals new fish
- 3:22:23so fish have both a flea and a hunt
- 3:22:25method
- 3:22:27fish dot flea fish dot hunt
- 3:22:32the fish swims away the fish is
- 3:22:34searching for smaller fish
- 3:22:36so an interface defines a sort of
- 3:22:38contract that all classes inheriting
- 3:22:40from
- 3:22:41should follow an interface declares what
- 3:22:44a class
- 3:22:44should have and the inheritance class
- 3:22:47defines
- 3:22:48how it should do it the benefits of
- 3:22:50using interfaces
- 3:22:51for one is that you can inherit multiple
- 3:22:53interfaces
- 3:22:55unlike with standard inheritance there's
- 3:22:57added security
- 3:22:59and this gives us a plug-and-play style
- 3:23:01of coding because
- 3:23:02anything that has the same interface
- 3:23:05will definitely have the same
- 3:23:06implemented methods and properties
- 3:23:08so you can swap these out for like a
- 3:23:10different creature if you would like
- 3:23:12so yeah those are interfaces if you
- 3:23:14found this video helpful please be sure
- 3:23:15to give this video a thumbs up
- 3:23:17leave a random comment down below and
- 3:23:19subscribe if you'd like to become a
- 3:23:20fellow bro
- 3:23:24all right welcome back everybody a list
- 3:23:27a list is a
- 3:23:28data structure that represents a list of
- 3:23:31objects
- 3:23:31that can be accessed by index it's
- 3:23:34similar to an array
- 3:23:35but they can increase and decrease
- 3:23:37dynamically in size during runtime
- 3:23:40which rays cannot do so here's an
- 3:23:42example of an array an array has a
- 3:23:43static
- 3:23:44fixed size and we cannot change it
- 3:23:46normally after runtime
- 3:23:48so let's create a standard array just
- 3:23:50for this example let's create an array
- 3:23:52of food
- 3:23:52string food equals new
- 3:23:56string and let's say that this array has
- 3:23:58a size of three
- 3:24:00so i'm going to add some elements to
- 3:24:02this array
- 3:24:11and then i can display these elements
- 3:24:13with a symbol for each loop
- 3:24:19pizza hamburger and hot dog so if i
- 3:24:22attempt to add another element to this
- 3:24:24array this is what happens
- 3:24:29we'll run into an exception and index
- 3:24:32out of range exception
- 3:24:33because index was outside the bounds of
- 3:24:35the array
- 3:24:36so you can't normally change the size of
- 3:24:38an array another option is to use a list
- 3:24:40a list can increase and decrease
- 3:24:42dynamically in size during runtime
- 3:24:45so let's create a list step one is that
- 3:24:48we'll need to import
- 3:24:50system.collections.generic so include
- 3:24:53this at the top i'll place it right
- 3:24:54underneath
- 3:24:55using system and this is how to declare
- 3:24:58a list
- 3:24:59list then within angle brackets list the
- 3:25:02type of object you would like to store
- 3:25:04strings and then i will name this list
- 3:25:07food
- 3:25:08equals new list
- 3:25:12angle brackets the data type again
- 3:25:16parentheses semicolon and we now have
- 3:25:19a list named food and to add elements to
- 3:25:22this list there is a built in add method
- 3:25:25food dot add then within
- 3:25:29this method pass in the object you would
- 3:25:31like to store
- 3:25:32i would like to store let's say a pizza
- 3:25:35then a hamburger then a hot dog
- 3:25:38so food dot add pizza hamburger
- 3:25:42and hot dog
- 3:25:46and let's take a look so we have our
- 3:25:49pizza
- 3:25:50hamburger and hot dog and i bet we can
- 3:25:52add even more than three elements
- 3:25:57so it appears that our list can resize
- 3:25:59dynamically which is a bonus
- 3:26:01then to access one of these elements
- 3:26:03it's much like an array you use that set
- 3:26:05of straight brackets
- 3:26:06so i'm going to display whatever's
- 3:26:08within element
- 3:26:09number zero food index zero
- 3:26:14and that would be pizza so accessing an
- 3:26:17element is the same as an array you just
- 3:26:19type the list name
- 3:26:20and then add a set of straight brackets
- 3:26:22then the index number okay so here's a
- 3:26:24few other useful methods of lists
- 3:26:27there is a remove method food dot remove
- 3:26:31then type in what you would like to
- 3:26:33remove i would like to remove
- 3:26:34fries and let's see if that's still in
- 3:26:37here
- 3:26:39nope fries is missing pizza hamburger
- 3:26:42hot dog
- 3:26:43so that is the add method and remove
- 3:26:45method but there's a few other useful
- 3:26:47methods
- 3:26:48so we can insert an object at a given
- 3:26:50element
- 3:26:51let's insert maybe sushi at index 1 via
- 3:26:54the insert method food dot insert
- 3:26:58then we need an index and an item
- 3:27:02so at the beginning of our list at index
- 3:27:050
- 3:27:06i will insert sushi
- 3:27:11sushi pizza hamburger hot dog fries
- 3:27:14we can get the current size of our array
- 3:27:17using the
- 3:27:18count property so within a writeline
- 3:27:20statement
- 3:27:21i'm going to display food dot count
- 3:27:24property and the current size
- 3:27:28of our list is four so that is the count
- 3:27:32property we can find the index of an
- 3:27:35element
- 3:27:37food dot index of
- 3:27:41and let's find where pizza is
- 3:27:46so that is at index zero you can also
- 3:27:50find
- 3:27:50the last index of a given item so what
- 3:27:53if we have fries both
- 3:27:54in the beginning and at the end food dot
- 3:27:58add fries
- 3:28:02so i have that in the beginning and at
- 3:28:04the end so i'm going to use
- 3:28:06the last index of method
- 3:28:09food dot last index
- 3:28:12of fries
- 3:28:15and the last index surprise is at four
- 3:28:19zero one two three four so let me get
- 3:28:23rid of that okay we can check to see
- 3:28:25if our list contains a given item
- 3:28:28using the contains method food dot
- 3:28:31contains pizza
- 3:28:35this will return a boolean true if pizza
- 3:28:38is within our list and false if not
- 3:28:40pizza is within our list so that returns
- 3:28:43true
- 3:28:44and that is the contains method then we
- 3:28:46have sword
- 3:28:47food dot sword this will sort our list
- 3:28:51alphabetically
- 3:28:53so our list in alphabetical order is
- 3:28:55fries hamburger hot dog pizza
- 3:28:57and we can sort in reverse order
- 3:29:00food.reverse
- 3:29:04and our list in reverse order is fry's
- 3:29:07hot dog hamburger pizza
- 3:29:09we can clear our list food dot
- 3:29:13clear
- 3:29:16and now our list is empty and lastly
- 3:29:20we can convert our list into an array so
- 3:29:23i'm going to declare a new array
- 3:29:25string let's say food array
- 3:29:29equals food dot
- 3:29:322 array
- 3:29:36string item in food array so we have
- 3:29:38pizza hamburger hot dog fries
- 3:29:41well okay then everybody that is a list
- 3:29:44it's similar to an array it's a data
- 3:29:46structure that represents a list of
- 3:29:48objects
- 3:29:49that can be accessed by index it's
- 3:29:51similar to an array
- 3:29:52but they can increase and decrease
- 3:29:54dynamically in size
- 3:29:56but they waste more memory so yeah those
- 3:29:58are lists in c
- 3:29:59sharp if you found this video helpful
- 3:30:00please be sure to give it a thumbs up
- 3:30:02drop a random comment down below and as
- 3:30:04always subscribe if you'd like to become
- 3:30:06a fellow bro
- 3:30:09all right what's going on everybody so
- 3:30:11in this video i'm going to show you all
- 3:30:12how we can create a list of objects from
- 3:30:15a custom class of ours
- 3:30:16in the last video i showed you all how
- 3:30:18we can create a list of strings
- 3:30:19but what if we would like to create a
- 3:30:21list of players to keep track of the
- 3:30:23amount of players in our game and their
- 3:30:24names
- 3:30:25so let's create a list list then we need
- 3:30:28angle brackets and
- 3:30:30the data type of the object we would
- 3:30:31like to store within our list
- 3:30:33i would like to store player objects and
- 3:30:35i'll name
- 3:30:36this list players because we're keeping
- 3:30:38track of the players
- 3:30:39players equals new list
- 3:30:44angle brackets our data type again
- 3:30:46player
- 3:30:47parentheses semicolon so we now have a
- 3:30:50list
- 3:30:50that can store player objects so let's
- 3:30:53instantiate some player objects
- 3:30:54and i have a field setup username you
- 3:30:57can also set up getters and setters too
- 3:30:59if you prefer
- 3:31:00so let's create some player objects
- 3:31:01player and we'll call this player
- 3:31:03player1
- 3:31:04equals new player but we need to pass in
- 3:31:07a username as an argument
- 3:31:09let's say that this is chad and i'll
- 3:31:11create maybe
- 3:31:12two more players so player one
- 3:31:16player two player three player two will
- 3:31:18be what about steve
- 3:31:20and karen okay now to add
- 3:31:24our player objects to our list type the
- 3:31:26name of the list
- 3:31:27dot add and then pass in the name of the
- 3:31:31object
- 3:31:32players dot add player 1 then do the
- 3:31:35same thing with player 2 and player 3.
- 3:31:38then i'll use a for each loop to display
- 3:31:40all these for each
- 3:31:44the data type is player player
- 3:31:48in players so let's take a look at this
- 3:31:52i'm going to
- 3:31:53console.writeline each player but since
- 3:31:56we're displaying each object directly
- 3:31:59what's going to be displayed is the
- 3:32:01namespace
- 3:32:02plus the data type of our object we can
- 3:32:05access
- 3:32:05the username field
- 3:32:09and that will display each player's
- 3:32:10username or we can overwrite the
- 3:32:13tostring method so we have chad steve
- 3:32:16and kieran
- 3:32:17or we can override the tostring method
- 3:32:20so let's try that too public
- 3:32:24override to string
- 3:32:28and i'm going to return
- 3:32:31username so then when i use
- 3:32:34console.writeline i can just type in
- 3:32:36the object's name
- 3:32:41so we have chad steve and karen now
- 3:32:43another thing that you can do
- 3:32:44when we instantiate these player objects
- 3:32:47we can do so anonymously
- 3:32:49so within the add method of our list
- 3:32:52players
- 3:32:52we can simply just pass in new
- 3:32:56player then pass in a name and then we
- 3:32:58don't necessarily need to create a
- 3:33:00name for each of these objects so i'm
- 3:33:02going to pass in
- 3:33:03new player steve and new player karen
- 3:33:08so this is optional but i like to create
- 3:33:10anonymous objects
- 3:33:11it's less work so we should have our
- 3:33:14three players
- 3:33:15chad steve and karen so yeah that's how
- 3:33:18to add objects to a list
- 3:33:20within the angle brackets you just list
- 3:33:22the type of object that you would like
- 3:33:24to store within your list
- 3:33:25so yeah that's a list of objects in c
- 3:33:27sharp if you found this video helpful
- 3:33:29please be sure to smash that like button
- 3:33:31leave a random comment down below
- 3:33:32and subscribe if you'd like to become a
- 3:33:34fellow bro
- 3:33:37all right we have a lot to talk about
- 3:33:40today getters and setters but before i
- 3:33:42explain what these are let me explain
- 3:33:44the situation
- 3:33:45we have a class car and we have one
- 3:33:48field within our car class
- 3:33:49speed so when we construct a car object
- 3:33:52we need to pass in something for the
- 3:33:54speed
- 3:33:55maybe this is miles per hour kilometers
- 3:33:57per hour doesn't really matter
- 3:33:59so let's say that somebody takes our car
- 3:34:01object
- 3:34:02and changes the speed to like whatever
- 3:34:05this number is
- 3:34:06100 million actually let's make that a
- 3:34:09billion
- 3:34:09how can we prevent people from doing
- 3:34:11this because currently our car is going
- 3:34:13way too fast
- 3:34:15so one way in which we could do that is
- 3:34:18to change
- 3:34:18this field from public to private
- 3:34:21but then we can't access it at all
- 3:34:24another way is to set up
- 3:34:25getters and setters and this adds
- 3:34:28security to our program
- 3:34:29via this concept called encapsulation so
- 3:34:32we need to first
- 3:34:33set up a property for this field the
- 3:34:36property name
- 3:34:37is the same as the field name except
- 3:34:39it's capital
- 3:34:40so type public if you're returning
- 3:34:44something
- 3:34:44list the data type then speed and then
- 3:34:47let's make this capital
- 3:34:49then add a set of curly braces a
- 3:34:51property combines the aspects
- 3:34:53of both fields and methods and it shares
- 3:34:56the same name with the field
- 3:34:57so it's kind of like something in
- 3:34:59between a field and a method
- 3:35:01and it contains accessors a get and
- 3:35:04set accessor now to first assign a value
- 3:35:07we'll need to change our constructor
- 3:35:09instead of assigning the field speed we
- 3:35:12will assign
- 3:35:12the property speed so this has a capital
- 3:35:16s
- 3:35:16let's create a get accessor first when
- 3:35:19we would like to
- 3:35:20get the value of speed whatever is
- 3:35:22contained within speed
- 3:35:24we will use this get accessor so type
- 3:35:26get
- 3:35:27curly braces and we will return
- 3:35:30speed and then we'll need to assign this
- 3:35:33with
- 3:35:34the set accessor then make sure you have
- 3:35:36that semicolon at the end
- 3:35:38so by using this get accessor this
- 3:35:41will read whatever value is within our
- 3:35:44field
- 3:35:45then we'll need a set accessor if we
- 3:35:47would like this to be
- 3:35:48writable set curly braces so what we'll
- 3:35:51do here
- 3:35:52is set speed equal to value
- 3:35:56so value is kind of like a parameter
- 3:35:58properties combine the aspects of both
- 3:36:00fields and methods
- 3:36:02so when we assign a value to speed
- 3:36:05well then this value is going to be
- 3:36:08this so it's kind of like an argument
- 3:36:10and a parameter
- 3:36:11and within our set accessor we can
- 3:36:13actually set up like some code or some
- 3:36:15rules or something
- 3:36:16so if somebody attempts to change the
- 3:36:18speed we could write an if
- 3:36:20statement like if value
- 3:36:23is greater than let's say 500 then we
- 3:36:26will limit this
- 3:36:28let's change speed equal to 500 then
- 3:36:32else speed
- 3:36:35equals value
- 3:36:40now car.speed is inaccessible due to its
- 3:36:42protection level
- 3:36:44so we need to access instead of the
- 3:36:46field the property so that's
- 3:36:48speed with a capital s and then if we're
- 3:36:50going to
- 3:36:51display the value of speed we'll access
- 3:36:53the property
- 3:36:54instead of the field so if we attempt to
- 3:36:57set our speed equal to 1 billion
- 3:37:00well we will access this set accessor
- 3:37:03and then it's going to be limited to 500
- 3:37:05then so if we attempt to change our
- 3:37:07speed to 1 billion
- 3:37:09it will be limited to 500 and then we
- 3:37:11can still display the value
- 3:37:13and access it because by having a getter
- 3:37:16and setter
- 3:37:16this is both readable and writable
- 3:37:20so yeah those are getters and setters
- 3:37:23they are accessors
- 3:37:24found within a property a property
- 3:37:26combines the aspect of both
- 3:37:28fields and methods and if you have a
- 3:37:30private field
- 3:37:31you can also set up a property that has
- 3:37:34getters and setters
- 3:37:36for more security so yeah those are
- 3:37:38getters and setters
- 3:37:40in c sharp
- 3:37:43all right everybody we have to talk
- 3:37:45about auto implemented properties
- 3:37:47this is an intimidating name but don't
- 3:37:49worry it's actually fairly simple
- 3:37:51they are shortcuts when no additional
- 3:37:53logic
- 3:37:54is required within a property you do not
- 3:37:57have to define a field for a property
- 3:37:59and you only have to write get and or
- 3:38:01set inside the property
- 3:38:03here's an example i have a class car and
- 3:38:04what if we would like a
- 3:38:06model field and property so that might
- 3:38:08look something like this
- 3:38:10make sure to not make your field public
- 3:38:12for this example
- 3:38:13so this will be a string named model for
- 3:38:15a model of car
- 3:38:17and i would like to set up a property
- 3:38:19that has get and set accessors
- 3:38:21so that would be public string
- 3:38:26model with a capital m curly braces and
- 3:38:30then i need a get and set accessor
- 3:38:33all we'll do within the get accessor is
- 3:38:36return
- 3:38:38model and that's the field and a set
- 3:38:41will be model r field
- 3:38:45equals value and within my constructor i
- 3:38:49will set
- 3:38:49this dot model property make sure that
- 3:38:52you have capital m
- 3:38:54equals model whatever our parameter is
- 3:38:58so if you're not doing anything else
- 3:39:00within your getter and setter there's
- 3:39:01actually a shortcut to all of this
- 3:39:03and that's by using an auto implement
- 3:39:05property so this is what we'll do we're
- 3:39:07going to instead
- 3:39:08get rid of all this and type
- 3:39:12public string model
- 3:39:16with a capital m curly braces
- 3:39:20get semicolon set semicolon
- 3:39:23and this will do the same thing however
- 3:39:25it's a lot less to write
- 3:39:27so if there's no additional logic that
- 3:39:29you need besides
- 3:39:30you know setting the value and getting
- 3:39:33the current
- 3:39:33field well you can just use an auto
- 3:39:35implemented property
- 3:39:36and this will do the same thing let's
- 3:39:38test it so let's create a car object
- 3:39:40car car equals new car then pass in
- 3:39:44a model of car let's say a porsche this
- 3:39:46time
- 3:39:47and then i would like to display the
- 3:39:49car's model car
- 3:39:51dot model property with a capital m
- 3:39:54so there will still be a model field but
- 3:39:56it's hidden
- 3:39:58and this car's model is a porsche
- 3:40:01so that's like a shortcut you can do an
- 3:40:03auto implemented property is a shortcut
- 3:40:06when
- 3:40:06no additional logic is required in the
- 3:40:09property
- 3:40:10you do not have to define a field for a
- 3:40:12property and you only have to write
- 3:40:14get and or set inside the property so
- 3:40:17yeah that is an
- 3:40:18auto implemented property if you liked
- 3:40:20this video please give it a thumbs up
- 3:40:22drop a random comment down below and
- 3:40:23subscribe if you'd like to become a
- 3:40:25fellow bro
- 3:40:29all right what's going on people enums
- 3:40:31enums are a special kind of class
- 3:40:34that contains a grouping of named
- 3:40:36integer constants we tend to use enums
- 3:40:39when we have values
- 3:40:40that we know will not change so here's
- 3:40:42an example let's create an enum of
- 3:40:44planets
- 3:40:45and we will associate a planet number
- 3:40:47with each named planet
- 3:40:48so to create an enum type enum then a
- 3:40:51unique name kind of like we're creating
- 3:40:52a class
- 3:40:53then curly braces so i'm going to add
- 3:40:56the name of each planet within our solar
- 3:40:58system beginning with mercury and then
- 3:41:00separate each with a comma
- 3:41:01so we have mercury venus earth then just
- 3:41:04continue on in this pattern
- 3:41:09this isn't necessary but i like to place
- 3:41:11each of my members on a new line within
- 3:41:13an
- 3:41:14enum i think it's easier to read
- 3:41:17okay we now have an enum of planets and
- 3:41:20there's an associated integer with each
- 3:41:22of these members
- 3:41:23if we do not explicitly set an integer
- 3:41:26by default the first
- 3:41:28member is zero then one two three then
- 3:41:31you just follow that pattern
- 3:41:32so let's access one of these members of
- 3:41:34our enum of planets
- 3:41:36let's display a message planets
- 3:41:39dot pluto
- 3:41:43is a planet so taxes
- 3:41:47a name of one of these members you type
- 3:41:50the name of your enum planets dot the
- 3:41:52name of your named member pluto
- 3:41:54this will return the name not the
- 3:41:57integer
- 3:41:58let's try it pluto is a planet
- 3:42:01this is no different from using the two
- 3:42:04string method this will do the same
- 3:42:05thing
- 3:42:06pluto is a planet okay now if we need
- 3:42:08the associated integer
- 3:42:10with each of these named members we
- 3:42:12would cast this member
- 3:42:14as an integer let's change our message
- 3:42:16around
- 3:42:17let's say that planets.mercury
- 3:42:21is planet number
- 3:42:24than to access one of the integers
- 3:42:27stored within a member
- 3:42:28that would be planets dot the name of a
- 3:42:31planet
- 3:42:32mercury and then we will precede this
- 3:42:34with parentheses
- 3:42:36and this will convert our named member
- 3:42:39into
- 3:42:39an integer so our message is now
- 3:42:43mercury is planet number zero so these
- 3:42:46named members
- 3:42:47always begin with zero but we can change
- 3:42:49that and set them
- 3:42:50let's say that mercury equals one venus
- 3:42:54equals two and then continue on in this
- 3:42:56pattern
- 3:43:00let's display pluto as well because
- 3:43:03pluto is feeling left out
- 3:43:04planets dot pluto is planet number
- 3:43:09planets dot pluto then cast as an
- 3:43:12integer
- 3:43:14okay mercury is planet number one pluto
- 3:43:16is planet number nine
- 3:43:18let's try something a little bit more
- 3:43:19complex let's create an enum to keep
- 3:43:22track of the radius of each planet
- 3:43:25enum planet radius
- 3:43:29and let me just copy this
- 3:43:33okay here's the radius of each planet in
- 3:43:35kilometers
- 3:43:36mercury is two four three nine
- 3:43:40six zero five one for venus
- 3:43:44and then i'll just fast forward the
- 3:43:45footage you can copy this down if you'd
- 3:43:46like
- 3:43:55all right we have the radius and
- 3:43:57kilometers of each planet
- 3:44:00so these are in name integer pairs
- 3:44:03let's create a variable to store the
- 3:44:05name of one of our planets
- 3:44:07string name equals what about earth
- 3:44:10planet radius the name of our enum dot
- 3:44:13the name of our member
- 3:44:15earth so if we're going to store this
- 3:44:17within a string variable we need to use
- 3:44:19the
- 3:44:19to string method and then
- 3:44:23let's display this just to test it let's
- 3:44:26say
- 3:44:27planet colon space plus name
- 3:44:31and this should display just earth
- 3:44:34planet
- 3:44:35earth okay let's get the radius of earth
- 3:44:40int radius
- 3:44:43planetradius dot earth
- 3:44:47then cast this as an integer
- 3:44:53console.rightline let's say radius
- 3:44:57colon space plus radius
- 3:45:01radius 6371 let me add kilometers
- 3:45:06so that's kind of the nice thing about
- 3:45:08enums is that we don't necessarily need
- 3:45:10to remember all of these numbers
- 3:45:12it's kind of like restoring variables so
- 3:45:14to say they are name
- 3:45:15integer constants that will not change
- 3:45:17through the life of a program
- 3:45:19now let's move on to a challenge round
- 3:45:20let's create a method that will
- 3:45:22calculate
- 3:45:23the volume of one of our planets when we
- 3:45:25pass in a radius
- 3:45:27public static will return a double
- 3:45:32and this will be the volume method and
- 3:45:35there is one parameter
- 3:45:36the parameter is planet radius and we
- 3:45:40will name this just radius
- 3:45:43okay we need to return let's say volume
- 3:45:49and double volume equals
- 3:45:53and here's the formula to calculate the
- 3:45:56volume of a sphere 4.0 divided by
- 3:46:003.0 times math
- 3:46:03dot pi times
- 3:46:06our radius to the power of three so we
- 3:46:08can use math
- 3:46:10dot power pass in
- 3:46:13our radius make sure to cast this as an
- 3:46:16integer
- 3:46:18to the power of three
- 3:46:23and let's call the volume method and
- 3:46:25store this within
- 3:46:26a double named volume
- 3:46:29double volume equals we will invoke the
- 3:46:32volume method and pass in planet
- 3:46:36radius dot earth
- 3:46:40and then we'll display the volume
- 3:46:42console.writeline
- 3:46:44volume colon space plus
- 3:46:48volume and i think that's in cubed
- 3:46:51kilometers
- 3:46:55all right and the volume of earth
- 3:46:59is whatever number this is one zero
- 3:47:02eight three something something
- 3:47:03something something
- 3:47:05cubed kilometers so yeah those are enums
- 3:47:08they're like a special class that
- 3:47:09contains a
- 3:47:10set or grouping of named integer
- 3:47:13constants
- 3:47:14we tend to use enums when we have values
- 3:47:16that we know will not change
- 3:47:18and to get the integer value from an
- 3:47:20item you must
- 3:47:21explicitly convert it to an integer so
- 3:47:24yeah those are enums if you found this
- 3:47:26video helpful
- 3:47:27please be sure to smash that like button
- 3:47:29leave a random comment down below
- 3:47:30and subscribe if you'd like to become a
- 3:47:32fellow bro
- 3:47:35if you wouldn't mind please like comment
- 3:47:38and subscribe
- 3:47:39one like equals one prayer for the
- 3:47:41youtube algorithm
- 3:47:43uh generics all right
- 3:47:46let's get started generics anything
- 3:47:48that's generic
- 3:47:49is not specific to any particular data
- 3:47:52type
- 3:47:53we can make something generic by adding
- 3:47:54a set of angle brackets
- 3:47:56then t we can make classes generic
- 3:47:58methods
- 3:47:59fields whatever we want this allows for
- 3:48:02greater code reusability for different
- 3:48:04data types
- 3:48:05let's say that we have three different
- 3:48:06types of arrays an array of integers
- 3:48:08doubles and strings let's create those
- 3:48:11int
- 3:48:12array i'll call this interray
- 3:48:15equals i don't know one two three
- 3:48:18the elements really aren't important the
- 3:48:20data type is
- 3:48:21okay then let's say we have an array of
- 3:48:24doubles
- 3:48:25i will name this double array and
- 3:48:28the elements are 1.0 2.0
- 3:48:32and 3.0 then let's say that we have an
- 3:48:36array of strings
- 3:48:38string array equals
- 3:48:43one two
- 3:48:46and three okay what if i would like to
- 3:48:50display the elements of each of these
- 3:48:51arrays so we can use a method for that
- 3:48:54let's begin with a method to accept an
- 3:48:56array of integers
- 3:48:58so let's add that here public static
- 3:49:02void we're not returning anything and
- 3:49:04let's say that this method is named
- 3:49:06display elements
- 3:49:11so there is one parameter if we're
- 3:49:13displaying an array of integers
- 3:49:15well that will be our parameter int
- 3:49:18array
- 3:49:19and let's call this array and then we'll
- 3:49:21need a for loop or for each loop to
- 3:49:24iterate over the elements of this array
- 3:49:26let's use a for each loop for each
- 3:49:29int item in array
- 3:49:33then we will write preferably not right
- 3:49:36line
- 3:49:38item then maybe i'll add a space okay
- 3:49:40let's call the display elements method
- 3:49:42that we just created
- 3:49:44and then pass in our interray as an
- 3:49:46argument
- 3:49:48and let's see what happens okay we have
- 3:49:51the numbers one
- 3:49:52two three so what if we attempt to pass
- 3:49:55in
- 3:49:56our double array display elements
- 3:49:59then we will pass in our array of
- 3:50:01doubles
- 3:50:02so we actually can't use this method for
- 3:50:05doubles we can only
- 3:50:06use this for integers cannot convert
- 3:50:08from double to
- 3:50:10int if i would like to display my array
- 3:50:12of doubles
- 3:50:13well i would need a completely different
- 3:50:15method that accepts
- 3:50:16an array of doubles as an argument so
- 3:50:19i'm going to
- 3:50:20copy this current method that we have
- 3:50:22change into
- 3:50:23double and change into double here as
- 3:50:27well
- 3:50:28and then we can call this version of our
- 3:50:30display elements method this will do the
- 3:50:34same thing
- 3:50:35actually let me make one change i'm just
- 3:50:37going to add a right line to the end of
- 3:50:39these
- 3:50:40so basically we have two methods that do
- 3:50:43the exact
- 3:50:43same thing however they accept a
- 3:50:45different data type as an argument
- 3:50:48let's add one more method for strings
- 3:50:53public static void display elements
- 3:50:56the parameter is an array of strings for
- 3:50:58each
- 3:50:59string item in array and then i can use
- 3:51:03the string version of display elements
- 3:51:06but pass
- 3:51:06in our string array
- 3:51:10so we have each of our three different
- 3:51:12types of arrays all displayed
- 3:51:15so this is a lot of work we have three
- 3:51:17methods that do basically the same thing
- 3:51:19however they accept
- 3:51:20different arguments so what if we had
- 3:51:22one method that could accept
- 3:51:24basically any data type well that's
- 3:51:26where generics come in
- 3:51:28so let's take these first two methods
- 3:51:30and delete them we will no longer need
- 3:51:32them
- 3:51:32we're going to turn this method into a
- 3:51:34generic method
- 3:51:35so after the method name add a set of
- 3:51:38angle brackets
- 3:51:39then add t so really you can put
- 3:51:41anything you want between these angle
- 3:51:43brackets i like to say thing because i
- 3:51:45think it's funny
- 3:51:46we're going to accept a thing as an
- 3:51:48argument
- 3:51:49so we need to change that here as well
- 3:51:51so change any instance of
- 3:51:53a data type to t or
- 3:51:56thing if you're using thing okay so
- 3:51:59change
- 3:51:59string to thing we're accepting an array
- 3:52:02of things
- 3:52:03for each thing item
- 3:52:06in array so now we have one method that
- 3:52:09accepts
- 3:52:10all data types we can reuse this one
- 3:52:12method for an array of integers
- 3:52:14doubles and strings
- 3:52:18and it appears to work so that's where
- 3:52:20generics are helpful
- 3:52:22anything that's generic is not specific
- 3:52:24to a particular data type
- 3:52:26we can add angle brackets t or really
- 3:52:29anything within these angle brackets
- 3:52:31just make sure that it's consistent
- 3:52:32so we can add this to classes methods
- 3:52:35fields etc
- 3:52:36and this allows for greater code
- 3:52:38reusability for different data
- 3:52:40types so instead of three different
- 3:52:41methods that all accept different data
- 3:52:43types we have one that accepts basically
- 3:52:46all data types
- 3:52:47so yeah those are generics if you found
- 3:52:49this video helpful be sure to smash that
- 3:52:51like button
- 3:52:51leave a random comment down below and
- 3:52:53subscribe if you'd like to become a
- 3:52:55fellow bro
- 3:52:56hey what's going on everybody it's bro
- 3:52:59hope you're doing well
- 3:53:00and in this video i'm going to explain a
- 3:53:01multi-threading in c-sharp
- 3:53:03so sit back relax and well enjoy the
- 3:53:06show
- 3:53:09if you find this video helpful please
- 3:53:11remember to like
- 3:53:12comment and subscribe your support will
- 3:53:14help keep this channel running
- 3:53:16all right all right all right threads a
- 3:53:19thread is an execution path of a program
- 3:53:22we can use multiple threads to perform
- 3:53:25different tasks of our program
- 3:53:27at the same time so when we begin a
- 3:53:29program we have
- 3:53:30one thread that is running and that is
- 3:53:32named the main thread
- 3:53:33twerk with threading include at the top
- 3:53:36using system
- 3:53:37dot threading so let's get the current
- 3:53:40main thread that is running and i'll
- 3:53:41assign this to a local variable of the
- 3:53:43thread data type
- 3:53:44let's say thread main thread
- 3:53:48equals thread dot current
- 3:53:51thread so this will assign the current
- 3:53:54thread that is running
- 3:53:55to a local variable named main thread or
- 3:53:58whatever you want to name it
- 3:54:00so i'm going to change the name property
- 3:54:02of main thread to equal
- 3:54:04let's say main thread
- 3:54:07and then i'll display this with a
- 3:54:08rightline statement main thread
- 3:54:11dot name and the name of this thread is
- 3:54:15main thread
- 3:54:16here's our job let's say that we need
- 3:54:18two timers to run
- 3:54:19one is counting up from zero to ten and
- 3:54:22the other is counting from ten to zero
- 3:54:24one is counting up the other is counting
- 3:54:25down but we need both of these timers to
- 3:54:28run
- 3:54:28concurrently so if we were to write
- 3:54:31these both on the same
- 3:54:32thread this is what this would look like
- 3:54:34and then later on we'll
- 3:54:35have these timers run on different
- 3:54:36threads so let's create a countdown and
- 3:54:39count
- 3:54:39up method public static
- 3:54:43void count
- 3:54:46down and then i'll need a for loop to
- 3:54:50iterate 10 times
- 3:54:514 and i equals 10
- 3:54:56then i will continue this as long as i
- 3:54:58is greater than or
- 3:55:00equal to zero and then
- 3:55:03decrement i by one during each iteration
- 3:55:06and during each iteration let's say that
- 3:55:08timer number one
- 3:55:10colon space plus i plus seconds
- 3:55:15now we can actually have our current
- 3:55:17thread that is running sleep for a given
- 3:55:19amount of milliseconds by typing thread
- 3:55:21dot sleep then pass in how many
- 3:55:24milliseconds you would like your thread
- 3:55:25to sleep
- 3:55:26so 1000 milliseconds for one second and
- 3:55:29then when we escape our for loop let's
- 3:55:31display that
- 3:55:33timer number one is complete
- 3:55:36and we'll also need a count up method so
- 3:55:39let's copy countdown
- 3:55:41paste it change down to up
- 3:55:44i is equal to zero we will continue this
- 3:55:47as long as
- 3:55:48i is less than or equal to ten
- 3:55:52increment i by one during each iteration
- 3:55:55timer number
- 3:55:56two timer number two is complete so
- 3:55:58let's invoke both
- 3:56:00countdown and count up
- 3:56:04so remember that these are both running
- 3:56:06on the same thread
- 3:56:07our main thread and when we complete our
- 3:56:10main thread
- 3:56:10let's display that main thread
- 3:56:14dot name plus is
- 3:56:17complete and i'm just going to hide this
- 3:56:20line here
- 3:56:22okay let's go so we have timer number
- 3:56:26one that's running currently
- 3:56:27timer number two has not started yet
- 3:56:29because these are both running on the
- 3:56:31same thread
- 3:56:36so timer number one is complete and now
- 3:56:38we're working on timer number two
- 3:56:46and now timer number two is complete our
- 3:56:48main thread is complete
- 3:56:50so if i would like both of these methods
- 3:56:52to execute at the same time i would need
- 3:56:54to create some additional threads
- 3:56:56and here's how to do so thread let's
- 3:57:00name this thread 1
- 3:57:01equals new thread and within the
- 3:57:04constructor of our thread let's pass in
- 3:57:06the method we would like to execute
- 3:57:09thread one is in charge of counting down
- 3:57:11and thread two is in charge of counting
- 3:57:14up
- 3:57:18so each of these threads has a built-in
- 3:57:20start method which we need to invoke to
- 3:57:22begin each of these threads
- 3:57:24thread1.start and thread2.start
- 3:57:30and we can get rid of these we now have
- 3:57:33one thread in charge of counting down
- 3:57:35and the other thread is in charge of
- 3:57:36counting up
- 3:57:37and our main thread is still running in
- 3:57:39the background
- 3:57:42so our main thread is complete and we
- 3:57:44have one timer that's counting down
- 3:57:46and the other that is counting up and as
- 3:57:48you can see these are both running at
- 3:57:50the same time concurrently
- 3:57:55so if you have a method that has
- 3:57:56parameters there's one additional step
- 3:57:58let's say that we have i don't know a
- 3:58:00string name or something
- 3:58:02we don't necessarily need to use these
- 3:58:04okay so we can't use these as they are
- 3:58:07currently
- 3:58:08so what we're going to do is within the
- 3:58:10constructor of our threads
- 3:58:11is pass in what is known as a lambda
- 3:58:14expression
- 3:58:15so parentheses arrow then the name of
- 3:58:18our method
- 3:58:19countdown parentheses then any
- 3:58:22arguments if there are any let's say i
- 3:58:24don't know timer
- 3:58:26number one i probably won't use this at
- 3:58:28all but this is just for an example
- 3:58:30okay let's do the same thing with thread
- 3:58:32two so we have a lambda expression
- 3:58:35parenthesis arrow and this is in charge
- 3:58:38of counting
- 3:58:39up and passing i don't know timer number
- 3:58:41two i guess
- 3:58:43i might have to make a separate video on
- 3:58:45lambda expressions
- 3:58:46okay let's try this again so this should
- 3:58:49work
- 3:58:51all right there we go so yeah those are
- 3:58:55threads
- 3:58:56a thread is an execution path of a
- 3:58:58program we can use
- 3:58:59multiple threads to perform different
- 3:59:01tasks of our program at the same time
- 3:59:04when we begin a program we have one
- 3:59:06thread that is running
- 3:59:07and that is referred to as the main
- 3:59:09thread so yeah that's multi-threading in
- 3:59:11c sharp if you would like a copy of this
- 3:59:13code i will post this to the comment
- 3:59:15section down below
- 3:59:16and well yeah that's multithreading in c
- 3:59:19sharp
- 3:59:20hey you yeah i'm talking to you if you
- 3:59:23learned something new
- 3:59:24then help me help you in three easy
- 3:59:26steps
- 3:59:27by smashing that like button drop a
- 3:59:29comment down below
- 3:59:30and subscribe if you'd like to become a
- 3:59:32fellow bro
- 3:59:40[Music]
- 3:59:59you
About this transcript
This page contains the full transcript of C# Full Course for free ๐ต by Bro Code, generated from the public captions YouTube serves with the video. The transcript has 37,282 words across 6,501 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator โ no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.