YouTube2Text

C# Full Course for free ๐ŸŽต โ€” Transcript

by Bro Code ยท 37,282 words ยท 6,501 segments ยท language en ยท Watch on YouTube

Full transcript

  1. 0:00hey what's going on everybody it's you
  2. 0:02bro hope you're doing well
  3. 0:03and in this video i'm going to show you
  4. 0:05all everything you need to know to get
  5. 0:06started writing code in c-sharp
  6. 0:08so sit back relax and enjoy the show
  7. 0:13if you wouldn't mind please like comment
  8. 0:15and subscribe
  9. 0:16one like equals one prayer for the
  10. 0:18youtube algorithm
  11. 0:20i'm gonna tell you why you need to learn
  12. 0:22c sharp c
  13. 0:23sharp is a very flexible language we can
  14. 0:26use c
  15. 0:27sharp for all sorts of things console
  16. 0:29apps web services
  17. 0:31games and if you're an aspiring game
  18. 0:32developer c-sharp is definitely a
  19. 0:34language you want to learn
  20. 0:36along with c plus but why not learn both
  21. 0:38and the average salary for a c-sharp
  22. 0:41developer if you have some experience
  23. 0:43is 63 000 per year according to
  24. 0:45glassdoor
  25. 0:46if that all sounds good let's begin to
  26. 0:48code with c-sharp we'll need an ide
  27. 0:51that's an integrated development
  28. 0:52environment think of it
  29. 0:53as software that helps us write other
  30. 0:55software one that i would recommend is
  31. 0:58visual studio community so you can just
  32. 1:00google that then head to this url at the
  33. 1:02top here
  34. 1:03okay then we are going to download
  35. 1:05visual studio and just follow the
  36. 1:07installation procedures
  37. 1:08so let's open this and run it so
  38. 1:11continue and here we have a bunch of
  39. 1:13different packages that we can download
  40. 1:14you'll want to check
  41. 1:15dot net desktop development for using c
  42. 1:18sharp
  43. 1:19and if you're interested in using unity
  44. 1:20you might as well just download this
  45. 1:22package too
  46. 1:23then we are going to install you have
  47. 1:25the option to sign in but i'm just going
  48. 1:27to click
  49. 1:27not now maybe later you can also pick a
  50. 1:30color theme for your ide i'm going to go
  51. 1:32with dark because i like to feel like a
  52. 1:33pretend elite hacker
  53. 1:35then start visual studio now with this
  54. 1:37home screen we'll continue without code
  55. 1:39i'll show you all how we can create a
  56. 1:40new project
  57. 1:41all right now to create a new project
  58. 1:43we'll go to file
  59. 1:45new project select c sharp
  60. 1:48console application next we'll need a
  61. 1:51name for this project
  62. 1:52i'll call this my first
  63. 1:55program next then create
  64. 1:58all right now this font size is very
  65. 2:01small you can either zoom in here or
  66. 2:02down at the bottom or you can change
  67. 2:04the font as well as the font size by
  68. 2:06going to tools
  69. 2:08options environment fonts and colors
  70. 2:12and you can change the size here what
  71. 2:13about 18
  72. 2:16and we should be good and here we have a
  73. 2:18small program that was created for us
  74. 2:20automatically
  75. 2:20all this does is display some output the
  76. 2:23words hello world
  77. 2:24to compile and run a program you just
  78. 2:26have to click this green play button at
  79. 2:28the top
  80. 2:30and this will create a console window
  81. 2:32that displays our output of
  82. 2:34hello world whatever we have written
  83. 2:36here within quotes
  84. 2:38and this line of code is known as the
  85. 2:40main method it's the entry point for
  86. 2:42where our program begins
  87. 2:44and this set of curly braces after the
  88. 2:46main method is the
  89. 2:47body of the main method so our program
  90. 2:50will begin
  91. 2:50by executing any code at the top of the
  92. 2:53main method
  93. 2:53and it will work its way down and
  94. 2:55execute any subsequent code that follows
  95. 2:58so this main method is made up of a
  96. 2:59bunch of words that we don't quite
  97. 3:01understand yet
  98. 3:02static void main string args in time
  99. 3:05we'll learn what each of these words
  100. 3:06mean but that's more of an advanced
  101. 3:08topic
  102. 3:09so a textbook that i read once said to
  103. 3:11think of the main method as like a
  104. 3:13magical spell or incantation that we
  105. 3:15have to say
  106. 3:16in order for our program to run so let's
  107. 3:18see what happens if i remove the main
  108. 3:20method
  109. 3:20i'll still keep this line though
  110. 3:22console.writeline hello world
  111. 3:24and let's try it okay so it looks like
  112. 3:27there were build errors so we cannot run
  113. 3:29the program as it is now
  114. 3:30so we do need that main method and if
  115. 3:32you are missing it you can just type it
  116. 3:34in
  117. 3:34and it should work now now this is a
  118. 3:37standard writeline method whatever text
  119. 3:40that we put within quotes will appear
  120. 3:42within
  121. 3:42our console window to display as output
  122. 3:45so let's actually change it right now it
  123. 3:47says hello world but let's type
  124. 3:48something else
  125. 3:49so make sure that this is within double
  126. 3:51quotes let's change this to
  127. 3:53i like pizza
  128. 3:56and then run this again
  129. 4:00so now my output should say i like pizza
  130. 4:03and then you can add a second line of
  131. 4:04output just by writing
  132. 4:06console.writeline then within
  133. 4:08parentheses and within double quotes you
  134. 4:10can write something else
  135. 4:11like it's really
  136. 4:14good and now we have two lines of output
  137. 4:17i like pizza
  138. 4:18it's really good so to write output to
  139. 4:21the console window you type
  140. 4:23console.writeline then within quotes
  141. 4:25some text that you want to write one
  142. 4:27thing that you may want to change
  143. 4:29is the font and the font size of your
  144. 4:31console window because right now it's
  145. 4:33very difficult for me to read
  146. 4:35so to change that right click in the top
  147. 4:37left corner go to
  148. 4:38properties then go to font let's
  149. 4:41increase this to maybe 28
  150. 4:43you can pick a new font style as well
  151. 4:45i'll keep it the same
  152. 4:46then you can also change the color
  153. 4:48scheme too i'll change the screen text
  154. 4:50to maybe green then okay all right but
  155. 4:54now this window is huge so let's change
  156. 4:56that
  157. 4:56so again go to the top left corner go to
  158. 5:00properties
  159. 5:01then go to layout let's change the width
  160. 5:04to
  161. 5:04what about i don't know 50 and the
  162. 5:06height to
  163. 5:08let's say 15 i think that should be good
  164. 5:12okay yeah that's not too bad and here
  165. 5:14it's actually a lot easier to read
  166. 5:16i like pizza it's really good oh one
  167. 5:19last stupid thing that i wanted to show
  168. 5:20you all is how we can make our console
  169. 5:22beep type console dot beep
  170. 5:25parentheses semicolon so then our
  171. 5:28program is going to beep when it reaches
  172. 5:30the end
  173. 5:32it's not really important at all i just
  174. 5:34thought it would be something fun to
  175. 5:35throw in at the end of this video so
  176. 5:37that's how to make your console beep
  177. 5:38for some reason all right well that's
  178. 5:41the very basics of c sharp to get you
  179. 5:43started in the next video we'll cover
  180. 5:45more on output and comments so i'll post
  181. 5:49this code to the comment section down
  182. 5:50below
  183. 5:51and well that's the very basics to get
  184. 5:53you started in
  185. 5:54c-sharp hey you yeah i'm talking to you
  186. 5:58if you learned something new then help
  187. 6:00me help you in three easy steps
  188. 6:03by smashing that like button drop a
  189. 6:05comment down below
  190. 6:06and subscribe if you'd like to become a
  191. 6:08fellow bro
  192. 6:21[Music]
  193. 6:30hey what's going on everybody it's you
  194. 6:32bro hope you're doing well
  195. 6:34and in this video i'm going to show you
  196. 6:35all a few different ways in which we can
  197. 6:37format output in c sharp
  198. 6:38so sit back relax and enjoy the show
  199. 6:43if you find this video helpful please
  200. 6:45remember to like
  201. 6:46comment and subscribe your support will
  202. 6:49help keep this channel running
  203. 6:51all right everybody here's a few
  204. 6:52different ways in which we can format
  205. 6:54output in c sharp
  206. 6:55as we covered in the last video to
  207. 6:56display something to our console window
  208. 6:58we would type
  209. 6:59console.writeline then you need a set of
  210. 7:02parenthesis then a semicolon at the end
  211. 7:04you place your message within the
  212. 7:06parenthesis this is known as a method
  213. 7:08and we have a semicolon at the end the
  214. 7:10semicolon is kind of like
  215. 7:12the period at the end of a sentence when
  216. 7:14we finish a statement in programming we
  217. 7:16end it with a semicolon
  218. 7:18so to display a message within double
  219. 7:20quotes you would type something
  220. 7:22so this is known as a string literal
  221. 7:24we're literally printing something to
  222. 7:26our console
  223. 7:27so we kind of covered this in the last
  224. 7:28video this will display the word
  225. 7:30hello now there's another way in which
  226. 7:32we can display output that is just
  227. 7:34right console dot right parentheses
  228. 7:37semicolon
  229. 7:38and within quotes let's display
  230. 7:40something else what about
  231. 7:42hey now let's take a look at the
  232. 7:44difference between the two
  233. 7:46all right we have hey immediately
  234. 7:48followed by hello
  235. 7:49all in the same line in the last video
  236. 7:51we used two right line statements
  237. 7:53each string literal had their very own
  238. 7:56line but in this case
  239. 7:57it's all one line so with writeline
  240. 8:00after you finish your string literal
  241. 8:02it's going to hit enter like we're
  242. 8:03hitting the enter key and we'll move
  243. 8:05down to a new line
  244. 8:06right doesn't do that so you have two
  245. 8:08different ways in which you can display
  246. 8:09output to the console window
  247. 8:11right and right line write does not hit
  248. 8:13the enter key after the end
  249. 8:15it doesn't create a new line and
  250. 8:16rightline does so you can use either one
  251. 8:18depending on what you need
  252. 8:19now there is a shortcut to create a
  253. 8:22rightline statement you type
  254. 8:23c then hit tab twice and that will auto
  255. 8:26generate
  256. 8:26a right light statement for you okay so
  257. 8:29the next topic to cover is comments we
  258. 8:31can add a comment to our code
  259. 8:33and this will not be displayed as output
  260. 8:35so to write a comment within your code
  261. 8:37you need
  262. 8:38two forward slashes and you can see that
  263. 8:40the color
  264. 8:41of our text changed to green so i'm
  265. 8:44going to tell you
  266. 8:44this is a comment and this has no effect
  267. 8:48on our output
  268. 8:49this comment will not be seen all we see
  269. 8:52here is
  270. 8:52hey and hello you can also write a
  271. 8:55multi-line comment
  272. 8:56you would type forward slash asterisk
  273. 8:59this is a multi-line
  274. 9:04comment then to end a multi-line comment
  275. 9:08you would end it with asterisk then
  276. 9:10forward slash again
  277. 9:11and last but not least we have escape
  278. 9:13sequences we can add an escape sequence
  279. 9:15to a string to format our output
  280. 9:17so let's create a writeline statement
  281. 9:19and type whatever your name is
  282. 9:22okay now to create an escape sequence we
  283. 9:25need to
  284. 9:25add a backslash followed by a certain
  285. 9:28character
  286. 9:29depending on what character you select
  287. 9:31it's going to have a special effect on
  288. 9:33your string
  289. 9:34so if i typed backslash t that will add
  290. 9:37a tab escape sequence before displaying
  291. 9:39my name
  292. 9:43all right so we have a tab before my
  293. 9:45string so let's change this to maybe
  294. 9:48a backspace i'll add that here so
  295. 9:51backslash b that will add a backspace
  296. 9:55and my name is now br
  297. 9:59code alright so we can also do newline
  298. 10:04and for new line so my first name is on
  299. 10:08one line
  300. 10:09and my last name is on a second line now
  301. 10:12you may have noticed that there's all
  302. 10:13this garbage at the end of my console
  303. 10:15window when my program is done running
  304. 10:17so one way in which we can hide that at
  305. 10:19the end of our program type
  306. 10:21console.read key so basically this will
  307. 10:24prevent
  308. 10:25our program from ending until we type a
  309. 10:27key
  310. 10:29and it should be hidden now so if i hit
  311. 10:31the enter key well then my program is
  312. 10:33going to finish running
  313. 10:35all right everybody so those are a few
  314. 10:37ways in which we can format output in c
  315. 10:39sharp if you can give this video a
  316. 10:41thumbs up drop a random comment down
  317. 10:43below
  318. 10:43and well those are a few ways to format
  319. 10:46output in c
  320. 10:47sharp hey what's going on everybody
  321. 10:50it's bro hope you're doing well and in
  322. 10:52this video i'm going to explain
  323. 10:53variables in c sharp so sit back relax
  324. 10:56and enjoy the show
  325. 11:00do you all remember back in middle
  326. 11:01school when we learned about algebra we
  327. 11:03had to solve an equation to find the
  328. 11:05value of x
  329. 11:06and x as some representation of a number
  330. 11:09well with programming we can create our
  331. 11:10own variables but we are not limited to
  332. 11:12just
  333. 11:13numbers we can store characters words
  334. 11:16and even these things called booleans
  335. 11:17which are either true or
  336. 11:19false so i'm going to show you all how
  337. 11:21we can create our own variables in c
  338. 11:23sharp creating a variable takes two
  339. 11:25steps declaration and initialization
  340. 11:28to create a variable we first have to
  341. 11:30declare what type of data we'll be
  342. 11:31storing
  343. 11:32if we need to store a number we'll type
  344. 11:34int short for integer
  345. 11:36and we'll need a unique name for this
  346. 11:38specific variable
  347. 11:40so let's just call this x and then end
  348. 11:42the statement with a semicolon because
  349. 11:44with programming semicolons are kind of
  350. 11:46like
  351. 11:46periods when you finish a sentence okay
  352. 11:48so this step
  353. 11:50was declaration and i'll just add a note
  354. 11:52here
  355. 11:53so this step is declaration
  356. 11:57and now to assign a value we will
  357. 11:59initialize this variable
  358. 12:00so to assign this variable x a value
  359. 12:03type
  360. 12:04x equals and then some value
  361. 12:07let's say one two three then semicolon
  362. 12:11okay so we now have a variable named x
  363. 12:14and this step is initialization
  364. 12:18now you can combine these two steps so
  365. 12:20to do that we would say
  366. 12:22and let's say y this time equals
  367. 12:25three two one semicolon so we have two
  368. 12:28variables
  369. 12:29x and y so this step right here is both
  370. 12:32declaration and initialization now these
  371. 12:35variables
  372. 12:36behave as the value that they contain so
  373. 12:38let's display some of these
  374. 12:40so within a console.writeline statement
  375. 12:43i'm going to display x and then with a
  376. 12:46second rightline statement i will
  377. 12:48display
  378. 12:49y so console.rightline
  379. 12:52y and we have one two three
  380. 12:55three two one now you can use these for
  381. 12:58like math two we could say
  382. 13:00like int z equals x
  383. 13:03plus y and then display whatever
  384. 13:06z is console.writeline
  385. 13:10z and the result is
  386. 13:14444. so these names for these variables
  387. 13:18really aren't descriptive
  388. 13:19x y and z we can give some more unique
  389. 13:21names that describe what kind of value
  390. 13:24that
  391. 13:24these variables contain so let's say int
  392. 13:27edge and i'll assign this variable age a
  393. 13:30value
  394. 13:30of 21 and then i can display my edge
  395. 13:33within a writeline statement or use it
  396. 13:35for something
  397. 13:36so let's say console.writeline
  398. 13:39and then i can display a message along
  399. 13:42with my age
  400. 13:43so let's say your age
  401. 13:47is so this part is within quotes
  402. 13:50to display the value of my age i will
  403. 13:53add
  404. 13:54plus edge make sure age is not within
  405. 13:57quotes
  406. 13:58because then we're printing a string
  407. 14:00literal and not the
  408. 14:01value contained within edge so this
  409. 14:03should display
  410. 14:06your age is 21. so there's more data
  411. 14:10types than just int
  412. 14:11this time let's use a double a double
  413. 14:14is really just a decimal number it's
  414. 14:16like a floating point number but with
  415. 14:18more precision
  416. 14:19there are floats in c sharp but i don't
  417. 14:21really think they're that important for
  418. 14:22beginners
  419. 14:23so with a double we can store a decimal
  420. 14:26number
  421. 14:26and int only stores a hole integer
  422. 14:30so if i try to display like 21.5
  423. 14:34and then display it this is what happens
  424. 14:39so we'll run into a build error so with
  425. 14:41int we can only store
  426. 14:42whole integers but with a double we can
  427. 14:44store something that includes a decimal
  428. 14:47so let's say we have a double height and
  429. 14:50i'll assign this
  430. 14:51a perfectly average height of 300.5
  431. 14:55centimeters i guess and now since this
  432. 14:57variable is initialized we can use it
  433. 14:59for something
  434. 15:00we can change the value or we can use it
  435. 15:03within a message
  436. 15:04so let's use console.writeline and i'll
  437. 15:07display
  438. 15:08my height within some sort of message
  439. 15:11let's say your
  440. 15:12height is space
  441. 15:15plus height plus another string let's
  442. 15:19say
  443. 15:21cm 4 centimeters
  444. 15:24then let's try it okay your age is 21
  445. 15:27your height is 300.5 centimeters
  446. 15:31so a double variable can store a
  447. 15:34decimal number unlike with integers
  448. 15:37they only store whole numbers now the
  449. 15:40next data type is
  450. 15:41boolean and to declare a boolean
  451. 15:43variable we type
  452. 15:44bool then a variable name so booleans
  453. 15:48can only store
  454. 15:49true or false so this might be good if
  455. 15:51you need
  456. 15:52something that only has like two options
  457. 15:54like a light switch it's on
  458. 15:56or it's off if somebody's online that
  459. 15:58could be true if they're offline
  460. 16:00that's false or maybe somebody is alive
  461. 16:03let's say
  462. 16:03bull alive either true or
  463. 16:06false but the last time i checked i am
  464. 16:08alive so i'm going to say that is true
  465. 16:11and then we can use this boolean
  466. 16:12variable for something
  467. 16:14so let's use this within a
  468. 16:16console.writeline statement
  469. 16:18console.writeline and what should we say
  470. 16:22are you alive
  471. 16:28plus alive
  472. 16:31are you alive that is true so if i
  473. 16:34change this to false
  474. 16:36this would display are you alive
  475. 16:40that is false so that is a boolean
  476. 16:44variable it's only true or false
  477. 16:47so for practice i'll encourage you to
  478. 16:49try and type something besides true or
  479. 16:51false like the word pizza
  480. 16:53we can't shove a whole pizza into a
  481. 16:55boolean variable it can only be true or
  482. 16:57false so let's change that back okay the
  483. 17:00next data type
  484. 17:01is chart it's a character a single
  485. 17:04character
  486. 17:05what about symbol char symbol
  487. 17:08and we need to place this within single
  488. 17:11quotes
  489. 17:11so let's say our character is the at
  490. 17:14sign
  491. 17:15and then let's display it
  492. 17:19console.writeline
  493. 17:23your symbol is
  494. 17:27plus symbol
  495. 17:33your symbol is at sign so make sure that
  496. 17:36this is within single quotes when you
  497. 17:38assign a value
  498. 17:39so with chars we can only assign a
  499. 17:42single character if i
  500. 17:43attempted to add like a different symbol
  501. 17:45too well this wouldn't work then
  502. 17:48see we're getting an error here so
  503. 17:50that's where strings come in with a
  504. 17:52string we can store
  505. 17:53a series of characters and to declare
  506. 17:55this we type
  507. 17:56string with a capital s string let's say
  508. 17:59name
  509. 18:00string name and this value is within
  510. 18:03double quotes
  511. 18:04so type whatever your first name is and
  512. 18:06then we can use our name for something
  513. 18:09let's display a message that says hello
  514. 18:11whatever your name is
  515. 18:13console.writeline
  516. 18:16hello plus name
  517. 18:21okay hello bro your age is 21 your
  518. 18:24height is 300.5 centimeters
  519. 18:27are you alive false your symbol is at
  520. 18:30sign
  521. 18:31let's say that we have a username and
  522. 18:33i'll say
  523. 18:35string username
  524. 18:39equals symbol
  525. 18:42plus name and then i'll display this
  526. 18:48console.writeline
  527. 18:52your user name
  528. 18:56is colon space
  529. 18:59quotes plus user
  530. 19:02name whatever you typed here
  531. 19:07okay your username is at bro
  532. 19:11those are variables they're containers
  533. 19:13for values
  534. 19:15but you can store more than just numbers
  535. 19:16you can store boolean values
  536. 19:19characters and strings and a string is a
  537. 19:22series of characters
  538. 19:23so if you would like a copy of this code
  539. 19:25i will post this to the comment section
  540. 19:27down below
  541. 19:28and well those are variables in c sharp
  542. 19:33okay it looks like we're on constants
  543. 19:35constants are
  544. 19:36immutable values which are known at
  545. 19:39compile time
  546. 19:40and do not change for the life of a
  547. 19:42program let's say that we have a
  548. 19:44double variable named pi and this equals
  549. 19:47the first few digits of pi
  550. 19:503.14159 if this is not a constant
  551. 19:53people can actually change the value
  552. 19:55contained within this variable and that
  553. 19:57could be a security concern let's say pi
  554. 19:59now equals 420 and then i will display
  555. 20:02the value contained within pi
  556. 20:04so if you do not want people to be able
  557. 20:06to change this value
  558. 20:08we can precede the data type of this
  559. 20:10variable with the
  560. 20:12const keyword and this variable is now a
  561. 20:15constant and we can't update this
  562. 20:16variable
  563. 20:17now that it's a constant so yeah that's
  564. 20:19a constant it's an immutable value which
  565. 20:21is known at compile time
  566. 20:23and does not change for the life of the
  567. 20:25program and it adds a little bit of
  568. 20:27security to our programs because people
  569. 20:29cannot later change these values
  570. 20:31so yeah those are constants in c sharp
  571. 20:35oh yeah typecasting typecasting
  572. 20:39is the ability to convert a value to a
  573. 20:41different data type
  574. 20:42it's useful when we accept user input
  575. 20:45because when the user types in something
  576. 20:47it's going to be a string and strings
  577. 20:49can only do so many things
  578. 20:50and different data types can perform
  579. 20:53different things
  580. 20:54so it may be necessary to convert from
  581. 20:56one data type to another
  582. 20:57like for example we can't use strings
  583. 20:59for any sort of math we would need to
  584. 21:00convert a string
  585. 21:02to an integer or double to be able to
  586. 21:04use a string representation of a number
  587. 21:07so let's begin let's say we would like
  588. 21:09to convert a double to an integer
  589. 21:11let's create a double variable named a
  590. 21:14and i'll assign this a value of 3.1 for
  591. 21:17the first few digits of pi
  592. 21:19so what we could do if we wanted to
  593. 21:20convert this number to an
  594. 21:22end we could store this within a
  595. 21:23separate variable let's say
  596. 21:25int b equals now to convert this value
  597. 21:29to an integer we can use the convert
  598. 21:32classes
  599. 21:332 and 32 method so convert
  600. 21:36dot 2 into 32 parentheses semicolon
  601. 21:39and within the parentheses i'm going to
  602. 21:41place what i want to convert
  603. 21:43i'm going to convert the value stored
  604. 21:45with an a and then assign it to
  605. 21:47variable b and then let's display this
  606. 21:49variable with a rightline statement
  607. 21:51so console.writeline and then i will
  608. 21:54display whatever b
  609. 21:55is then let's try it
  610. 21:58and the value stored within a b is three
  611. 22:01so if we convert a double to an integer
  612. 22:04it's going to truncate
  613. 22:05any decimal portion if there is any so
  614. 22:07since we wrote this program this way
  615. 22:09it's not going to get rid of the value
  616. 22:11within a
  617. 22:12so if i were to display a it's going to
  618. 22:14be 3.14
  619. 22:16all we did is that we took the value
  620. 22:18stored with an a
  621. 22:19and created a copy of it then converted
  622. 22:22it to an integer and stored it within b
  623. 22:24it's also possible to display the data
  624. 22:27type of whatever variable that we're
  625. 22:28working with
  626. 22:29so after a within our rightline method
  627. 22:32type
  628. 22:33dot get type then add a set of
  629. 22:36parenthesis this is the
  630. 22:38get type method so this will display
  631. 22:41instead of the value stored within a
  632. 22:43it's going to display the data type of
  633. 22:45whatever a is
  634. 22:47so the data type of a is currently
  635. 22:50a double now let's change this to b
  636. 22:53b dot get type and our variable b
  637. 22:56is an integer into 32
  638. 23:01so if you need the data type of a
  639. 23:03variable follow
  640. 23:04that variable with dot get type then a
  641. 23:07set of parentheses
  642. 23:08this is a built-in method of variables
  643. 23:11let's convert an into double this time
  644. 23:14let's create
  645. 23:14int c equals what about 1 2 3
  646. 23:18and we'll create double d equals
  647. 23:22convert dot to double
  648. 23:26then parenthesis semicolon within the
  649. 23:28parentheses
  650. 23:29we will convert c so place that within
  651. 23:31there
  652. 23:32and within a writeline statement we will
  653. 23:34display
  654. 23:35whatever d is console.writeline
  655. 23:38d so if your
  656. 23:42double doesn't contain like a decimal
  657. 23:44portion it's not going to display
  658. 23:46like point zero but if i were to add
  659. 23:48like
  660. 23:490.1 to the end of this then it will
  661. 23:51display a decimal portion
  662. 23:55so 123.1 and the data type
  663. 23:58of d let's follow this with the get
  664. 24:02type method so the data type of d
  665. 24:05is in fact a double okay now let's
  666. 24:08convert an integer to a string
  667. 24:10we'll need int what about e e equals
  668. 24:13three two one and i'll create string
  669. 24:17f equals convert dot
  670. 24:20to string are you beginning to see a
  671. 24:23pattern here
  672. 24:24then parenthesis semicolon then pass in
  673. 24:27e
  674. 24:27so place that within the parenthesis and
  675. 24:30we will display whatever
  676. 24:31f is console.writeline
  677. 24:35the value stored within f and that will
  678. 24:38be
  679. 24:41three two one so a string we can't use
  680. 24:44for any arithmetic but with ins and
  681. 24:46doubles we can
  682. 24:47so if we convert an integer to a string
  683. 24:49we can no longer use this for any math
  684. 24:52but if we converted a string to an
  685. 24:53integer then we can use it for any sort
  686. 24:56of math
  687. 24:56so let's follow this with get type
  688. 24:59method
  689. 25:00f dot get type and the data type
  690. 25:03of f is in fact a string okay
  691. 25:06exercise number three let's convert a
  692. 25:09string
  693. 25:09to hr hr is just a single character
  694. 25:14string g equals so a string is always
  695. 25:18within double quotes what about the
  696. 25:19dollar sign
  697. 25:21and a char is always within single
  698. 25:23quotes we treat strings
  699. 25:25and chars differently you can store a
  700. 25:27single character within a string
  701. 25:29however strings are different from chars
  702. 25:31there's different things that we can do
  703. 25:33so let's say char what comes after g e f
  704. 25:36g
  705. 25:36h char h equals
  706. 25:40convert dot to
  707. 25:43char then of course parenthesis
  708. 25:47semicolon within the parentheses we will
  709. 25:49place g
  710. 25:52and then display whatever h is so
  711. 25:57console.writeline h
  712. 26:00so h will still be a dollar sign
  713. 26:04but we treat it different because it's a
  714. 26:05character there's different things that
  715. 26:07we can do
  716. 26:07then let's get the type of whatever h is
  717. 26:12and h is hr
  718. 26:15a single character okay last but not
  719. 26:18least let's convert
  720. 26:19a string to a boolean string
  721. 26:23i equals true within double quotes
  722. 26:27so this is different from true without
  723. 26:30double quotes
  724. 26:31because this is a boolean and right now
  725. 26:33we're getting an error
  726. 26:34so represents a boolean true or false a
  727. 26:37string
  728. 26:38is always within double quotes so we
  729. 26:40would treat this string
  730. 26:41different from this boolean even though
  731. 26:44they say the same thing
  732. 26:45okay then let's convert this string to a
  733. 26:48boolean
  734. 26:49type bool not cool bull
  735. 26:52j equals convert
  736. 26:56dot to boolean
  737. 26:59parentheses semicolon pass in i
  738. 27:03then we will display whatever j is
  739. 27:06console.writeline j and this will
  740. 27:09display
  741. 27:12true then let's get the type of this
  742. 27:14variable
  743. 27:15so j dot get type method
  744. 27:19and the data type of j is a boolean now
  745. 27:23all right everybody so that's how to
  746. 27:25type cast we can convert
  747. 27:27a value to a different data type and
  748. 27:29it's really useful when we accept user
  749. 27:31input because
  750. 27:32it's always going to be a string and
  751. 27:34there's different things that we can do
  752. 27:36with different data types so it may be
  753. 27:38necessary to convert from one data type
  754. 27:40to another
  755. 27:41so if you would like a copy of all this
  756. 27:43code i will post this to the comment
  757. 27:44section down below
  758. 27:45and well that's typecasting in c sharp
  759. 27:49hey what's going on everybody it's you
  760. 27:52bro hope you're doing well and in this
  761. 27:53video i'm going to show you all how we
  762. 27:55can accept some user input in c
  763. 27:57sharp so sit back relax and enjoy the
  764. 28:00show
  765. 28:02all right here's how we can accept some
  766. 28:04user input now when we do accept user
  767. 28:06input
  768. 28:07it's of the string data type so let's
  769. 28:09create a variable
  770. 28:10to hold and store our input let's create
  771. 28:13a variable
  772. 28:14named name and we will ask the user for
  773. 28:17what their name is
  774. 28:18now to read some user input type
  775. 28:22console.readline
  776. 28:24then parentheses semicolon so when the
  777. 28:26user
  778. 28:27types in something then hits enter we
  779. 28:29will take
  780. 28:30that user input and store it within this
  781. 28:32variable name
  782. 28:33which we can then use for something so
  783. 28:35within a writeline statement
  784. 28:37let's use the user's name for something
  785. 28:39hello
  786. 28:40plus name and let's take a look
  787. 28:45okay now to enter in some user input
  788. 28:47we'll have to type this into the console
  789. 28:49directly
  790. 28:50however it is good practice to let the
  791. 28:52user know what we want them to type in
  792. 28:54exactly because
  793. 28:55how's the views are supposed to know
  794. 28:57that they're supposed to type in their
  795. 28:58name right here
  796. 28:59so what we'll do is preceding our
  797. 29:01readline statement let's create a prompt
  798. 29:03to tell the user what we want them to
  799. 29:04type in
  800. 29:05so within a rightline statement let's
  801. 29:07say what's
  802. 29:08your name and then try this again
  803. 29:14okay what's your name now the user knows
  804. 29:16that they have to type in their name so
  805. 29:18type in your first name hit enter hello
  806. 29:21whatever your first name is
  807. 29:22okay this time let's accept a user's age
  808. 29:26and then we'll typecast the user's age
  809. 29:28into an integer variable
  810. 29:30so let's copy what we have paste it
  811. 29:34console.writeline what's your
  812. 29:37edge and we'll create a new variable of
  813. 29:40the int data type
  814. 29:41named age now we'll need to cast this
  815. 29:44user input into
  816. 29:45an integer so to do that we would type
  817. 29:48convert dot 2 and 32
  818. 29:52parenthesis semicolon now take this
  819. 29:55statement without the semicolon
  820. 29:58cut it and then paste it within the
  821. 30:00parentheses so what we're doing
  822. 30:02is accepting some user input then
  823. 30:04converting it
  824. 30:05to an integer that we're storing within
  825. 30:07this variable edge
  826. 30:08and then we will display the user's age
  827. 30:12console.writeline
  828. 30:13u r plus age plus
  829. 30:18years old then let's run this again
  830. 30:23so our program is actually going to wait
  831. 30:25at each prompt until the user types
  832. 30:27something in
  833. 30:28so what's your name bro let's say that
  834. 30:30i'm 21 i'll hit enter
  835. 30:32hello bro you are 21 years old
  836. 30:35now one issue that you may run into is
  837. 30:37if the user types in something that's
  838. 30:39non-numeric and we try and convert it
  839. 30:42so let's try this again what's your name
  840. 30:44bro
  841. 30:45and instead of an age what if i type in
  842. 30:47a word like pizza
  843. 30:48this will cause an exception an
  844. 30:50exception will interrupt our program
  845. 30:52and this specific exception is a format
  846. 30:56exception input string was not in a
  847. 30:58correct format
  848. 30:59so there will be a future video on
  849. 31:02handling exceptions which will prevent
  850. 31:03the program from pausing and being
  851. 31:05interrupted
  852. 31:06but that's a future video in the
  853. 31:08meantime we'll just have to be sure that
  854. 31:10we're entering in the correct user input
  855. 31:12well okay then everybody that's how to
  856. 31:14accept some user input in c
  857. 31:16sharp i will post this code to the
  858. 31:18comment section down below
  859. 31:19and well yeah that's how to accept user
  860. 31:22input in c
  861. 31:23sharp hey everyone we're going to cover
  862. 31:26some basic arithmetic
  863. 31:27in c sharp so sit back relax and enjoy
  864. 31:30the show
  865. 31:33everyone i thought we could just cover
  866. 31:34some basic arithmetic real quick
  867. 31:36let's say we have a variable friends and
  868. 31:38i will assign this a value of what about
  869. 31:40five
  870. 31:41and within a writeline statement i will
  871. 31:43display the value stored within friends
  872. 31:45so of course
  873. 31:46friends will have a value of five but
  874. 31:49what if i need to
  875. 31:50update this like increment it i could
  876. 31:52say friends
  877. 31:53equals friends and we make a friend so
  878. 31:56we could say friends equals friends plus
  879. 31:58one
  880. 31:58because remember the value stored within
  881. 32:00friends is five
  882. 32:02this is no different from saying friends
  883. 32:03equals five plus one which would be six
  884. 32:06so let me display the value stored
  885. 32:08within friends and of course
  886. 32:09that's six of course you can change this
  887. 32:11to a different number too you could say
  888. 32:13friends equals friends plus two then the
  889. 32:16amount of friends that we have
  890. 32:17is seven so there is a shortcut of
  891. 32:20writing this too because this can be
  892. 32:22somewhat tedious
  893. 32:23what you could say is friends plus
  894. 32:25equals
  895. 32:26then the amount that you want to
  896. 32:27increment this variable by
  897. 32:29so if i need to increment friends by two
  898. 32:32i could say friends
  899. 32:33plus equals two and that would do the
  900. 32:35same thing that's like a shortcut
  901. 32:37so of course that would be seven so
  902. 32:39there's a third way to increment and
  903. 32:42decrement two
  904. 32:42this method is found within loops you
  905. 32:44could say friends
  906. 32:46plus plus however this will only
  907. 32:48increment a
  908. 32:49variable by one so if friends is
  909. 32:51initially five
  910. 32:52then we say plus plus well then friends
  911. 32:54will equal six
  912. 32:55so there's three different ways to
  913. 32:57increment a variable
  914. 32:58this way is found within loops this is
  915. 33:00the standard way and that's kind of the
  916. 33:02shortcut this line right here
  917. 33:04okay so let's decrement friends so we
  918. 33:06could say friends
  919. 33:08equals friends minus one or we could say
  920. 33:12friends minus equals one or
  921. 33:15friends minus minus so if we combine all
  922. 33:19these the amount of friends that we have
  923. 33:20is two let's work on some multiplication
  924. 33:24if we need to double the amount of
  925. 33:26friends that we have we would say
  926. 33:27friends equals friends times
  927. 33:31we use an asterisk for multiplication
  928. 33:33and programming
  929. 33:34times two then the amount of friends
  930. 33:36that we have
  931. 33:38is 10 and the shortcut would be
  932. 33:43friends asterisk equals 2
  933. 33:46and that would do the same thing now
  934. 33:49division
  935. 33:50let's say that steve breaks up our group
  936. 33:52of friends
  937. 33:53so we could say friends equals friends
  938. 33:55divided by two and for division we use a
  939. 33:58forward slash
  940. 33:59now pay attention to this our current
  941. 34:01number of friends is five but what if we
  942. 34:03divide this by two
  943. 34:05well you would think that we get 2.5
  944. 34:07right well you're wrong we end up with
  945. 34:092.
  946. 34:09that's because friends is an integer we
  947. 34:12can only store whole numbers
  948. 34:13we can't store that decimal portion that
  949. 34:150.5
  950. 34:16so if this was a double well then that
  951. 34:19would be 2.5
  952. 34:21so that's referred to as integer
  953. 34:23division and that's something you have
  954. 34:24to look out for
  955. 34:25so if friends equals friends divided by
  956. 34:282
  957. 34:28well then the shortcut would be friends
  958. 34:31forward slash
  959. 34:32equals 2 and that would do the same
  960. 34:33thing and then remember i'm storing this
  961. 34:36within an integer so we will truncate
  962. 34:38that decimal portion and we end up with
  963. 34:402.
  964. 34:41and our last operator for this topic is
  965. 34:43the modulus
  966. 34:45also known as the remainder operator now
  967. 34:48for
  968. 34:48this example i'm going to change the
  969. 34:49amount of friends that we have to 10
  970. 34:51so we can find the remainder of any
  971. 34:53division but i'm going to store this
  972. 34:55within a new
  973. 34:56variable int remainder
  974. 34:59and to find the remainder we could say
  975. 35:01friends
  976. 35:02then modulus which is represented by a
  977. 35:04percent sign
  978. 35:05than 3 so 10 divided by 3
  979. 35:08will have a remainder and we will store
  980. 35:10that remainder within a new variable
  981. 35:13so let's pretend that we have a total of
  982. 35:15ten people and we have to break up into
  983. 35:17groups of three so there's going to be
  984. 35:20one person remaining
  985. 35:21and i will just display that remainder
  986. 35:23of one but if this was in groups of two
  987. 35:26so friends modulus two well ten divides
  988. 35:29by two evenly
  989. 35:30so there will be no remainder and the
  990. 35:32value will be zero
  991. 35:34so if you need to find the remainder you
  992. 35:36can always use this modulus
  993. 35:38the remainder operator and it's also
  994. 35:40pretty good for finding if numbers are
  995. 35:42even or odd too so that's always a plus
  996. 35:44as well
  997. 35:45alright everybody so that is some basic
  998. 35:47arithmetic i will post this code to the
  999. 35:49comment section down below
  1000. 35:50and well that's some basic arithmetic in
  1001. 35:53c sharp
  1002. 35:54hey everyone in this video i'm going to
  1003. 35:56show you all a few useful methods of the
  1004. 35:59math class
  1005. 36:00so sit back relax and enjoy the show
  1006. 36:05alright everybody so here's a few useful
  1007. 36:07methods of the math class that you might
  1008. 36:09be interested in
  1009. 36:10let's begin with creating a double
  1010. 36:12variable named x
  1011. 36:13and i'll assign a value of three now the
  1012. 36:16first useful method
  1013. 36:17is the pow method that will raise a
  1014. 36:20value to a certain power
  1015. 36:22and we'll store this within a new
  1016. 36:24variable let's say a
  1017. 36:25double a equals then to access the math
  1018. 36:28class type
  1019. 36:30math with a capital m dot and here's a
  1020. 36:33whole bunch of methods they have access
  1021. 36:34to
  1022. 36:35so we are looking for the pow method
  1023. 36:38right here
  1024. 36:38then add a set of parentheses semicolon
  1025. 36:41now within the parentheses the first
  1026. 36:42value is your base so the base will be x
  1027. 36:45and that is three
  1028. 36:46and what power do we want to raise x to
  1029. 36:49let's raise x to the second power
  1030. 36:51and then display whatever our result is
  1031. 36:53a so within the right line statement
  1032. 36:55i'm going to display a so here is the
  1033. 36:58result
  1034. 36:583 to the power of 2 is
  1035. 37:01drum roll please 9.
  1036. 37:04so if we need 3 to the power of 3 that
  1037. 37:07would be
  1038. 37:08you know x whatever you're working with
  1039. 37:10that's the base
  1040. 37:11comma 3. so 3 to the power of 3
  1041. 37:15is 27. okay let's try square root i'll
  1042. 37:19turn this line into a comment
  1043. 37:20and we'll create double b equals
  1044. 37:24then again to access methods from the
  1045. 37:25math class type
  1046. 37:27math with a capital m dot and we are
  1047. 37:30looking for
  1048. 37:30square root that is all the way down
  1049. 37:34here
  1050. 37:34s q r t parenthesis semicolon within the
  1051. 37:38parentheses
  1052. 37:39we will find the square root of x
  1053. 37:43and then store it within b console.write
  1054. 37:47line
  1055. 37:48b
  1056. 37:50and that would be 1.73 something
  1057. 37:53something something something
  1058. 37:55so that is the square root method let's
  1059. 37:57move on
  1060. 37:58let's create a double c and we'll find
  1061. 38:01the absolute value of x
  1062. 38:04so math dot a b s
  1063. 38:08so the absolute value is how far away
  1064. 38:11something is
  1065. 38:11from zero so if you have a negative
  1066. 38:13number like negative three
  1067. 38:15well it's going to turn that into a
  1068. 38:17positive number
  1069. 38:19so the absolute value of negative 3 oh
  1070. 38:22we need to display c
  1071. 38:24so the absolute value of negative 3 is
  1072. 38:27in fact
  1073. 38:273 so that is the absolute value method
  1074. 38:31let's move on rounding so double
  1075. 38:35let's see we're on d equals math.round
  1076. 38:38and we can round a number
  1077. 38:40pay attention to the capitalization too
  1078. 38:42okay
  1079. 38:43now let's change x to 3.14
  1080. 38:47and then we will round x so place that
  1081. 38:49within the parentheses of the round
  1082. 38:51method
  1083. 38:52and we will display d
  1084. 38:56and 3.14 rounded is 3.
  1085. 38:59now you can always round a number up by
  1086. 39:02using the ceiling method
  1087. 39:03let's create double e equals
  1088. 39:07math.ceiling all right then we will
  1089. 39:10place
  1090. 39:11x within this method and display
  1091. 39:14e so 3.14
  1092. 39:18rounded up using the ceiling method is
  1093. 39:214.
  1094. 39:22alternatively there is a floor method
  1095. 39:24that will always round down
  1096. 39:26and i'm just going to copy this so
  1097. 39:28double
  1098. 39:30f math dot floor and
  1099. 39:33let's change this to 3.99
  1100. 39:36and display f
  1101. 39:39so 3.99 rounded down is three
  1102. 39:43okay now we'll need two variables for
  1103. 39:46this next example
  1104. 39:48so let's create double y equals
  1105. 39:52what about five okay so we have the
  1106. 39:55max method we can find the maximum value
  1107. 39:58of two values or variables so
  1108. 40:01double g equals math.max
  1109. 40:06and within the parentheses we want to
  1110. 40:08compare x and y
  1111. 40:10and separate each of these with a comma
  1112. 40:13so this will find
  1113. 40:14the max between x and y and store the
  1114. 40:17maximum number within
  1115. 40:18g and then we should display g
  1116. 40:22so the maximum between x and y is y
  1117. 40:26and that contains five then we have min
  1118. 40:30so double h math.min
  1119. 40:36console.writeline h so the minimum
  1120. 40:39between
  1121. 40:40x and y is x 3.99
  1122. 40:44all right everybody so those are a few
  1123. 40:46useful methods of the math class
  1124. 40:48i will post this code to the comments
  1125. 40:49section down below and well
  1126. 40:51yeah those are a few useful methods of
  1127. 40:53the math class
  1128. 40:55hey what's going on everybody it's bro
  1129. 40:57hope you're doing well and in this video
  1130. 40:58i'm going to explain how we can generate
  1131. 41:00some random numbers in c
  1132. 41:01sharp so sit back relax and enjoy the
  1133. 41:04show
  1134. 41:07all right people here's how to generate
  1135. 41:09some random numbers
  1136. 41:10just so you know these are not true
  1137. 41:11random numbers but rather
  1138. 41:13pseudo random numbers and they're fairly
  1139. 41:15darn close so to create some random
  1140. 41:17numbers we'll need to instantiate a
  1141. 41:19random object
  1142. 41:20type random with an uppercase r random
  1143. 41:23with lowercase r
  1144. 41:24equals new random parentheses semicolon
  1145. 41:28so the name of our random object is
  1146. 41:30random really you couldn't name this
  1147. 41:32anything but i usually just call it
  1148. 41:33random
  1149. 41:34and we can use this object to generate
  1150. 41:36random numbers and there's a few
  1151. 41:38built-in methods of this random object
  1152. 41:40to access them add a dot and we'll focus
  1153. 41:43on
  1154. 41:43the next method and next double method
  1155. 41:46next we'll generate a random whole
  1156. 41:48integer so we'll want to specify a range
  1157. 41:51this by itself will generate a random
  1158. 41:53number between i believe
  1159. 41:54zero and just over two billion so we'll
  1160. 41:57probably want to set a range
  1161. 41:58let's pretend that we're rolling a six
  1162. 42:00sided dice i need a random number
  1163. 42:02between
  1164. 42:03one and six so i can set that range
  1165. 42:05within the parentheses
  1166. 42:06so the minimum would be one comma and
  1167. 42:09the max would be
  1168. 42:10technically seven because the second
  1169. 42:12number is going to be exclusive
  1170. 42:14so this will give us a random number
  1171. 42:16between one and
  1172. 42:18six just specify the range and we'll
  1173. 42:20store this within a variable
  1174. 42:22int num equals random.next a random
  1175. 42:25number between
  1176. 42:26one and a six all right then let's
  1177. 42:29display this
  1178. 42:30console.writeline num and we should have
  1179. 42:33a random number between
  1180. 42:34one and six
  1181. 42:38and we got six all right then you can
  1182. 42:40always make adjustments to this too like
  1183. 42:42you can add
  1184. 42:42i don't know 100 and that should give us
  1185. 42:45a number between
  1186. 42:46101 and 106. so that's always an option
  1187. 42:50too
  1188. 42:51okay now what if you need to roll a
  1189. 42:5320-sided dice like we're playing
  1190. 42:54dungeons and dragons we use polyhedral
  1191. 42:57dice
  1192. 42:57so that would be 1 comma 21 and this
  1193. 43:00will give us a random number between
  1194. 43:021 and 20 and we rolled an 8.
  1195. 43:05now there is a next double method which
  1196. 43:08will generate a random
  1197. 43:09decimal number so let's say double num
  1198. 43:13equals random dot next double
  1199. 43:18and the random number will be between 0
  1200. 43:21and
  1201. 43:211. so we have 0.701
  1202. 43:26something something something something
  1203. 43:28so if you need a random
  1204. 43:29decimal or random double number you can
  1205. 43:31use random.next double
  1206. 43:34otherwise if you need a random hold
  1207. 43:36integer you're better off using the next
  1208. 43:38method of random
  1209. 43:40now if you need let's say three random
  1210. 43:42numbers we can keep on reusing the same
  1211. 43:44random object we only need to
  1212. 43:45instantiate it once
  1213. 43:47let's say we're going to roll three dice
  1214. 43:49so three
  1215. 43:50six sided dice let's copy what we have
  1216. 43:53and paste it twice
  1217. 43:54so we have num1 num2 num3
  1218. 43:58then let's display these
  1219. 44:00console.writeline num1
  1220. 44:02num2 and num3
  1221. 44:06so we will have three random numbers and
  1222. 44:09the result
  1223. 44:10is three six and four well okay then
  1224. 44:13everybody that
  1225. 44:14is how to generate random numbers in c
  1226. 44:16sharp
  1227. 44:17i will post this code to the comment
  1228. 44:19section down below if you can give this
  1229. 44:21video a thumbs up
  1230. 44:22drop a random comment down below and
  1231. 44:24well that's how to generate random
  1232. 44:25numbers
  1233. 44:26in c sharp hey what's going on people
  1234. 44:29it's
  1235. 44:29bro hope you're doing well and in this
  1236. 44:31video we're going to create a small
  1237. 44:32program to find the hypotenuse of a
  1238. 44:34right triangle
  1239. 44:35in c sharp so sit back relax and enjoy
  1240. 44:38the show
  1241. 44:41everyone i thought for practice we could
  1242. 44:43create a small program to find
  1243. 44:45the hypotenuse of a right triangle this
  1244. 44:47will be more or less just for practice
  1245. 44:49so we'll need some prompts
  1246. 44:51let's ask for sides a and b let's begin
  1247. 44:54with a
  1248. 44:55enter side a and then we'll store
  1249. 44:59the user's input within a double double
  1250. 45:02a equals and we'll convert the user
  1251. 45:05input
  1252. 45:06to a double convert to
  1253. 45:09double then within the parenthesis
  1254. 45:15console.read line
  1255. 45:17then another set of parenthesis make
  1256. 45:19sure there is a semicolon at the end too
  1257. 45:21okay then for side b we can just copy
  1258. 45:24what we have
  1259. 45:25change any instance of a to b enter
  1260. 45:28side b double b
  1261. 45:32and we'll have to calculate whatever c
  1262. 45:34is double
  1263. 45:35c math dot square root
  1264. 45:40then we need to multiply a times a
  1265. 45:46that's effectively a to the power of two
  1266. 45:49plus
  1267. 45:51b times b and
  1268. 45:54let's display the result
  1269. 45:58console.writeline
  1270. 46:00the hypotenuse
  1271. 46:04is plus c
  1272. 46:09and let's give it a go okay enter
  1273. 46:12side a how about three enter side b four
  1274. 46:16the hypotenuse is five well everybody
  1275. 46:18that was
  1276. 46:19a very small practice program if you
  1277. 46:22would like a copy of this code i will
  1278. 46:24post this to the comment section down
  1279. 46:25below
  1280. 46:26if you can give this video a thumbs up
  1281. 46:28drop a random comment down below
  1282. 46:30and well that is a very small sample
  1283. 46:32program
  1284. 46:33in c sharp hey what's going on everybody
  1285. 46:37it's you bro hope you're doing well and
  1286. 46:38in this video i'm going to show you all
  1287. 46:40some useful string methods in c
  1288. 46:42sharp so sit back relax and enjoy the
  1289. 46:44show
  1290. 46:47all right everybody here's a few useful
  1291. 46:49string methods that you may be
  1292. 46:51interested in
  1293. 46:52so to begin we'll need a string to work
  1294. 46:53with or a variable containing a string
  1295. 46:56so why don't we create a variable named
  1296. 46:58full name to store your own full name
  1297. 47:01now to access some methods available to
  1298. 47:04strings we'll need
  1299. 47:04to type a string or a variable
  1300. 47:06containing a string
  1301. 47:08then follow this with a dot and there
  1302. 47:09should be a prompt for
  1303. 47:11a whole bunch of methods that we have
  1304. 47:13access to i'll show you some of the more
  1305. 47:15useful ones
  1306. 47:16like two upper full name dot to upper
  1307. 47:18then to invoke this
  1308. 47:20add a set of parentheses then a
  1309. 47:22semicolon to finish this statement but
  1310. 47:24then we'll need to reassign this
  1311. 47:26full name equals full name to upper
  1312. 47:29then we can display our full name and
  1313. 47:31when i run this
  1314. 47:32my name should all be uppercase
  1315. 47:35just like that okay now alternatively
  1316. 47:38there's
  1317. 47:39two lower it's the same process as
  1318. 47:41before
  1319. 47:42but change the method to lower
  1320. 47:46and my name should all be lowercase okay
  1321. 47:49so that's
  1322. 47:49two upper and two lower let's move on
  1323. 47:52for this next example we'll need a phone
  1324. 47:54number
  1325. 47:55so let's create a string named phone
  1326. 47:58number equals and make up some phone
  1327. 48:01number and be sure to include some
  1328. 48:03dashes
  1329. 48:03one two three four five six seven eight
  1330. 48:06nine zero
  1331. 48:07okay what i'm going to do is replace
  1332. 48:09these dashes with a different character
  1333. 48:12so to do that we'll need to use the
  1334. 48:14replace method
  1335. 48:15so phone number dot replace
  1336. 48:20so within the set of parentheses we'll
  1337. 48:22need the character we're going to
  1338. 48:24replace
  1339. 48:25and another character we're going to
  1340. 48:27replace the old character with
  1341. 48:29so i would like to replace all dashes
  1342. 48:32with a forward slash so separate each of
  1343. 48:35these characters with a comma
  1344. 48:38so this will replace my phone number the
  1345. 48:40dash is within the phone number
  1346. 48:42with forward slashes phone number equals
  1347. 48:44phone number dot replace
  1348. 48:46and let's display our phone number with
  1349. 48:49a right line statement
  1350. 48:51so all of these dashes have been
  1351. 48:53replaced with forward slashes
  1352. 48:55now if you replace these with no
  1353. 48:57characters just an empty set
  1354. 48:59of quotes that will effectively remove
  1355. 49:02those dashes within the phone number
  1356. 49:04which could have some uses too so that
  1357. 49:06is the replace
  1358. 49:07method available to strings next we have
  1359. 49:10the insert method we can insert
  1360. 49:12a character or a string at a given index
  1361. 49:14within a pre-existing string
  1362. 49:16let's say i would like to take my full
  1363. 49:18name and turn it into some sort of
  1364. 49:20username
  1365. 49:21by preceding my first name with the at
  1366. 49:23sign
  1367. 49:24so to do that i can use the insert
  1368. 49:26method i'll create a new
  1369. 49:28variable for this example let's say
  1370. 49:29string username
  1371. 49:31equals full name dot insert
  1372. 49:34and with the insert method we will list
  1373. 49:36an index
  1374. 49:38and what we would like to insert so
  1375. 49:40computers they always start at zero
  1376. 49:42the beginning would be zero that would
  1377. 49:43be the first index
  1378. 49:45so at the beginning index zero i would
  1379. 49:47like to insert
  1380. 49:48an at sign and this will create a new
  1381. 49:51variable called username
  1382. 49:53so i will display that within a
  1383. 49:55rightline statement
  1384. 49:57console.writeline username and my
  1385. 50:00username is now at whatever my full name
  1386. 50:03was
  1387. 50:04now this doesn't necessarily have to be
  1388. 50:06a single character we could
  1389. 50:07say like mr dot and precede
  1390. 50:11my full name with mr and my username is
  1391. 50:15now mr bro code okay so that is the
  1392. 50:18insert
  1393. 50:18method what i'm about to show you next
  1394. 50:21technically
  1395. 50:21isn't a method it's a property but it's
  1396. 50:23found within the same place as methods
  1397. 50:25for
  1398. 50:26strings so to access the length property
  1399. 50:29of a string type a string dot link
  1400. 50:33and you do not add a set of parenthesis
  1401. 50:35afterwards that is done with methods
  1402. 50:37so the length property will return the
  1403. 50:40given amount of characters within a
  1404. 50:41string
  1405. 50:42so i'm going to display this within a
  1406. 50:44rightline statement
  1407. 50:45if you need this you can always like
  1408. 50:47assign this to a variable or something
  1409. 50:49but that might be overkill for this
  1410. 50:51lesson so console.writeline
  1411. 50:53the length of my full name and my full
  1412. 50:56name
  1413. 50:56has eight characters in it one potential
  1414. 51:00use of the length property of strings
  1415. 51:02is that let's say that somebody's
  1416. 51:03creating a username and there's a max 12
  1417. 51:05characters
  1418. 51:06well you can get the length of whatever
  1419. 51:07they type in and check to see if it's
  1420. 51:09more than 12 characters
  1421. 51:10this next method is the substring method
  1422. 51:13and it can be a little complex
  1423. 51:15basically speaking we can take a section
  1424. 51:17from an original string
  1425. 51:19and create an entirely new string but we
  1426. 51:21have to specify
  1427. 51:22a position and how many characters you
  1428. 51:24would like to extract
  1429. 51:26from that given position so we'll create
  1430. 51:29two new strings
  1431. 51:30from full name first name and last name
  1432. 51:32using the substring method
  1433. 51:34so let's declare string first name
  1434. 51:38equals full name dot
  1435. 51:41substring okay so within the substring
  1436. 51:45we're going to list
  1437. 51:46an index and the amount of characters
  1438. 51:48afterwards we would like to include
  1439. 51:50within our substring
  1440. 51:51so i would like to take the first three
  1441. 51:54characters from my full name
  1442. 51:55so the index computers always start with
  1443. 51:57zero of the beginning of my string
  1444. 51:59would be zero and i'm going to take
  1445. 52:01three characters
  1446. 52:02after this index so if i was to display
  1447. 52:06my first name now it's going to be
  1448. 52:09bro the first three characters of my
  1449. 52:11full name
  1450. 52:12now let's do the same thing with last
  1451. 52:14name so let's copy what we have
  1452. 52:17change first name to last name but now
  1453. 52:20we have a different index
  1454. 52:22so i would like my substring to begin
  1455. 52:24where the c is
  1456. 52:25so let's count what the index is going
  1457. 52:27to be this would be 0
  1458. 52:291 2 3 four so let's change
  1459. 52:32zero to four and how many characters
  1460. 52:35would we like to include within our
  1461. 52:36substring
  1462. 52:38one two three four so four
  1463. 52:41comma four and that is my last name so i
  1464. 52:43will display that within
  1465. 52:45a writeline statement first name and
  1466. 52:47last name
  1467. 52:49so that is the substring method we can
  1468. 52:51take a section or a slice
  1469. 52:53from an original string and create an
  1470. 52:55entirely new string from it
  1471. 52:57all right everybody those are a few
  1472. 52:59useful string methods
  1473. 53:01but not all of them so if you would like
  1474. 53:03a complete list you can always type a
  1475. 53:05string
  1476. 53:06dot and there should be a pop-up for all
  1477. 53:08of the different string methods you can
  1478. 53:09access
  1479. 53:10and if you highlight one of these
  1480. 53:11there's a description of what they do
  1481. 53:13exactly
  1482. 53:14i only showed you a few of the more
  1483. 53:15useful ones for beginners
  1484. 53:17so if you can give this video a big fat
  1485. 53:19thumbs up drop a random comment down
  1486. 53:21below and well
  1487. 53:22those are some useful string methods in
  1488. 53:25c sharp
  1489. 53:28all right people if statements an if
  1490. 53:31statement
  1491. 53:32is a basic form of decision making if
  1492. 53:34some condition that we specify
  1493. 53:36is true we can execute a block of code
  1494. 53:39if it's not true we can do something
  1495. 53:40else entirely
  1496. 53:42so let's write a small program where we
  1497. 53:44will ask a user to enter in their edge
  1498. 53:46and depending on their age we can write
  1499. 53:48an if statement that checks to see if
  1500. 53:50somebody is 18 years or older
  1501. 53:52let's say that a user is signing up for
  1502. 53:54a credit card and you have to be 18
  1503. 53:55years or older
  1504. 53:56so if their age is greater than or equal
  1505. 53:59to 18
  1506. 54:00we will display a message that says
  1507. 54:02you're now signed up if they're less
  1508. 54:03than 18 we can do something else
  1509. 54:06so to write an if statement we just
  1510. 54:07write if then parentheses to hold
  1511. 54:10some sort of condition which we would
  1512. 54:11like to evaluate followed by a set of
  1513. 54:13parentheses
  1514. 54:14so if some condition that we set within
  1515. 54:16the parentheses is true
  1516. 54:18we will execute this block of code if
  1517. 54:20it's not true we skip over it entirely
  1518. 54:22so let's check to see if age is greater
  1519. 54:24than or equal to 18
  1520. 54:26then we will display something console
  1521. 54:29that right line you are now signed up
  1522. 54:34and let's try it please enter your age
  1523. 54:37let's say that i'm 21 i hit enter
  1524. 54:39boom you are now signed up now what if i
  1525. 54:41enter in an edge that's less than 18
  1526. 54:43let's say that i'm 12 years old and i'm
  1527. 54:45trying to sign up for a credit card
  1528. 54:46well nothing appears to happen and
  1529. 54:48that's normal so if
  1530. 54:50this condition evaluates to be true we
  1531. 54:52execute this block of code
  1532. 54:54if it's false we skip over it entirely
  1533. 54:56as if it never existed
  1534. 54:58now we can take a different course of
  1535. 54:59action if our above condition
  1536. 55:02is false we can perform some other block
  1537. 55:04of code
  1538. 55:05so let's display a message that says you
  1539. 55:06must be 18 years or older to sign up
  1540. 55:09you must be 18 plus to sign
  1541. 55:12up so if this condition is true we do
  1542. 55:15this
  1543. 55:15if it's false we do this so let's try it
  1544. 55:18again
  1545. 55:19please enter your age let's say that i'm
  1546. 55:2012 i hit enter you must be 18 plus to
  1547. 55:23sign up
  1548. 55:24so if this is true do this if it's not
  1549. 55:27true
  1550. 55:27do this now between the if and else
  1551. 55:30statements you can add an else if
  1552. 55:31statement
  1553. 55:32if there's more things they want to
  1554. 55:33check before reaching the else block
  1555. 55:35so the else block is kind of like a last
  1556. 55:38resort if all of buff statements
  1557. 55:40evaluate to be false
  1558. 55:42let's check to see if somebody's age is
  1559. 55:45less than zero else if age is less than
  1560. 55:48zero
  1561. 55:49then we will display a custom message
  1562. 55:51such as
  1563. 55:53you haven't been born yet
  1564. 55:57please enter your age i am negative 12
  1565. 56:00years old
  1566. 56:01you haven't been born yet with these
  1567. 56:03else if statements you can add as many
  1568. 56:05as you want between
  1569. 56:06if and else let's add another so i will
  1570. 56:08copy what we have
  1571. 56:10let's check to see if somebody's age is
  1572. 56:12greater than 100
  1573. 56:13if it is then we'll say i don't know
  1574. 56:15you're too old to sign up
  1575. 56:16that's kind of mean i guess but it
  1576. 56:18serves the purpose of this example
  1577. 56:20so you are too old to sign
  1578. 56:23up now we're going to run into one issue
  1579. 56:27okay please enter your age i am 101
  1580. 56:29years old
  1581. 56:30you are now signed up now the reason
  1582. 56:33that this block of code executed and not
  1583. 56:35this one is that we're just going down
  1584. 56:37an order starting from the top
  1585. 56:39since age which was 101 is technically
  1586. 56:42greater than or equal to 18
  1587. 56:44this condition evaluated to be true and
  1588. 56:47we executed
  1589. 56:48this block of code and skipped
  1590. 56:49everything else after it and continued
  1591. 56:51on to the rest of the program
  1592. 56:53so if we first want to check to see if
  1593. 56:55somebody's age is greater than
  1594. 56:56100 we should probably put that within
  1595. 56:59the if statement and check that first
  1596. 57:01because age is technically greater than
  1597. 57:03or equal to 18 if we set age to be
  1598. 57:06101 else if we will check to see if age
  1599. 57:09is greater than or equal to 18
  1600. 57:11then we just have to change these lines
  1601. 57:13of text around
  1602. 57:15so you are too old to sign up so the
  1603. 57:18order of
  1604. 57:19these if statements and else if
  1605. 57:20statements does matter
  1606. 57:22and let's try it again okay please enter
  1607. 57:24your age i am 101 years old
  1608. 57:26you are too old to sign up all right now
  1609. 57:29in this next example let's ask a user
  1610. 57:31for their name instead of their age
  1611. 57:33let's change int age to string name
  1612. 57:36and we do not need to convert this
  1613. 57:38because it's already a string
  1614. 57:40so to check to see if somebody does not
  1615. 57:42enter in some user input
  1616. 57:43we can check to see if our variable name
  1617. 57:48equals an empty set of quotes so this is
  1618. 57:51an empty string
  1619. 57:52now the reason that we're using two
  1620. 57:53equal signs is that this is a comparison
  1621. 57:56operator
  1622. 57:57if you just use one that is an
  1623. 57:59assignment operator
  1624. 58:00c sharp thinks you're attempting to
  1625. 58:02assign name with an empty string
  1626. 58:05so we're comparing if these two are
  1627. 58:07equal so that's why we use
  1628. 58:09double equals so if name is equal to an
  1629. 58:11empty string
  1630. 58:12that means that somebody skipped over
  1631. 58:14entering in some user input
  1632. 58:16so let's yell at them you did
  1633. 58:19not enter your name so
  1634. 58:22if our name is not empty we can display
  1635. 58:26maybe hello whatever the user's name is
  1636. 58:28so
  1637. 58:30hello plus name
  1638. 58:34and let's try it
  1639. 58:37okay please enter your name i'm just
  1640. 58:38going to hit enter you did not enter
  1641. 58:40your name
  1642. 58:41let's run this again and take it
  1643. 58:43seriously this time please enter your
  1644. 58:45name
  1645. 58:45bro hello bro now there's another way in
  1646. 58:48which we could
  1647. 58:49write this so instead of using this
  1648. 58:51double equal sign we can use
  1649. 58:53exclamation point equal sign that means
  1650. 58:56not equals
  1651. 58:57if name does not equal an empty string
  1652. 58:59then we will display
  1653. 59:01hello name else we will display
  1654. 59:05you did not enter your name so we're
  1655. 59:07checking to see if name is
  1656. 59:08not equal to an empty string and if
  1657. 59:10that's true display hello
  1658. 59:12name okay please enter your name i'll
  1659. 59:15hit enter
  1660. 59:16you did not enter your name let's try it
  1661. 59:18again please enter your name
  1662. 59:20bro hello bro all right everybody those
  1663. 59:23are if statements they're a very basic
  1664. 59:26form of decision making we can check to
  1665. 59:27see if some condition
  1666. 59:29is true if it is true then we can do
  1667. 59:31something if not we can do something
  1668. 59:32else
  1669. 59:33so if you would like a copy of both
  1670. 59:35examples from this video i will post
  1671. 59:37that code to the comment section down
  1672. 59:39below
  1673. 59:39and well those are if statements and see
  1674. 59:42you sharp
  1675. 59:43hey what's going on everybody it's you
  1676. 59:45bro hope you're doing well
  1677. 59:47and in this video i'm going to explain
  1678. 59:49switches in c-sharp
  1679. 59:50so sit back relax and enjoy the show
  1680. 59:55all right everybody switches a switch is
  1681. 59:58an efficient alternative
  1682. 59:59to writing many else if statements i
  1683. 1:00:01wrote a small program
  1684. 1:00:03we'll ask a user what day is it today
  1685. 1:00:05like what day of the week
  1686. 1:00:06and we'll store their input within a
  1687. 1:00:08variable named day
  1688. 1:00:10if day equals monday we'll write it's
  1689. 1:00:12monday and we have else if statements
  1690. 1:00:14for every day of the week that follows
  1691. 1:00:16after and an else block at the end
  1692. 1:00:18day is not a day they type in something
  1693. 1:00:20that's obviously not a day
  1694. 1:00:22using else if statements back to back
  1695. 1:00:24like this really isn't that efficient
  1696. 1:00:26and if a veteran programmer sees you do
  1697. 1:00:28something like this
  1698. 1:00:29they'll probably call you a noob a few
  1699. 1:00:31else if statements is okay but
  1700. 1:00:33this is somewhat excessive so let's
  1701. 1:00:35rewrite this program
  1702. 1:00:36so that we use a switch instead here's
  1703. 1:00:39how to create one we'll keep our prompt
  1704. 1:00:40too
  1705. 1:00:41so switch parentheses curly braces
  1706. 1:00:44within the parentheses we will place
  1707. 1:00:47what we would like to examine
  1708. 1:00:48we will examine our day variable against
  1709. 1:00:51many cases
  1710. 1:00:52and if there is a match then we will
  1711. 1:00:54execute some block of code
  1712. 1:00:56so to write a case write case then some
  1713. 1:00:59value
  1714. 1:01:00like monday then end this with a colon
  1715. 1:01:03so anything indented after our case is
  1716. 1:01:06part of the block of code for this case
  1717. 1:01:09so if we have a match if day equals case
  1718. 1:01:12monday then let's write it's monday
  1719. 1:01:17then on the next line add a break to
  1720. 1:01:19exit the switch
  1721. 1:01:20okay so that's one case let's write a
  1722. 1:01:23case for each
  1723. 1:01:24day of the week and i'll just fast
  1724. 1:01:25forward this footage
  1725. 1:01:31and our switch is now complete although
  1726. 1:01:33i did not add that line from the else
  1727. 1:01:35statement quite yet we'll take care of
  1728. 1:01:37that a little bit later
  1729. 1:01:38so let's run this just to test it so
  1730. 1:01:41what day is it today let's say that it's
  1731. 1:01:42sunday
  1732. 1:01:43enter it's sunday nice one thing that
  1733. 1:01:46we're missing although it's optional
  1734. 1:01:48is a default case if there's no matches
  1735. 1:01:50what we want to do
  1736. 1:01:52so currently there is no default case
  1737. 1:01:54within the switch
  1738. 1:01:55so what day is it today if i write
  1739. 1:01:57something incorrect like
  1740. 1:01:58pizza well the switch technically
  1741. 1:02:00doesn't do anything
  1742. 1:02:01so if there are no matching cases let's
  1743. 1:02:04do something
  1744. 1:02:05and we type default colon space if there
  1745. 1:02:07are no matching cases we will perform
  1746. 1:02:10this block of code at the end so let's
  1747. 1:02:12write something
  1748. 1:02:13day plus is
  1749. 1:02:16not a day and then break
  1750. 1:02:21and let's try it again so this is kind
  1751. 1:02:23of like our else statement
  1752. 1:02:24okay what day is it today this time
  1753. 1:02:27let's say it's
  1754. 1:02:28birthday birthday is not a day
  1755. 1:02:32all right everybody those are switches
  1756. 1:02:34they're efficient alternatives to using
  1757. 1:02:37many else if statements
  1758. 1:02:38if you would like a copy of this code i
  1759. 1:02:40will post this to the comment section
  1760. 1:02:42down below
  1761. 1:02:43if you can give this video a thumbs up
  1762. 1:02:45drop a random comment down below
  1763. 1:02:47and those are switches in c sharp
  1764. 1:02:52all right logical operators we can use
  1765. 1:02:54logical operators to check to see if
  1766. 1:02:56more than one condition
  1767. 1:02:58is true or false we'll use two logical
  1768. 1:03:01operators
  1769. 1:03:02and as well as or and is represented by
  1770. 1:03:04two ampersands
  1771. 1:03:06and or is represented by two vertical
  1772. 1:03:08bars we'll create a small program
  1773. 1:03:10to ask the user what the temperature is
  1774. 1:03:12outside and depending on what the user
  1775. 1:03:13types in will display a custom message
  1776. 1:03:16so we'll need to work with ranges of
  1777. 1:03:18temperatures so let's begin we'll ask
  1778. 1:03:20the user
  1779. 1:03:20for some input so console.writeline
  1780. 1:03:24what's the temperature outside
  1781. 1:03:27and you can write this in fahrenheit or
  1782. 1:03:29celsius but i'll use celsius
  1783. 1:03:31okay then we'll store our input within a
  1784. 1:03:34double
  1785. 1:03:34variable named temp short for
  1786. 1:03:36temperature and we'll need to use
  1787. 1:03:39console.read
  1788. 1:03:40line and then we'll need to convert this
  1789. 1:03:43to a double
  1790. 1:03:44using convert dot 2 double method so
  1791. 1:03:47convert
  1792. 1:03:47dot 2 double then within the parenthesis
  1793. 1:03:51place your redline method within here
  1794. 1:03:56okay now we need to check to see if our
  1795. 1:03:59temperature falls within a certain range
  1796. 1:04:01using an if statement and using the and
  1797. 1:04:03logical operator
  1798. 1:04:04that will help us with that so let's
  1799. 1:04:06check to see if our temp
  1800. 1:04:08is greater than or equal to 10 degrees
  1801. 1:04:11celsius and if our temperature
  1802. 1:04:14is less than or equal to what about 25
  1803. 1:04:18so both of these conditions must be true
  1804. 1:04:21in order for us to execute this
  1805. 1:04:23if statement if one of them is false we
  1806. 1:04:26will skip over this if statement
  1807. 1:04:27so this condition needs to be true and
  1808. 1:04:30this condition needs to be true so our
  1809. 1:04:33temperature has to fall within
  1810. 1:04:34that range so we will display a custom
  1811. 1:04:37message
  1812. 1:04:38like it's warm outside it's not hot
  1813. 1:04:42it's not cold it's just warm let's try
  1814. 1:04:44it
  1815. 1:04:45okay so what if the temperature was like
  1816. 1:04:4935 degrees celsius well nothing appears
  1817. 1:04:52to happen we did not execute
  1818. 1:04:54this if statement even though this
  1819. 1:04:56condition is true
  1820. 1:04:57this condition must also be true because
  1821. 1:04:59we're using the and logical operator
  1822. 1:05:02so let's try this again and type in a
  1823. 1:05:04temperature between that range
  1824. 1:05:05what's the temperature outside let's say
  1825. 1:05:07that it's 15 degrees celsius
  1826. 1:05:10it's warm outside now using the or
  1827. 1:05:12logical operator
  1828. 1:05:14only one of these conditions needs to be
  1829. 1:05:16true in order for this entire expression
  1830. 1:05:18to evaluate to be true unlike with the
  1831. 1:05:21analogical
  1832. 1:05:22operator both conditions must be true so
  1833. 1:05:24let's check for some extreme
  1834. 1:05:25temperatures using an else if statement
  1835. 1:05:28else if temp let's say is less than or
  1836. 1:05:32equal to
  1837. 1:05:33negative 50 or temp
  1838. 1:05:36is greater than or equal to 50
  1839. 1:05:40then we will display do
  1840. 1:05:43not go outside
  1841. 1:05:46let's try it okay what's the temperature
  1842. 1:05:49outside
  1843. 1:05:50let's say that it's negative 52 degrees
  1844. 1:05:52celsius do not go outside
  1845. 1:05:54so even though this condition is true
  1846. 1:05:57and
  1847. 1:05:57this one is false using the or logical
  1848. 1:06:00operator
  1849. 1:06:00only one of these conditions needs to be
  1850. 1:06:02true in order for our entire expression
  1851. 1:06:05to evaluate to be true so let's do the
  1852. 1:06:07same thing with a
  1853. 1:06:08very high temperature this time what's
  1854. 1:06:11the temperature outside let's say that
  1855. 1:06:13it's like a kajillion degrees celsius
  1856. 1:06:15maybe our sun explodes or something do
  1857. 1:06:18not go outside
  1858. 1:06:19well okay then everybody those are
  1859. 1:06:21logical operators we can use them to
  1860. 1:06:23check to see if more than one condition
  1861. 1:06:25is true or false so if you would like a
  1862. 1:06:28copy of this code i will post this to
  1863. 1:06:30the comment section down below and well
  1864. 1:06:32those are logical operators in c sharp
  1865. 1:06:38all right while loops a while loop will
  1866. 1:06:40repeat some code while some condition
  1867. 1:06:42remains true we'll write a small program
  1868. 1:06:45where we will ask a user to enter in
  1869. 1:06:47their name
  1870. 1:06:47if they do not enter in anything we will
  1871. 1:06:49keep on prompting them to enter their
  1872. 1:06:51name they have to enter something
  1873. 1:06:53in order to escape the loop so this is
  1874. 1:06:55what we'll do to begin with
  1875. 1:06:56let's create a prompt to ask a user for
  1876. 1:06:58their name
  1877. 1:06:59i'll use a write statement instead of a
  1878. 1:07:01writeline statement
  1879. 1:07:03and let's say enter your name
  1880. 1:07:06and then we will declare a string
  1881. 1:07:08variable named name
  1882. 1:07:13the console.readline and then store that
  1883. 1:07:15within variable name
  1884. 1:07:17and then at the end of our program we'll
  1885. 1:07:19say hello
  1886. 1:07:20plus name okay so after running this
  1887. 1:07:24what if a user doesn't type in anything
  1888. 1:07:26like they just had enter
  1889. 1:07:28hello and then there's nothing here so
  1890. 1:07:30what if we
  1891. 1:07:31try and force the user to enter in
  1892. 1:07:33something we can write this code within
  1893. 1:07:35a while loop
  1894. 1:07:36so after we accept user input for the
  1895. 1:07:39first time
  1896. 1:07:39let's check that user input and we'll
  1897. 1:07:42use a while loop
  1898. 1:07:43our condition will be while name is
  1899. 1:07:45equal to
  1900. 1:07:46an empty string we will prompt the user
  1901. 1:07:49again
  1902. 1:07:49to enter in something for their name and
  1903. 1:07:52we do not need to declare
  1904. 1:07:53name twice name equals console.readline
  1905. 1:07:56so let's try this again our condition is
  1906. 1:07:58we will continue
  1907. 1:07:59this while loop this section of code as
  1908. 1:08:02long as name
  1909. 1:08:03equals an empty string if they just hit
  1910. 1:08:05enter so let's try it
  1911. 1:08:06okay enter your name i'm going to hit
  1912. 1:08:08enter enter your name no
  1913. 1:08:10enter your name no and to your name no
  1914. 1:08:12okay i'll type in something
  1915. 1:08:14hello bro so with a while loop you'll
  1916. 1:08:17need some way to eventually
  1917. 1:08:18exit the while loop otherwise that is
  1918. 1:08:20known as an infinite loop
  1919. 1:08:22and here's an example of an infinite
  1920. 1:08:23loop i'm going to say
  1921. 1:08:25while one is equal to one
  1922. 1:08:28and then let's display something else
  1923. 1:08:30such as
  1924. 1:08:33help i'm stuck in
  1925. 1:08:36an infinite loop so basically we have no
  1926. 1:08:40way to
  1927. 1:08:41change this condition so it's always
  1928. 1:08:43going to be true
  1929. 1:08:44and we will repeat this code forever so
  1930. 1:08:46let's try it again
  1931. 1:08:50okay enter your name help i'm stuck in
  1932. 1:08:53an infinite loop
  1933. 1:08:55so with a while loop you'll eventually
  1934. 1:08:57want some way to eventually
  1935. 1:08:58exit the while loop now to optimize this
  1936. 1:09:01code we don't technically need
  1937. 1:09:03this right statement here because we
  1938. 1:09:04will ask that within the while loop
  1939. 1:09:07and i'm going to declare string name and
  1940. 1:09:10assign this
  1941. 1:09:11an empty string so this would work the
  1942. 1:09:13same as before as well
  1943. 1:09:15so enter your name i do not type in
  1944. 1:09:17anything and we are prompted again to
  1945. 1:09:19type in our name
  1946. 1:09:20and i'll type in something well okay
  1947. 1:09:22then everybody those are while loops
  1948. 1:09:24they will repeat
  1949. 1:09:25some code while some condition remains
  1950. 1:09:28true
  1951. 1:09:29and it will do so infinitely until this
  1952. 1:09:32condition becomes false
  1953. 1:09:34if you would like a copy of this code i
  1954. 1:09:35will post this to the comment section
  1955. 1:09:37down below
  1956. 1:09:38if you can give this video a thumbs up
  1957. 1:09:40drop a random comment down below
  1958. 1:09:41and well those are wild loops in c sharp
  1959. 1:09:47okay
  1960. 1:09:48for loops a for loop will repeat some
  1961. 1:09:51code a
  1962. 1:09:52finite amount of times unlike with while
  1963. 1:09:55loops a while loop will execute an
  1964. 1:09:57infinite amount of times as long as its
  1965. 1:09:59condition remains true
  1966. 1:10:01for loops are limited so let's create a
  1967. 1:10:03for loop to i don't know
  1968. 1:10:05count to ten so let's create a for loop
  1969. 1:10:07by typing four
  1970. 1:10:08parentheses curly braces and within the
  1971. 1:10:11for loop the parentheses of the for loop
  1972. 1:10:14there's three separate statements we're
  1973. 1:10:16going to fill in the first
  1974. 1:10:17is that we need some sort of counter or
  1975. 1:10:19index to keep track of which iteration
  1976. 1:10:22we're on within the for loop
  1977. 1:10:23so let's declare an index int index
  1978. 1:10:26and i will assign zero to begin with so
  1979. 1:10:29people usually just shorten
  1980. 1:10:31index to simply i for short
  1981. 1:10:34so that is the first statement the
  1982. 1:10:36second statement is our condition
  1983. 1:10:38when do we want to stop so let's stop
  1984. 1:10:41when
  1985. 1:10:41i is greater than or equal to 10.
  1986. 1:10:44so we'll just say i is less than 10.
  1987. 1:10:48so it's kind of like we'll continue this
  1988. 1:10:50for loop as long as
  1989. 1:10:51i is less than 10 and once we hit 10
  1990. 1:10:54we'll stop
  1991. 1:10:55and then the third statement is how much
  1992. 1:10:57do we want to
  1993. 1:10:58increment or decrement our index by so
  1994. 1:11:01you can write
  1995. 1:11:02i plus equals one to increment by one or
  1996. 1:11:05another variation is just
  1997. 1:11:07i plus plus so after each iteration
  1998. 1:11:09we'll increment i
  1999. 1:11:11our counter our index by one and during
  2000. 1:11:14each iteration
  2001. 1:11:14let's display whatever i is so after
  2002. 1:11:17running this code
  2003. 1:11:18we will execute this for loop ten times
  2004. 1:11:21and we will count
  2005. 1:11:22the numbers zero through nine so if we
  2006. 1:11:25want 1 through 10 we can change our
  2007. 1:11:27index
  2008. 1:11:27to 1. into i equals 1 and we will
  2009. 1:11:31continue this for loop as long as i
  2010. 1:11:33is less than or equal to 10 and then
  2011. 1:11:36our for loop will execute a total of ten
  2012. 1:11:39times and count the numbers one through
  2013. 1:11:41ten
  2014. 1:11:42and you can also skip iterations too
  2015. 1:11:44like we can increment
  2016. 1:11:45our index i by two i plus equals
  2017. 1:11:49two
  2018. 1:11:52now this for loop is going to execute
  2019. 1:11:54five times we're incrementing our index
  2020. 1:11:57by two during each iteration
  2021. 1:11:59let's change this to three i plus equals
  2022. 1:12:02three
  2023. 1:12:04and this will execute four times one 4 7
  2024. 1:12:0710. okay let's do something a little bit
  2025. 1:12:10different let's count down let's pretend
  2026. 1:12:12that we're counting down to a new year
  2027. 1:12:14so we'll start at 10 and count down to
  2028. 1:12:16one then display happy new year
  2029. 1:12:18so four parentheses curly braces
  2030. 1:12:22we'll set index i to equal 10 to begin
  2031. 1:12:25with
  2032. 1:12:26we'll continue this as long as i is
  2033. 1:12:28greater than
  2034. 1:12:29zero then we will decrement i by one
  2035. 1:12:33during each iteration so you can use
  2036. 1:12:35minus minus or
  2037. 1:12:36minus equals one either variation works
  2038. 1:12:39okay so
  2039. 1:12:40after each iteration we will display i
  2040. 1:12:43then when we exit our for loop we will
  2041. 1:12:46display happy new year within a
  2042. 1:12:47rightline statement
  2043. 1:12:48so happy new year
  2044. 1:12:52let's try it okay 10 9 8 7
  2045. 1:12:556 5 4 3 2 1 happy new year
  2046. 1:12:59so everybody those are for loops they
  2047. 1:13:01will repeat
  2048. 1:13:02some code a finite amount of times
  2049. 1:13:05unlike with a while loop a while loop
  2050. 1:13:07will execute
  2051. 1:13:08some code infinitely as long as its
  2052. 1:13:10condition remains true
  2053. 1:13:12a for loop is limited in that regard
  2054. 1:13:14well everybody
  2055. 1:13:15those are for loops if you can give this
  2056. 1:13:18video a big fat thumbs up
  2057. 1:13:19drop a random comment down below and
  2058. 1:13:21well those are for loops
  2059. 1:13:23in c sharp hey what's going on everybody
  2060. 1:13:25it's you bro hope you're doing well
  2061. 1:13:27and in this video we're going to use
  2062. 1:13:29nested loops to create a program to draw
  2063. 1:13:31a rectangle for us made out of a certain
  2064. 1:13:33symbol that we choose
  2065. 1:13:34so sit back relax and enjoy the show
  2066. 1:13:39all right welcome back so nested loops a
  2067. 1:13:42nested loop is a
  2068. 1:13:44loop that's inside of another loop there
  2069. 1:13:47is an outer loop
  2070. 1:13:48and an inner loop and both of these
  2071. 1:13:50combined are nested
  2072. 1:13:51loops so the uses they vary they're used
  2073. 1:13:54a lot in sorting algorithms so i thought
  2074. 1:13:56let's create a program where we will
  2075. 1:13:58draw a rectangle
  2076. 1:14:00we'll have the outer loop in charge of
  2077. 1:14:02counting the rows
  2078. 1:14:03and the inner loop in charge of counting
  2079. 1:14:05the columns and we will let the user
  2080. 1:14:07choose a symbol that they would like to
  2081. 1:14:09create their rectangle of
  2082. 1:14:10so we'll need to accept some user input
  2083. 1:14:13so console.writeline
  2084. 1:14:15let's ask how many rows how many
  2085. 1:14:18rows actually let's make this a write
  2086. 1:14:23statement
  2087. 1:14:24okay and then we will store this within
  2088. 1:14:26int rows
  2089. 1:14:28equals then we will need to convert some
  2090. 1:14:30user input
  2091. 1:14:31convert 2 and 32
  2092. 1:14:34because user input's always a string
  2093. 1:14:38then within the parenthesis console.read
  2094. 1:14:44okay let's ask for columns
  2095. 1:14:47so let's copy this paste it
  2096. 1:14:51how many columns int
  2097. 1:14:55columns then let's ask the user for a
  2098. 1:14:59symbol to use
  2099. 1:15:05what symbol or character
  2100. 1:15:09and let's use a string i guess you can
  2101. 1:15:12use a char as well
  2102. 1:15:14string symbol and we do not need to
  2103. 1:15:16convert this because it's already a
  2104. 1:15:18string
  2105. 1:15:19okay we'll have an outer loop in charge
  2106. 1:15:21of counting the rows
  2107. 1:15:23and the inner loop is in charge of
  2108. 1:15:24counting the columns
  2109. 1:15:27so four then we need our index
  2110. 1:15:30by the way it doesn't matter if you use
  2111. 1:15:32a while loop or a for loop
  2112. 1:15:34it's just the concept of having a loop
  2113. 1:15:36inside of another loop i just so happen
  2114. 1:15:37to be using
  2115. 1:15:38a for loop for the outer loop and a for
  2116. 1:15:41loop for the inner loop
  2117. 1:15:43so into i equals zero
  2118. 1:15:47we will continue this as long as i is
  2119. 1:15:50less than
  2120. 1:15:51our rows because we're letting the user
  2121. 1:15:53choose how many rows
  2122. 1:15:55then we will increment i by 1 during
  2123. 1:15:58each iteration
  2124. 1:15:59now we need an inner for loop and let's
  2125. 1:16:01just copy this
  2126. 1:16:03now one naming convention with the index
  2127. 1:16:06of the inner for loop since i is already
  2128. 1:16:08taken
  2129. 1:16:09what people usually use for an index for
  2130. 1:16:11the inner loop is
  2131. 1:16:12j because j comes after i i guess
  2132. 1:16:15so into j equals zero j
  2133. 1:16:18is less than columns than
  2134. 1:16:22j plus plus okay now what we'll do
  2135. 1:16:25within
  2136. 1:16:25the inner for loop is display our symbol
  2137. 1:16:29so console dot right make sure you use
  2138. 1:16:32right and not right line we will display
  2139. 1:16:35our symbol
  2140. 1:16:37so this inner loop is in charge of the
  2141. 1:16:39columns
  2142. 1:16:40the outer loop is in charge of the rows
  2143. 1:16:42basically how this is going to work
  2144. 1:16:44is that once we enter our outer for loop
  2145. 1:16:47we will immediately enter our inner for
  2146. 1:16:49loop in order to complete
  2147. 1:16:51one iteration of the outer for loop we
  2148. 1:16:54have to finish
  2149. 1:16:55all iterations of the inner for loop and
  2150. 1:16:58once we finish
  2151. 1:16:59all iterations of the inner for loop we
  2152. 1:17:01can complete
  2153. 1:17:02one iteration of the outer for loop and
  2154. 1:17:05during the next iteration
  2155. 1:17:06we have to do it all over again so on
  2156. 1:17:08the next iteration we would have to
  2157. 1:17:10complete
  2158. 1:17:10all the iterations for the inner for
  2159. 1:17:12loop so
  2160. 1:17:14this will display a rectangle made out
  2161. 1:17:16of a certain symbol
  2162. 1:17:18however there is one thing missing and
  2163. 1:17:19i'll show you how many rows
  2164. 1:17:21let's say four how many columns five and
  2165. 1:17:24what symbol
  2166. 1:17:25uh what about the i don't know that sign
  2167. 1:17:29all right so this ended up in one long
  2168. 1:17:32row so after we complete our iterations
  2169. 1:17:35for the inner for loop we should
  2170. 1:17:36probably move down to the next row
  2171. 1:17:38kind of like a typewriter we finish one
  2172. 1:17:40line then we move down to the next row
  2173. 1:17:42so let's use an empty rightline
  2174. 1:17:44statement just to move down to the next
  2175. 1:17:45line
  2176. 1:17:46so if we try this again okay four rows
  2177. 1:17:50five columns let's use an at sign there
  2178. 1:17:53we go there's a rectangle we have
  2179. 1:17:55one two three four five columns
  2180. 1:17:58and one two three four four rows
  2181. 1:18:01so basically a nested loop is really
  2182. 1:18:03just a loop that's inside of another
  2183. 1:18:05loop
  2184. 1:18:06when you'll encounter it it really
  2185. 1:18:07varies but they're used a lot in sorting
  2186. 1:18:09algorithms
  2187. 1:18:10and this was a small program just to
  2188. 1:18:13practice
  2189. 1:18:13working with nested loops alright so if
  2190. 1:18:16you want a copy of this code i will post
  2191. 1:18:18this to the comment section down below
  2192. 1:18:19if you liked this video give it a thumbs
  2193. 1:18:21up drop a random comment down below
  2194. 1:18:23and that is an example of nested loops
  2195. 1:18:26in c
  2196. 1:18:26sharp hey everyone it's you bro hope
  2197. 1:18:30you're doing well
  2198. 1:18:30and in this video we're going to create
  2199. 1:18:32a number guessing game
  2200. 1:18:33in c sharp so sit back relax and enjoy
  2201. 1:18:37the show
  2202. 1:18:40all right let's create a number guessing
  2203. 1:18:42game we will randomly generate some
  2204. 1:18:44random number between
  2205. 1:18:45a set minimum and maximum and we will
  2206. 1:18:48let the user guess as to what the number
  2207. 1:18:50is
  2208. 1:18:50if the number is too high or too low we
  2209. 1:18:52will give a hint
  2210. 1:18:53and the user can keep on guessing and
  2211. 1:18:55we'll keep track of how many attempts it
  2212. 1:18:57takes
  2213. 1:18:58so we'll need a random object to
  2214. 1:19:00generate some random numbers
  2215. 1:19:02random random equals new random and
  2216. 1:19:04there's a few other things we'll declare
  2217. 1:19:06as well at the top
  2218. 1:19:07boolean play again
  2219. 1:19:11so if the user would like to play again
  2220. 1:19:13we can keep this as true
  2221. 1:19:15if they would like to quit we can set
  2222. 1:19:16this to be false
  2223. 1:19:18int min this will be the minimum value
  2224. 1:19:21let's have the user guess a number
  2225. 1:19:23between 1 and 100
  2226. 1:19:26int max but you can feel free to adjust
  2227. 1:19:29these
  2228. 1:19:30max equals 100 and
  2229. 1:19:33guess i will declare this but not yet
  2230. 1:19:36assign a value
  2231. 1:19:38and number
  2232. 1:19:41and int guesses
  2233. 1:19:44okay we'll create a while loop and we
  2234. 1:19:46will let the user decide if they would
  2235. 1:19:48like to play again
  2236. 1:19:50so our condition while play again
  2237. 1:19:53equals true but since this itself is a
  2238. 1:19:57boolean variable you don't technically
  2239. 1:19:58need
  2240. 1:19:59this equals true portion you could just
  2241. 1:20:01say while play again
  2242. 1:20:03because this by itself would evaluate to
  2243. 1:20:05be true or false
  2244. 1:20:07now if our user decides they would like
  2245. 1:20:09to play again we should probably reset
  2246. 1:20:11everything
  2247. 1:20:12so let's take our guess and set the
  2248. 1:20:15sequel to zero
  2249. 1:20:17guesses this is the number of attempts
  2250. 1:20:20that equals zero as well and let's
  2251. 1:20:23generate a
  2252. 1:20:24new random number number equals random
  2253. 1:20:27dot next and within the parentheses of
  2254. 1:20:31the next method we can list
  2255. 1:20:33our minimum and maximum but remember
  2256. 1:20:35that this number is
  2257. 1:20:37exclusive but since we have these
  2258. 1:20:38variables we can place these here
  2259. 1:20:40instead
  2260. 1:20:41so min and max so this should be max
  2261. 1:20:45plus one
  2262. 1:20:46this will generate a random number
  2263. 1:20:47between one and one hundred
  2264. 1:20:49because like i said this number is
  2265. 1:20:51exclusive
  2266. 1:20:52okay then we will keep on asking the
  2267. 1:20:55user to type in a number like guess
  2268. 1:20:57again
  2269. 1:20:58so we'll need a nested while loop while
  2270. 1:21:01and our condition is guess
  2271. 1:21:05does not equal our number that is
  2272. 1:21:08randomly generated
  2273. 1:21:10so let's ask the user to type in a
  2274. 1:21:12number
  2275. 1:21:16guess a number
  2276. 1:21:19between
  2277. 1:21:22and this next part is going to get a
  2278. 1:21:23little funky plus
  2279. 1:21:26min plus i'll add a dash
  2280. 1:21:31plus max
  2281. 1:21:34plus a colon
  2282. 1:21:38okay then we will take variable guess
  2283. 1:21:42equals and we'll need to convert some
  2284. 1:21:44user input convert
  2285. 1:21:46to int 32 and within the parentheses
  2286. 1:21:52console dot read line
  2287. 1:21:58all right then we will display whatever
  2288. 1:22:01our guess is
  2289. 1:22:02console.writeline
  2290. 1:22:05guess colon space plus
  2291. 1:22:09guess and now we need to check our guess
  2292. 1:22:12versus the number and we can use
  2293. 1:22:16an if else if statement
  2294. 1:22:19if and the condition is
  2295. 1:22:23guess is greater than number
  2296. 1:22:26we will display a hint console that
  2297. 1:22:29right line
  2298. 1:22:32let's say that guess
  2299. 1:22:36plus is too
  2300. 1:22:39high
  2301. 1:22:43elsif
  2302. 1:22:46and the condition is guess
  2303. 1:22:49is less than number
  2304. 1:22:53let's copy this guess is
  2305. 1:22:56too low and then
  2306. 1:23:00if guess equals number we escape the
  2307. 1:23:02while loop
  2308. 1:23:03oh and we do need to increment our
  2309. 1:23:05guesses by one
  2310. 1:23:07after each guess so let's place that
  2311. 1:23:10right about here right before we
  2312. 1:23:14do another iteration of our while loop
  2313. 1:23:16guesses
  2314. 1:23:17plus plus to increment our guesses by
  2315. 1:23:20one
  2316. 1:23:21now when we exit the while loop that
  2317. 1:23:23means that we eventually got the right
  2318. 1:23:25number
  2319. 1:23:26so we will display the number with a
  2320. 1:23:28rightline statement
  2321. 1:23:30console.writeline number
  2322. 1:23:33colon space plus
  2323. 1:23:36number then we will display
  2324. 1:23:40you win console.writeline
  2325. 1:23:43you win
  2326. 1:23:46and let's display how many guesses it
  2327. 1:23:49took how many attempts
  2328. 1:23:51guesses call in space plus
  2329. 1:23:54guesses then we should probably
  2330. 1:23:58let the user decide if they would like
  2331. 1:23:59to play again and actually let's declare
  2332. 1:24:02one more variable string response
  2333. 1:24:06and then we will assign this within the
  2334. 1:24:09while loop
  2335. 1:24:10let's do that here we'll reset it
  2336. 1:24:14response equals an empty string
  2337. 1:24:20okay then we'll have the user type in if
  2338. 1:24:24they would like to play again
  2339. 1:24:26would you like to play
  2340. 1:24:30again y slash n
  2341. 1:24:33so they would have to type in either y
  2342. 1:24:35or n y to play again
  2343. 1:24:37for yes and for no
  2344. 1:24:40response equals
  2345. 1:24:44console dot read
  2346. 1:24:47line
  2347. 1:24:51and let's make this uppercase in case
  2348. 1:24:53they type in something that's lowercase
  2349. 1:24:55response equals response dot to
  2350. 1:24:59upper
  2351. 1:25:03so if our response
  2352. 1:25:08is equal to a capital y
  2353. 1:25:12play again equals true
  2354. 1:25:18else play again
  2355. 1:25:21equals false then when we
  2356. 1:25:25escape our outer while loop that means
  2357. 1:25:27we are done playing this game
  2358. 1:25:29so let's find where that ends right here
  2359. 1:25:33okay let's say thanks for playing oh
  2360. 1:25:36looks like i typed that already
  2361. 1:25:37so just type this in i guess all right
  2362. 1:25:42we're good so let's run this
  2363. 1:25:46guess a number between 1 and 100 what
  2364. 1:25:48about 50 that's right in the middle
  2365. 1:25:50right
  2366. 1:25:5050 is too high what about 25 25
  2367. 1:25:54is too high what about 12 12 is too high
  2368. 1:25:57this is a very low number i guess
  2369. 1:25:59what about six six is too high g's all
  2370. 1:26:02right
  2371. 1:26:02three three is too low our number is
  2372. 1:26:05between three
  2373. 1:26:06and six how about four
  2374. 1:26:09four is too low five all right finally
  2375. 1:26:13so it took seven guesses let's play
  2376. 1:26:15again i think i can do better
  2377. 1:26:17guess a number between one and one
  2378. 1:26:19hundred let's go with 50 again
  2379. 1:26:2150 is too low so something between 50
  2380. 1:26:24and 100 let's go with 75
  2381. 1:26:2675 is too high uh what about
  2382. 1:26:3163
  2383. 1:26:3363 is too high uh 57
  2384. 1:26:3857 is too low how about 60.
  2385. 1:26:42all right 60. that took five guesses
  2386. 1:26:45this time
  2387. 1:26:46would you like to play again no thanks
  2388. 1:26:48for playing
  2389. 1:26:49well everybody that was a very simple
  2390. 1:26:52number guessing game in csharp i'll post
  2391. 1:26:54all of this code to the comment section
  2392. 1:26:56down below if you would like a copy for
  2393. 1:26:58yourself
  2394. 1:26:59if you can give this video a thumbs up
  2395. 1:27:01feel free to drop a random comment down
  2396. 1:27:02below
  2397. 1:27:03and well that is a very simple number
  2398. 1:27:06guessing game
  2399. 1:27:06in c sharp hey what's going on everybody
  2400. 1:27:09it's you bro hope you're doing well
  2401. 1:27:11and in this video we're going to create
  2402. 1:27:12a game of rock paper scissors
  2403. 1:27:14in c-sharp so sit back relax and enjoy
  2404. 1:27:17the show
  2405. 1:27:20all right everybody let's create a game
  2406. 1:27:22of rock paper scissors what i like to do
  2407. 1:27:24is declare all the things that we'll
  2408. 1:27:26need at the top of our program
  2409. 1:27:28we'll need a random object to generate a
  2410. 1:27:30random number for the computer's choice
  2411. 1:27:32so we'll say that one equals rock two
  2412. 1:27:35equals paper
  2413. 1:27:36and three equal scissors so we'll need
  2414. 1:27:38to create a random object
  2415. 1:27:40random random equals new random and
  2416. 1:27:42we'll create a
  2417. 1:27:43boolean variable named play again and i
  2418. 1:27:47will set this
  2419. 1:27:48to equal true so if the player decides
  2420. 1:27:51to
  2421. 1:27:51exit they would not like to play another
  2422. 1:27:53game we can set this to be false
  2423. 1:27:55and we'll need a string variable to hold
  2424. 1:27:58the player's choice
  2425. 1:27:59i will declare this but not yet assign
  2426. 1:28:01it so this will store
  2427. 1:28:02rock paper or scissors and we'll need a
  2428. 1:28:05string variable for the computer's
  2429. 1:28:07choice as well
  2430. 1:28:08okay that's all of the different
  2431. 1:28:10variables that we'll need so let's
  2432. 1:28:12create a while loop
  2433. 1:28:13while play again equals
  2434. 1:28:17true now if we're working with boolean
  2435. 1:28:19variables we technically don't need
  2436. 1:28:21this equals true portion because this by
  2437. 1:28:24itself would evaluate to be true or
  2438. 1:28:26false
  2439. 1:28:27so you can just say while play again and
  2440. 1:28:29that would work as well
  2441. 1:28:30and then if the player decides to quit
  2442. 1:28:32we can change that to false
  2443. 1:28:33okay now let's ask for some user input
  2444. 1:28:36console
  2445. 1:28:36without right enter
  2446. 1:28:40rock paper
  2447. 1:28:43or scissors
  2448. 1:28:47and we will store the user's input
  2449. 1:28:49within string
  2450. 1:28:51variable player player equals console.rt
  2451. 1:28:56line now strings are case sensitive what
  2452. 1:29:00i'm going to do is take the player's
  2453. 1:29:02input and make it all uppercase just so
  2454. 1:29:04that everything is consistent
  2455. 1:29:06player equals player dot
  2456. 1:29:09two upper i guess you could make this
  2457. 1:29:12all lowercase two
  2458. 1:29:15okay now let's display the player's
  2459. 1:29:17choice with a right line statement
  2460. 1:29:20so what if the player decides to choose
  2461. 1:29:22something that isn't rock paper or
  2462. 1:29:23scissors
  2463. 1:29:24we would want some way to enforce that
  2464. 1:29:31so let's say that i choose the gun and
  2465. 1:29:34that's a valid response so we should
  2466. 1:29:36probably prevent the player from doing
  2467. 1:29:37so
  2468. 1:29:38so what we'll do is use a nested while
  2469. 1:29:41loop
  2470. 1:29:42so while then surround this section of
  2471. 1:29:44code within
  2472. 1:29:45a set of curly braces and we will
  2473. 1:29:47continue to prompt the user to type in
  2474. 1:29:49something that is valid a valid choice
  2475. 1:29:52so this is our condition while player
  2476. 1:29:55does not equal rock
  2477. 1:29:59and i'm just going to copy this
  2478. 1:30:03player does not equal paper
  2479. 1:30:07and player
  2480. 1:30:10does not equal scissors
  2481. 1:30:14so we'll need to assign player and
  2482. 1:30:16computer a value
  2483. 1:30:17let's do that at the top of our while
  2484. 1:30:19loop for play again
  2485. 1:30:21so player equals an empty string
  2486. 1:30:24and computer equals an empty string so
  2487. 1:30:28this is kind of nice when we do play
  2488. 1:30:29another round we can reset player and
  2489. 1:30:31computer
  2490. 1:30:33okay let's try this
  2491. 1:30:38enter rock paper or scissors i pick the
  2492. 1:30:41gun
  2493. 1:30:42enter rock paper or scissors how about a
  2494. 1:30:44bomb
  2495. 1:30:45fist nope okay so we have to choose
  2496. 1:30:48either rock paper or scissors
  2497. 1:30:49what about rock cool so that is a valid
  2498. 1:30:52response
  2499. 1:30:54okay so let's get rid of this right line
  2500. 1:30:55statement we won't really need it
  2501. 1:30:57anymore
  2502. 1:30:58now we'll need to generate a random
  2503. 1:30:59number for the computer player
  2504. 1:31:01one two or three and i will use a switch
  2505. 1:31:06so we will place the random number that
  2506. 1:31:09is generated within the parenthesis
  2507. 1:31:11we could say like i don't know
  2508. 1:31:14int random num
  2509. 1:31:18equals random dot
  2510. 1:31:21next and the range is going to be one
  2511. 1:31:24through four so remember that this
  2512. 1:31:27number is exclusive
  2513. 1:31:28this will generate a random whole number
  2514. 1:31:30between one and
  2515. 1:31:31three okay then what i'm going to do is
  2516. 1:31:34technically we don't need this portion
  2517. 1:31:36like we could place random num within
  2518. 1:31:38switch
  2519. 1:31:39but what i like to do is kind of shorten
  2520. 1:31:41and condense my code so i'm going to
  2521. 1:31:43take
  2522. 1:31:43this method of random.next and place it
  2523. 1:31:46within the switch itself
  2524. 1:31:47and that would work as well so let's
  2525. 1:31:49examine some cases
  2526. 1:31:51case 1 that will be rock
  2527. 1:31:55computer equals rock
  2528. 1:31:58all uppercase then break
  2529. 1:32:02and we'll need case two in case the
  2530. 1:32:04random number is two
  2531. 1:32:05case two computer
  2532. 1:32:09equals paper
  2533. 1:32:13then break and
  2534. 1:32:16case three
  2535. 1:32:20computer equals
  2536. 1:32:24scissors
  2537. 1:32:28then break what i'm going to do after
  2538. 1:32:31our switch
  2539. 1:32:32is display the player's choice along
  2540. 1:32:34with the computer's random choice
  2541. 1:32:36so let's use a redline statement so
  2542. 1:32:38player colon space
  2543. 1:32:41plus variable player then let's create
  2544. 1:32:44another right line for the computer
  2545. 1:32:49computer plus computer
  2546. 1:32:54let's run this
  2547. 1:32:58okay i will choose rock
  2548. 1:33:02player rock computer scissors let's play
  2549. 1:33:04again
  2550. 1:33:05i will choose paper player paper
  2551. 1:33:08computer paper
  2552. 1:33:09and scissors player scissors computer
  2553. 1:33:13scissors okay now we just need to
  2554. 1:33:15examine our choices and see who wins
  2555. 1:33:18that's the next step
  2556. 1:33:19now what we'll do at this point is
  2557. 1:33:21create a switch to examine
  2558. 1:33:22the player's choice versus the
  2559. 1:33:24computer's choice and we can decide who
  2560. 1:33:26wins
  2561. 1:33:27so let's create a switch and we will
  2562. 1:33:29examine
  2563. 1:33:30the variable player against many
  2564. 1:33:33different cases
  2565. 1:33:35so the first case will be rock
  2566. 1:33:41and for now we'll just break we'll fill
  2567. 1:33:42this in a little bit later
  2568. 1:33:44case paper
  2569. 1:33:49break and case
  2570. 1:33:53scissors
  2571. 1:33:57and break all right we're going to
  2572. 1:34:00check to see what the computer picked so
  2573. 1:34:04if computer
  2574. 1:34:08is equal to rock
  2575. 1:34:12then it's a draw so let's write
  2576. 1:34:20it's a draw i think i'm missing a break
  2577. 1:34:24statement
  2578. 1:34:26all right then else if
  2579. 1:34:31computer equals paper
  2580. 1:34:34that means that we lose else if computer
  2581. 1:34:38equals paper then we will write
  2582. 1:34:44you lose
  2583. 1:34:47else technically we don't need a
  2584. 1:34:50condition because the only choice left
  2585. 1:34:52is
  2586. 1:34:53scissors for the computer
  2587. 1:34:57else you win
  2588. 1:35:00so let's copy these statements paste it
  2589. 1:35:03underneath paper
  2590. 1:35:06so if we choose paper and the computer
  2591. 1:35:09chooses rock
  2592. 1:35:10that means that we win so let's change
  2593. 1:35:12this to you
  2594. 1:35:14win if we choose paper and the computer
  2595. 1:35:18chooses paper that means
  2596. 1:35:20it's a draw
  2597. 1:35:24else you lose
  2598. 1:35:28let's copy this again paste it
  2599. 1:35:31underneath scissors
  2600. 1:35:33so if we pick scissors and the computer
  2601. 1:35:35picks rock
  2602. 1:35:36you lose
  2603. 1:35:41if we choose scissors the computer picks
  2604. 1:35:43paper then we win
  2605. 1:35:46you win else
  2606. 1:35:53it's a draw all right let's test it
  2607. 1:35:58all right enter rock paper scissors
  2608. 1:36:00let's go with rock
  2609. 1:36:02rock paper you lose let's choose paper
  2610. 1:36:05this time
  2611. 1:36:05paper scissors you lose again and let's
  2612. 1:36:08try scissors
  2613. 1:36:11scissors rock you lose man i kind of
  2614. 1:36:13suck at this game
  2615. 1:36:15let's try it again paper paper rock okay
  2616. 1:36:18i finally won one
  2617. 1:36:19cool now at this point we're going to
  2618. 1:36:22let the player decide if they would like
  2619. 1:36:23to play another round so place
  2620. 1:36:25this code at the end of our outer while
  2621. 1:36:27loop so i'm just going to follow the
  2622. 1:36:28dots here
  2623. 1:36:30so that would go right here all right so
  2624. 1:36:33let's use a write statement
  2625. 1:36:38would you like to play
  2626. 1:36:41again y slash
  2627. 1:36:45n so y for yes
  2628. 1:36:48and for no actually i think i'm going to
  2629. 1:36:51create another variable so let's do so
  2630. 1:36:53at the top
  2631. 1:36:54string answer
  2632. 1:36:59and we should probably reset this to
  2633. 1:37:03answer equals an empty string
  2634. 1:37:06okay so we will add one more variable
  2635. 1:37:10okay answer equals
  2636. 1:37:15console dot read
  2637. 1:37:18line then let's make it uppercase
  2638. 1:37:22in case the player enters in something
  2639. 1:37:24that is lowercase
  2640. 1:37:25we'll still count it answer equals
  2641. 1:37:28answer
  2642. 1:37:28dot to upper
  2643. 1:37:34then let's check to see what the player
  2644. 1:37:35enters in
  2645. 1:37:37if answer is equal to y
  2646. 1:37:43then play again
  2647. 1:37:46equals true
  2648. 1:37:50else play again
  2649. 1:37:54equals false
  2650. 1:37:59all right then outside of the while loop
  2651. 1:38:02we will display a goodbye message
  2652. 1:38:06maybe we'll say thanks for playing
  2653. 1:38:10all right let's try it one last time
  2654. 1:38:16okay enter rock paper scissors always
  2655. 1:38:19choose rock
  2656. 1:38:21player rock computer paper you lose
  2657. 1:38:23would you like to play again
  2658. 1:38:24yes let's pick rock again rock paper you
  2659. 1:38:28lose okay maybe you don't always pick
  2660. 1:38:30rock would you like to play again
  2661. 1:38:31no because this is a dumb game thanks
  2662. 1:38:33for playing
  2663. 1:38:34well okay then everybody that is a game
  2664. 1:38:37of rock paper scissors
  2665. 1:38:39if you would like a copy of this code i
  2666. 1:38:40will post this to the comment section
  2667. 1:38:42down below
  2668. 1:38:43if you can smash that like button leave
  2669. 1:38:45a random comment down below
  2670. 1:38:46and well that's a game of rock paper
  2671. 1:38:49scissors for c-sharp
  2672. 1:38:54alright people well i thought for
  2673. 1:38:56practice we could create a sort of
  2674. 1:38:57calculator program we can pick two
  2675. 1:38:59numbers
  2676. 1:39:00we can add them subtract them multiply
  2677. 1:39:02them and divide them
  2678. 1:39:03so let's get started this will be just a
  2679. 1:39:05very simple project
  2680. 1:39:07i'm going to write just calculator
  2681. 1:39:09program
  2682. 1:39:10when we begin this program and then
  2683. 1:39:13maybe
  2684. 1:39:13i don't know format it a little bit
  2685. 1:39:15let's add some
  2686. 1:39:17i don't know dashes or something just to
  2687. 1:39:18kind of make this look nice
  2688. 1:39:20this will all be within the console okay
  2689. 1:39:23so we'll need to declare
  2690. 1:39:24num1 num2 and result these will be
  2691. 1:39:27doubles double num1 i'll set this equal
  2692. 1:39:31to
  2693. 1:39:31zero let's create num2
  2694. 1:39:36and result double result
  2695. 1:39:40and i will set that equal to zero as
  2696. 1:39:42well
  2697. 1:39:43okay we will need to ask for some user
  2698. 1:39:45input
  2699. 1:39:47console the right line enter
  2700. 1:39:50number one colon space and
  2701. 1:39:54then we will assign num1 equals
  2702. 1:39:58so user input's always a string we'll
  2703. 1:39:59need to convert this to
  2704. 1:40:01a double convert dot to double
  2705. 1:40:07and within the parenthesis we need
  2706. 1:40:10console.read
  2707. 1:40:12line method okay let's do the same thing
  2708. 1:40:15for num2
  2709. 1:40:17enter number two num2
  2710. 1:40:21equals convert.2double pass in
  2711. 1:40:24console.readline now we need the user to
  2712. 1:40:27type in an option do they want to add a
  2713. 1:40:29subtract multiply or divide
  2714. 1:40:33so let's type enter and option
  2715. 1:40:41console.writeline again what about
  2716. 1:40:44plus colon space add
  2717. 1:40:50then minus colon space
  2718. 1:40:54subtract
  2719. 1:40:57asterisk for multiply
  2720. 1:41:04and forward slash for divide
  2721. 1:41:07you can add other options if you want to
  2722. 1:41:10okay let's take a look at this so far
  2723. 1:41:12so we have calculator program enter
  2724. 1:41:15number one what about 3.14
  2725. 1:41:18enter number two uh let's say
  2726. 1:41:22i don't know five okay a couple things
  2727. 1:41:25i'm going to change real quick i'm going
  2728. 1:41:26to change
  2729. 1:41:27this right line to just right
  2730. 1:41:30then i'm going to add a tab escape
  2731. 1:41:32sequence
  2732. 1:41:34that is backslash t for tab
  2733. 1:41:38just so that it looks better
  2734. 1:41:43okay let's continue so i'm going to
  2735. 1:41:46create a switch that will read our user
  2736. 1:41:48input
  2737. 1:41:49switch console.read
  2738. 1:41:54line and we need
  2739. 1:41:57matching cases case
  2740. 1:42:01plus so if somebody would like to add
  2741. 1:42:04we will take our result equals
  2742. 1:42:07num1 plus num 2.
  2743. 1:42:13console.writeline and i'll do some
  2744. 1:42:16string interpolation
  2745. 1:42:19your result colon space
  2746. 1:42:25num1 plus
  2747. 1:42:28num2 equals
  2748. 1:42:33plus result and then we need to break
  2749. 1:42:37out of this case
  2750. 1:42:39okay let's do the same thing with minus
  2751. 1:42:43case minus result equals
  2752. 1:42:47num1 minus num2
  2753. 1:42:50num1 minus num2
  2754. 1:42:53and that's all we need then we need
  2755. 1:42:56multiply
  2756. 1:42:58case asterisk result
  2757. 1:43:01equals num1 times num2
  2758. 1:43:06replace minus with asterisk
  2759. 1:43:09and then divide
  2760. 1:43:14case forward slash for division result
  2761. 1:43:17equals num1 divided by num2
  2762. 1:43:21and replace the asterisk here
  2763. 1:43:25and then we should probably add a
  2764. 1:43:26default case just in case somebody
  2765. 1:43:28doesn't pick one of these options
  2766. 1:43:30default
  2767. 1:43:34console.writeline that
  2768. 1:43:37was not a valid
  2769. 1:43:40option and then break
  2770. 1:43:45enter number one 3.14
  2771. 1:43:48enter number two uh let's say 6.9
  2772. 1:43:53and i would like to multiply these so
  2773. 1:43:56i will add an asterisk
  2774. 1:43:593.14 times 6.9 equals 21.666
  2775. 1:44:03all right i'm just going to change one
  2776. 1:44:05thing i'm going to add
  2777. 1:44:07one line right before we read our user's
  2778. 1:44:10input for
  2779. 1:44:10what operation they would like to use
  2780. 1:44:14so let's add console dot
  2781. 1:44:17right now right line enter
  2782. 1:44:21an option
  2783. 1:44:24then let's try it again 3.14
  2784. 1:44:29multiplied by 6.9 is
  2785. 1:44:3421.666 okay now what if somebody would
  2786. 1:44:37like to continue using this calculator
  2787. 1:44:39so what we'll do is place all of this
  2788. 1:44:41code within a do while loop
  2789. 1:44:43i'm not sure if i discussed this in the
  2790. 1:44:45past but a do while loop will always
  2791. 1:44:47execute a body of code once
  2792. 1:44:49and then it checks the condition at the
  2793. 1:44:51end
  2794. 1:44:53so let's add that here so we have
  2795. 1:44:56do at the top then we need our while
  2796. 1:44:59with condition
  2797. 1:45:02while all right now before this
  2798. 1:45:06let's ask for some user input
  2799. 1:45:09console.writeline
  2800. 1:45:13would you like to continue
  2801. 1:45:21capital y equals yes
  2802. 1:45:24capital n equals no
  2803. 1:45:30and then we will read some user input so
  2804. 1:45:32within
  2805. 1:45:33the while loop the condition this will
  2806. 1:45:35be
  2807. 1:45:38console.readline
  2808. 1:45:41dot 2 upper
  2809. 1:45:44so in case they type in lowercase y or n
  2810. 1:45:47we'll just change it to uppercase via
  2811. 1:45:50the two upper method
  2812. 1:45:52and let's check to see if this is equal
  2813. 1:45:54to y
  2814. 1:45:55if it is then we'll reset our num1 num2
  2815. 1:45:59and result variables and do this all
  2816. 1:46:00over again
  2817. 1:46:02and if they would not like to continue
  2818. 1:46:04let's write
  2819. 1:46:06buy okay let's try this again
  2820. 1:46:10what is point one four divided by two so
  2821. 1:46:13enter number one three point one four
  2822. 1:46:14enter number two two and let's divide
  2823. 1:46:17these
  2824. 1:46:18the result is one point five seven let's
  2825. 1:46:21type
  2826. 1:46:22y for yes all right then we can do this
  2827. 1:46:24all over again
  2828. 1:46:25what is 5 divided by 3.14
  2829. 1:46:30so i will divide and the result is
  2830. 1:46:331.59 something something something would
  2831. 1:46:36you like to continue i'm going to type
  2832. 1:46:38and for no
  2833. 1:46:39bye so yeah that's a very simple
  2834. 1:46:42calculator program in c sharp i will
  2835. 1:46:45post this code in the comment section
  2836. 1:46:46down below
  2837. 1:46:47if you can give this video a thumbs up
  2838. 1:46:48leave a random comment down below
  2839. 1:46:50and subscribe if you'd like to become a
  2840. 1:46:52fellow bro
  2841. 1:46:53hey what's going on everybody it's you
  2842. 1:46:55bro hope you're doing well
  2843. 1:46:56and in this video i'm going to explain a
  2844. 1:46:58raise in c sharp so
  2845. 1:47:00sit back relax and enjoy the show
  2846. 1:47:04all right everybody so arrays think of
  2847. 1:47:07an array
  2848. 1:47:08as a variable but it can store multiple
  2849. 1:47:11values not just one so i have the string
  2850. 1:47:14variable named car and it contains the
  2851. 1:47:16name of a car bmw
  2852. 1:47:18so what we could do is turn this
  2853. 1:47:20variable into an array
  2854. 1:47:21so it can store multiple values and this
  2855. 1:47:24is all you have to do
  2856. 1:47:25after the data type add a set of square
  2857. 1:47:27brackets
  2858. 1:47:28and then surround all of the values with
  2859. 1:47:30a set of curly braces
  2860. 1:47:32boom you got yourself an array and
  2861. 1:47:34that's all there is to it so
  2862. 1:47:36you can add multiple values to this
  2863. 1:47:38array named car
  2864. 1:47:39and actually i'm going to rename this as
  2865. 1:47:41cars so it's plural so that we know that
  2866. 1:47:43there's multiple values within here
  2867. 1:47:45so let's separate each value with a
  2868. 1:47:48comma
  2869. 1:47:49so let's say we have a bmw a mustang
  2870. 1:47:53as well as a corvette
  2871. 1:47:56okay then let's display what cars is
  2872. 1:47:58with a right line statement
  2873. 1:48:00console the right line cars
  2874. 1:48:05so this is what appears we have the data
  2875. 1:48:07type of our array instead of the values
  2876. 1:48:09within the array
  2877. 1:48:11so if you need to access an element that
  2878. 1:48:14is an item from an array
  2879. 1:48:15you need to add after the variable name
  2880. 1:48:17well the array name
  2881. 1:48:19a set of square brackets and then the
  2882. 1:48:21index
  2883. 1:48:22so computers they always start with zero
  2884. 1:48:24the first element would be
  2885. 1:48:25zero so place that number within the
  2886. 1:48:27square brackets
  2887. 1:48:29cars at index zero and the first element
  2888. 1:48:32would be bmw so let's display the other
  2889. 1:48:36elements
  2890. 1:48:37let's just copy what we have okay let's
  2891. 1:48:40display
  2892. 1:48:40cars at index 1 that will be this value
  2893. 1:48:44because
  2894. 1:48:44this is index zero
  2895. 1:48:49and that is mustang
  2896. 1:48:52then the next value cars at index
  2897. 1:48:55two
  2898. 1:48:59and that is corvette so let's take a
  2899. 1:49:02look to see what happens if i attempt to
  2900. 1:49:03access an element that doesn't exist
  2901. 1:49:06so cars at index three so that would be
  2902. 1:49:10something beyond corvette
  2903. 1:49:16so we'll actually run into an error we
  2904. 1:49:19ran into
  2905. 1:49:20an index out of range exception so
  2906. 1:49:22arrays do have a
  2907. 1:49:24fixed size you have to make sure that
  2908. 1:49:25you're not accessing an element
  2909. 1:49:27that doesn't exist now if you need to
  2910. 1:49:29update an element within an array all
  2911. 1:49:31you have to do is type
  2912. 1:49:33the array name in this case it would be
  2913. 1:49:35cars straight brackets and
  2914. 1:49:37list the index so if i need to change
  2915. 1:49:39the first element within my array
  2916. 1:49:42i will say my arrand name cars at index
  2917. 1:49:44zero
  2918. 1:49:45equals let's place a tesla there instead
  2919. 1:49:49and then run this
  2920. 1:49:52so instead of a bmw in the first
  2921. 1:49:55position
  2922. 1:49:56index zero we have a tesla so if you
  2923. 1:49:58need to access or update an element
  2924. 1:50:01within an array
  2925. 1:50:02type the array name straight brackets
  2926. 1:50:03and then the index
  2927. 1:50:05and remember that these always start
  2928. 1:50:06with zero so what we've done here
  2929. 1:50:08to display the elements of an array is
  2930. 1:50:10that we wrote a writeline statement for
  2931. 1:50:13each individual
  2932. 1:50:14element so a much better approach would
  2933. 1:50:16be to iterate over
  2934. 1:50:18our array using a for loop and later on
  2935. 1:50:21i'll explain the for
  2936. 1:50:22each loop so to display all of the
  2937. 1:50:24elements of our array easily
  2938. 1:50:26let's create a for loop so we'll say
  2939. 1:50:29into i
  2940. 1:50:30equals zero will continue this as long
  2941. 1:50:32as i
  2942. 1:50:33is less than now arrays have a length
  2943. 1:50:35property so type the name of the array
  2944. 1:50:38dot link then i
  2945. 1:50:41plus plus to increment our index okay
  2946. 1:50:44with our writeline statement
  2947. 1:50:46let's display cars straight brackets
  2948. 1:50:50and the index is going to be i so i
  2949. 1:50:54is zero for the first iteration then
  2950. 1:50:56during the next iteration it will be one
  2951. 1:50:58then two and then it will stop because
  2952. 1:51:01this statement will no longer be true
  2953. 1:51:03okay let's try this again
  2954. 1:51:06so we have tesla mustang and corvette so
  2955. 1:51:09an easy way to display the elements of
  2956. 1:51:11an array
  2957. 1:51:12is to write a for loop and set the index
  2958. 1:51:15equal to i
  2959. 1:51:16whatever you have right here but we'll
  2960. 1:51:18have to discuss the
  2961. 1:51:20for each loop coming up in a later video
  2962. 1:51:22one thing that you should know with
  2963. 1:51:23arrays
  2964. 1:51:24is that they have a fixed size if you
  2965. 1:51:26would like to declare an array and then
  2966. 1:51:28assign values later
  2967. 1:51:29you'll need to declare that array with a
  2968. 1:51:31fixed size of how many elements you're
  2969. 1:51:33planning to place within that array
  2970. 1:51:35so that would look something like this
  2971. 1:51:37let's turn this line into a comment
  2972. 1:51:39and this is how to declare an array so
  2973. 1:51:42type the
  2974. 1:51:43data type square brackets the name of
  2975. 1:51:46the array
  2976. 1:51:46equals new the data type again square
  2977. 1:51:49brackets
  2978. 1:51:50and then within this set of square
  2979. 1:51:52brackets we will list a size
  2980. 1:51:54so if i plan on adding no more than
  2981. 1:51:57three elements to this array
  2982. 1:51:58i can place three here but you can make
  2983. 1:52:00this a larger size to accommodate more
  2984. 1:52:02values
  2985. 1:52:03and if you don't use those values
  2986. 1:52:05they'll just remain empty no big deal
  2987. 1:52:07and this will work the same as before
  2988. 1:52:09and then we'll just have to update some
  2989. 1:52:11values
  2990. 1:52:12so cars at index 0 1 and
  2991. 1:52:15zero is a tesla one is a mustang
  2992. 1:52:18and two is a corvette and this will
  2993. 1:52:21pretty much work the same as before
  2994. 1:52:23instead of just in one step declaring
  2995. 1:52:25and assigning an array
  2996. 1:52:27we have done it in two steps we declared
  2997. 1:52:29the array and then assigned some values
  2998. 1:52:31later
  2999. 1:52:32well okay then everybody that is an
  3000. 1:52:34array it's a variable that can store
  3001. 1:52:36multiple values and they have a fixed
  3002. 1:52:39size so that's something you need to pay
  3003. 1:52:41attention to
  3004. 1:52:42if you would like a copy of this code i
  3005. 1:52:44will post this to the comment section
  3006. 1:52:45down below
  3007. 1:52:46and well those are arrays in c sharp
  3008. 1:52:50oh yeah all right everybody
  3009. 1:52:53for each loops a for each loop is a
  3010. 1:52:56simpler
  3011. 1:52:57and more elegant way to iterate over an
  3012. 1:52:59array
  3013. 1:53:00however it's less flexible compared to a
  3014. 1:53:03standard for loop
  3015. 1:53:04i have an array of names of vehicles
  3016. 1:53:06named cars
  3017. 1:53:07we have a bmw mustang and corvette then
  3018. 1:53:10we can use
  3019. 1:53:11a standard for loop to iterate and
  3020. 1:53:13display the elements of this array
  3021. 1:53:16so this works fine however it's a lot of
  3022. 1:53:18work to write all of this
  3023. 1:53:19another approach although it's less
  3024. 1:53:21flexible is to create a
  3025. 1:53:23for each loop this will iterate once for
  3026. 1:53:25each element within my array
  3027. 1:53:27so type for each parenthesis curly
  3028. 1:53:30braces
  3029. 1:53:30and within the parentheses we list the
  3030. 1:53:32data type of each element
  3031. 1:53:34then we need a name for each element
  3032. 1:53:36that we're working with
  3033. 1:53:37since we're working with cars let's call
  3034. 1:53:39each element a car
  3035. 1:53:41string car in the name of the array
  3036. 1:53:45cars string car in cars then i will with
  3037. 1:53:48the right line statement
  3038. 1:53:49display whatever car is so this is kind
  3039. 1:53:52of like a nickname for the element that
  3040. 1:53:54we're currently working with
  3041. 1:53:56so let's get rid of this for loop
  3042. 1:53:57because we don't need it anymore
  3043. 1:53:59and use our for each loop instead
  3044. 1:54:03and this works the same as before one
  3045. 1:54:06downside with a for each loop is that
  3046. 1:54:08it's not flexible with a standard for
  3047. 1:54:10loop we can iterate forwards
  3048. 1:54:12backwards or even skip iterations so use
  3049. 1:54:15whatever loop is more appropriate for
  3050. 1:54:17your situation
  3051. 1:54:18well everybody that is a for each loop
  3052. 1:54:21it's a
  3053. 1:54:22simpler and more elegant way to iterate
  3054. 1:54:24over an array
  3055. 1:54:25however it's not as flexible if you just
  3056. 1:54:27need something simple to display the
  3057. 1:54:29elements of an array
  3058. 1:54:30you can easily use a for each loop all
  3059. 1:54:33right everybody so that's a for each
  3060. 1:54:34loop if you can give this video
  3061. 1:54:36a thumbs up drop a random comment down
  3062. 1:54:37below and well those are four each loops
  3063. 1:54:40in c-sharp
  3064. 1:54:44all right people methods a method
  3065. 1:54:46performs a
  3066. 1:54:47section of code whenever it's called
  3067. 1:54:49also known as
  3068. 1:54:51invoking a method and the benefit of
  3069. 1:54:53using a method
  3070. 1:54:54is that it lets us reuse code without
  3071. 1:54:56writing it
  3072. 1:54:57multiple times here's a scenario let's
  3073. 1:54:59say that we have to write a program
  3074. 1:55:01where we need to
  3075. 1:55:02sing happy birthday to our friend three
  3076. 1:55:04times because we would like to annoy him
  3077. 1:55:06one way in which we could do that
  3078. 1:55:08without writing a method is that we
  3079. 1:55:10would need to display as
  3080. 1:55:11output you know the lyrics to happy
  3081. 1:55:13birthday i just made my own version of
  3082. 1:55:15it so
  3083. 1:55:15happy birthday to you happy birthday to
  3084. 1:55:17you happy birthday dear you
  3085. 1:55:20happy birthday to you if i need to sing
  3086. 1:55:22happy birthday three times
  3087. 1:55:24i would need to copy and paste this code
  3088. 1:55:27two additional times
  3089. 1:55:28and that would allow me to effectively
  3090. 1:55:30sing happy birthday three
  3091. 1:55:31times so after running this we have all
  3092. 1:55:34of our text
  3093. 1:55:35repeated three times now a better way of
  3094. 1:55:38writing this would be to use a method
  3095. 1:55:40where we only have to write this once
  3096. 1:55:41and then we can keep on reusing it
  3097. 1:55:43yeah let's delete all this outside of
  3098. 1:55:45our main method
  3099. 1:55:46that will end right here we need to
  3100. 1:55:48create a new method and the way we do
  3101. 1:55:50that
  3102. 1:55:50is that for this specific case we have
  3103. 1:55:52to write static because we're invoking
  3104. 1:55:55a method from our main method which is
  3105. 1:55:57static so we need static but not always
  3106. 1:56:00void and then a unique and descriptive
  3107. 1:56:02name of this method what does it do
  3108. 1:56:04exactly
  3109. 1:56:05so let's call this method sing happy
  3110. 1:56:09birthday then a set of parentheses then
  3111. 1:56:11a set of curly braces
  3112. 1:56:13and we have ourselves a method named
  3113. 1:56:15saying happy birthday
  3114. 1:56:16and when we call this method what kind
  3115. 1:56:18of code do we want to execute
  3116. 1:56:20so let's copy and paste the lyrics to
  3117. 1:56:23happy birthday and then place it within
  3118. 1:56:25this method now to invoke a method or
  3119. 1:56:27called method
  3120. 1:56:28we have to type the name of the method
  3121. 1:56:31followed by a set of parentheses
  3122. 1:56:32and i like to think of the parentheses
  3123. 1:56:34kind of like a pair of telephones
  3124. 1:56:36they're communicating with each other
  3125. 1:56:38that's how i think of it at least and
  3126. 1:56:39then a semicolon to end
  3127. 1:56:41the statement so after running this this
  3128. 1:56:43will execute sing happy birthday
  3129. 1:56:45once now if i need to sing happy
  3130. 1:56:48birthday two additional times
  3131. 1:56:49i can just call this method two
  3132. 1:56:51additional times sing happy birthday
  3133. 1:56:53saying happy birthday sing happy
  3134. 1:56:55birthday and i do not need all of this
  3135. 1:56:57text i only need to write it once
  3136. 1:56:58and then i can just keep on reusing it
  3137. 1:57:01and here we have
  3138. 1:57:03the lyrics to my happy birthday song
  3139. 1:57:05repeated three times
  3140. 1:57:07let's move on to level two we're going
  3141. 1:57:09to create a string
  3142. 1:57:10variable named name string name equals
  3143. 1:57:13and make up some name
  3144. 1:57:16now what if we attempt to use this
  3145. 1:57:18variable within our method
  3146. 1:57:20so replace you with name happy birthday
  3147. 1:57:24dear name okay here's the situation
  3148. 1:57:27the name name does not exist in the
  3149. 1:57:30current context
  3150. 1:57:31methods don't have access to any
  3151. 1:57:33variables within another method
  3152. 1:57:36so anything within the main method this
  3153. 1:57:38singh happy birthday method cannot see
  3154. 1:57:40so we need to pass what are known as
  3155. 1:57:42arguments over to this method in order
  3156. 1:57:44to use
  3157. 1:57:45these values or variables so when you
  3158. 1:57:48invoke a method and you need to send
  3159. 1:57:50arguments over to a method we just list
  3160. 1:57:52whatever we would like to send over to
  3161. 1:57:54this method within the parentheses
  3162. 1:57:56so i would like to pass name as an
  3163. 1:57:59argument to this method
  3164. 1:58:00however we need a matching set of
  3165. 1:58:02arguments and parameters
  3166. 1:58:04parameters are kind of like what this
  3167. 1:58:06method is expecting to receive
  3168. 1:58:08and to create some parameters we list
  3169. 1:58:11the data type of what we're expecting
  3170. 1:58:13followed by some sort of name so let's
  3171. 1:58:15just say name
  3172. 1:58:17and now we can use this variable name
  3173. 1:58:19that is declared within the main method
  3174. 1:58:21and we now have our name variable
  3175. 1:58:23displayed within our happy birthday song
  3176. 1:58:25and here's how to send more than one
  3177. 1:58:27argument to a method let's create an
  3178. 1:58:29integer variable
  3179. 1:58:30named age and we will display the user's
  3180. 1:58:32edge within our happy birthday song
  3181. 1:58:35so separate each argument with a comma
  3182. 1:58:38we will pass name as well as age to
  3183. 1:58:41our happy birthday method but remember
  3184. 1:58:44we need a matching
  3185. 1:58:45set of arguments and parameters so we
  3186. 1:58:48need to add
  3187. 1:58:48a age parameter so separate each with a
  3188. 1:58:52comma
  3189. 1:58:52then list the data type of this variable
  3190. 1:58:54first int
  3191. 1:58:56and then a name we'll just call it edge
  3192. 1:58:59and now we can use this edge variable
  3193. 1:59:01within our method
  3194. 1:59:02so let's create a line where we will
  3195. 1:59:04display the person's edge
  3196. 1:59:06so console that right line
  3197. 1:59:09u r
  3198. 1:59:13plus age plus
  3199. 1:59:17years old and let's try this again
  3200. 1:59:22happy birthday to you happy birthday to
  3201. 1:59:24you happy birthday dear bro
  3202. 1:59:26you are 21 years old happy birthday to
  3203. 1:59:29you
  3204. 1:59:29i guess my happy birthday song could use
  3205. 1:59:31some work now these parameters they
  3206. 1:59:33don't necessarily need
  3207. 1:59:34the same exact name as the arguments
  3208. 1:59:36that we pass in let's rename
  3209. 1:59:38name as birthday boy
  3210. 1:59:41and replace edge with years
  3211. 1:59:44old okay then replace any instance of
  3212. 1:59:47name with birthday boy
  3213. 1:59:49and age with years old and this will
  3214. 1:59:52work the same
  3215. 1:59:54so you can rename these parameters
  3216. 1:59:57what's important is the data type
  3217. 1:59:59and the order of the values that you
  3218. 2:00:01pass in
  3219. 2:00:02all right everybody well those are
  3220. 2:00:04methods they perform
  3221. 2:00:05a section of code whenever they are
  3222. 2:00:07called
  3223. 2:00:08also known as invoking a method and the
  3224. 2:00:11benefit of using methods
  3225. 2:00:12is that it allows us to reuse code
  3226. 2:00:15without writing it
  3227. 2:00:16multiple times we only had to write the
  3228. 2:00:19lyrics to happy birthday once
  3229. 2:00:20then if we had to sing happy birthday
  3230. 2:00:23you know more than one time
  3231. 2:00:24we can just keep on invoking this method
  3232. 2:00:26however many times that we need
  3233. 2:00:28so those are methods if you can give
  3234. 2:00:30this video a thumbs up
  3235. 2:00:32drop a random comment down below and
  3236. 2:00:34well yeah those are methods
  3237. 2:00:35in c sharp
  3238. 2:00:39oh yeah the return keyword now the
  3239. 2:00:42return keyword
  3240. 2:00:43returns data back to the place where a
  3241. 2:00:45method is invoked
  3242. 2:00:47so let's create a method to multiply two
  3243. 2:00:49numbers together
  3244. 2:00:50something simple so when we declare a
  3245. 2:00:52method we'll have to make sure that we
  3246. 2:00:53do it outside of our main method
  3247. 2:00:55so right here so we will say static
  3248. 2:00:59void and we will name this method
  3249. 2:01:02multiply and we'll need to pass
  3250. 2:01:05in two numbers as arguments let's say
  3251. 2:01:07that the data type
  3252. 2:01:09will be double double x and double
  3253. 2:01:12y okay now we would like to multiply
  3254. 2:01:15these two numbers together
  3255. 2:01:17and we will store them within double
  3256. 2:01:19variable z
  3257. 2:01:20double z equals x times y
  3258. 2:01:24now to return data back to the place
  3259. 2:01:26where it's invoked
  3260. 2:01:28we can use this return keyword and what
  3261. 2:01:30kind of data would we like to return
  3262. 2:01:32let's return whatever z is now there's
  3263. 2:01:35one change that we need to make
  3264. 2:01:37we need to replace this void keyword
  3265. 2:01:39with the data type
  3266. 2:01:41of what we're returning exactly so since
  3267. 2:01:43we're returning a double
  3268. 2:01:45we'll change void to double if you're
  3269. 2:01:47not returning anything
  3270. 2:01:48you can just keep that keyword as void
  3271. 2:01:51now we can use this multiply method
  3272. 2:01:52to well multiply two numbers together so
  3273. 2:01:55let's write a program where we will ask
  3274. 2:01:56a user for some user input
  3275. 2:01:58multiply two numbers together then
  3276. 2:02:00return the result and display it
  3277. 2:02:02so let's accept some user input and
  3278. 2:02:04first we should probably declare the
  3279. 2:02:06variables that we'll need
  3280. 2:02:07double x double y and
  3281. 2:02:11double let's call this result and we
  3282. 2:02:13will ask the user to enter
  3283. 2:02:15in number one
  3284. 2:02:19and then assign x equal and we'll need
  3285. 2:02:22to convert
  3286. 2:02:23the user's input to a double because
  3287. 2:02:26it's always a string when we accept user
  3288. 2:02:28input
  3289. 2:02:28x equals convert dot
  3290. 2:02:32to double
  3291. 2:02:36then out of side of parenthesis within
  3292. 2:02:37the parenthesis console.read
  3293. 2:02:41line and we'll do the same thing for
  3294. 2:02:44variable y enter in number two
  3295. 2:02:48and then change x to y now if we need to
  3296. 2:02:52assign
  3297. 2:02:52results a value we can use this multiply
  3298. 2:02:55method
  3299. 2:02:56so type multiply add a set of
  3300. 2:02:58parenthesis to invoke it
  3301. 2:03:00but we'll need to pass two matching
  3302. 2:03:02arguments because we have
  3303. 2:03:03two parameters set up we need to pass in
  3304. 2:03:06two
  3305. 2:03:07double values or variables so we will
  3306. 2:03:09pass
  3307. 2:03:10x and y as arguments to this method
  3308. 2:03:13multiply
  3309. 2:03:14so this return keyword will return
  3310. 2:03:17whatever z
  3311. 2:03:17is back to the spot in where this method
  3312. 2:03:20is invoked
  3313. 2:03:21and we will store the result within this
  3314. 2:03:23variable named result
  3315. 2:03:24and then we should probably display it
  3316. 2:03:27let's display
  3317. 2:03:28result and well that's it let's try it
  3318. 2:03:32enter in number one have you always
  3319. 2:03:34wondered what
  3320. 2:03:36pi times 420 is well we're about to find
  3321. 2:03:38out
  3322. 2:03:39it is 1 318.8
  3323. 2:03:42so yeah that is the return keyword it
  3324. 2:03:44returns data back to the place where a
  3325. 2:03:46method is invoked
  3326. 2:03:47and if you would like you can actually
  3327. 2:03:49shorten this even further we don't
  3328. 2:03:50necessarily need to
  3329. 2:03:52store the result within you know a new
  3330. 2:03:54variable we could just say return
  3331. 2:03:56x times y and that would work the same
  3332. 2:03:58too
  3333. 2:03:59that is the return keyword it returns
  3334. 2:04:01data back to the place where
  3335. 2:04:02a method is invoked if you're able to
  3336. 2:04:05give this video a thumbs up
  3337. 2:04:06leave a random comment down below and
  3338. 2:04:08well yeah that's the return keyword in c
  3339. 2:04:10sharp let's go bros
  3340. 2:04:14okay it looks like we're talking about
  3341. 2:04:16method overloading now methods they can
  3342. 2:04:18actually share the same name
  3343. 2:04:19but you need a different set of
  3344. 2:04:21parameters a method's name
  3345. 2:04:23plus its parameters equals a method's
  3346. 2:04:26signature
  3347. 2:04:27and methods must have a unique method
  3348. 2:04:29signature
  3349. 2:04:30let's take this method for example
  3350. 2:04:32called multiply so this returns
  3351. 2:04:34a times b so what if i would like to
  3352. 2:04:36multiply more than two numbers
  3353. 2:04:38like three numbers well this method is
  3354. 2:04:41only set up for
  3355. 2:04:42two arguments so one thing that we could
  3356. 2:04:44do is method overload
  3357. 2:04:46so we can use the same name like we can
  3358. 2:04:48reuse it but we need
  3359. 2:04:49different parameters so to solve this
  3360. 2:04:51problem i'm going to create a second
  3361. 2:04:53multiply method
  3362. 2:04:55and this will have a different set of
  3363. 2:04:56parameters giving it a different
  3364. 2:04:58method signature so let's create a
  3365. 2:05:01method that has
  3366. 2:05:02three parameters a b and c return
  3367. 2:05:06a times b times c and you can see that
  3368. 2:05:09that problem went away and this should
  3369. 2:05:10work so 2 times 3 times 4
  3370. 2:05:13equals 24 and i can just keep on
  3371. 2:05:16creating more methods
  3372. 2:05:17so basically that's method overloading
  3373. 2:05:19methods can share the same name
  3374. 2:05:21but they need different parameters if
  3375. 2:05:23they do share the same name
  3376. 2:05:25a method's name plus its parameters
  3377. 2:05:27equals the method signature
  3378. 2:05:29and methods must have a unique signature
  3379. 2:05:32so yeah that's method overloading if you
  3380. 2:05:34would like a copy of this code i'll post
  3381. 2:05:36this to the comment section down below
  3382. 2:05:37if you can smash that like button drop a
  3383. 2:05:39random comment down below and well yeah
  3384. 2:05:41that's method overloading in c
  3385. 2:05:43sharp oh yeah ladies and gentlemen
  3386. 2:05:48do you know what's better than
  3387. 2:05:49overloaded methods the params keyword
  3388. 2:05:52now a methods parameters can take a
  3389. 2:05:55variable number of arguments if we use
  3390. 2:05:57the params keyword and the parameter
  3391. 2:05:59type must be a
  3392. 2:06:01single dimensional array what's the
  3393. 2:06:03benefit of this
  3394. 2:06:04let me explain so let's say that we have
  3395. 2:06:06an online store we have a method to
  3396. 2:06:08check
  3397. 2:06:08out where we will basically sum up a
  3398. 2:06:10customer's prices
  3399. 2:06:12and return a total and we'll tell the
  3400. 2:06:14customer you have to pay this total what
  3401. 2:06:16if the person
  3402. 2:06:16checking out has a variable number of
  3403. 2:06:19items that they would like to buy
  3404. 2:06:20well we would have to set up a bunch of
  3405. 2:06:22different methods like
  3406. 2:06:24what if they only buy one item well
  3407. 2:06:25there's a checkout method
  3408. 2:06:27that accepts one argument another
  3409. 2:06:29checkout method that accepts two
  3410. 2:06:31arguments then three
  3411. 2:06:32and then we continue on and on and on so
  3412. 2:06:34yeah don't do this
  3413. 2:06:35a better solution would be to use this
  3414. 2:06:37params keyword where we only need a
  3415. 2:06:39single method and this one single method
  3416. 2:06:42can accept a
  3417. 2:06:43varying number of arguments we don't
  3418. 2:06:44need like several copies of the same
  3419. 2:06:46method then
  3420. 2:06:47so to use the params keyword precede our
  3421. 2:06:50data type with the word params
  3422. 2:06:53then after the data type follow this
  3423. 2:06:55with a set of square brackets
  3424. 2:06:57now this parameter a is now an array i'm
  3425. 2:07:00going to name this as something a little
  3426. 2:07:02more descriptive like
  3427. 2:07:03prices because we're working with prices
  3428. 2:07:06so we need to iterate over the elements
  3429. 2:07:08of an array
  3430. 2:07:09and create a total so i'm going to
  3431. 2:07:11create a local variable
  3432. 2:07:13named double total so you can have
  3433. 2:07:16variables with the same name
  3434. 2:07:18but they need to be within a different
  3435. 2:07:19scope so these are in different methods
  3436. 2:07:22so they are not visible to each other
  3437. 2:07:24double total i will set the sql to zero
  3438. 2:07:27to begin with
  3439. 2:07:28and we'll use a for each loop to iterate
  3440. 2:07:31over the elements of this array
  3441. 2:07:33for each double
  3442. 2:07:36price in prices
  3443. 2:07:40total equals total plus
  3444. 2:07:43whatever price we're working with but
  3445. 2:07:45i'll shorten this
  3446. 2:07:46to plus equals price then at the end
  3447. 2:07:50we will return total and this should
  3448. 2:07:53work the same
  3449. 2:07:54okay the total price is 24.74
  3450. 2:07:59but there's some formatting that we'll
  3451. 2:08:00have to do in a future video and then i
  3452. 2:08:02can just keep on adding items to my
  3453. 2:08:04you know fictional shopping cart like i
  3454. 2:08:06would like to buy a bottle of water
  3455. 2:08:08for a dollar and a t-shirt for
  3456. 2:08:1110 and 25 cents
  3457. 2:08:15so you see by using this params keyword
  3458. 2:08:18a single method can accept
  3459. 2:08:20a varying amount of arguments so method
  3460. 2:08:22overloading
  3461. 2:08:23isn't always necessary if you're working
  3462. 2:08:25with a lot of different arguments you're
  3463. 2:08:27not really sure how many arguments are
  3464. 2:08:28going to be passed in
  3465. 2:08:30okay then everybody that is the params
  3466. 2:08:32keyword it can be applied to
  3467. 2:08:33a method parameter so that it can accept
  3468. 2:08:36a variable number of arguments
  3469. 2:08:38but the parameter type must be a single
  3470. 2:08:41dimensional array
  3471. 2:08:42and that is the params keyword if you
  3472. 2:08:44can help me out by smashing that like
  3473. 2:08:46button
  3474. 2:08:46drop a random comment down below and
  3475. 2:08:48well yeah that's the params keyword
  3476. 2:08:50in c-sharp hey
  3477. 2:08:54well it looks like we're on exceptions
  3478. 2:08:56today an exception
  3479. 2:08:57is an error that occurs during execution
  3480. 2:09:00so here's an example i have a small
  3481. 2:09:02program
  3482. 2:09:03where we will ask a user to type in two
  3483. 2:09:05numbers divide those numbers then
  3484. 2:09:06display the result
  3485. 2:09:08so what if i type in something that's
  3486. 2:09:10not a number i would like to divide
  3487. 2:09:11the number 5 by the word pizza we'll
  3488. 2:09:14encounter an exception
  3489. 2:09:16and this interrupts our program we
  3490. 2:09:17encountered a format exception
  3491. 2:09:20input string was not in a correct format
  3492. 2:09:22so any code that is considered dangerous
  3493. 2:09:25where it might cause an exception
  3494. 2:09:27we can surround with a try block so i
  3495. 2:09:29would consider this code to be dangerous
  3496. 2:09:31so type try then a set of curly braces
  3497. 2:09:34surround any code that is considered
  3498. 2:09:36dangerous with
  3499. 2:09:37the set of curly braces now if we have a
  3500. 2:09:39try block we also need a catch block
  3501. 2:09:41so after the try block add catch and the
  3502. 2:09:44catch block catches and handles
  3503. 2:09:46exceptions when they occur
  3504. 2:09:47but we need to specify what kinds of
  3505. 2:09:50exceptions we would like to catch and
  3506. 2:09:51handle
  3507. 2:09:52so catch parentheses curly braces and
  3508. 2:09:55this is kind of like a parameter so i
  3509. 2:09:57would like to catch
  3510. 2:09:58and handle any format
  3511. 2:10:01exceptions then add e so
  3512. 2:10:04if we encounter that same exception
  3513. 2:10:06again instead of our program being
  3514. 2:10:08interrupted we can do something else
  3515. 2:10:09instead
  3516. 2:10:10so let's write a message and let the
  3517. 2:10:11user know to type in only numbers enter
  3518. 2:10:15only numbers please
  3519. 2:10:18and let's try that again enter number
  3520. 2:10:21one
  3521. 2:10:22five enter number two pizza enter only
  3522. 2:10:25numbers please
  3523. 2:10:26and this did not interrupt our program
  3524. 2:10:29that's the important thing
  3525. 2:10:30now these try and catch blocks they will
  3526. 2:10:32only catch and handle
  3527. 2:10:33format exceptions what if somebody
  3528. 2:10:35accidentally divides by zero or
  3529. 2:10:37intentionally does so
  3530. 2:10:38so we do not have a catch block set up
  3531. 2:10:41for that we
  3532. 2:10:42can add multiple catch blocks so let's
  3533. 2:10:44catch any
  3534. 2:10:45divide by zero exceptions so catch
  3535. 2:10:47parentheses curly braces
  3536. 2:10:49and the exception we would like to catch
  3537. 2:10:51is divide by
  3538. 2:10:52zero exception then add e so if somebody
  3539. 2:10:56attempts to divide by zero
  3540. 2:10:57let's display a message you can't divide
  3541. 2:11:01by zero idiot but this only occurs with
  3542. 2:11:05integer division so let's turn our
  3543. 2:11:07doubles
  3544. 2:11:08x and y into integers and we'll have to
  3545. 2:11:12change
  3546. 2:11:12this cast as well so change to double
  3547. 2:11:15to and 32.
  3548. 2:11:20so if we attempt to divide a number by
  3549. 2:11:23zero which we can't mathematically do
  3550. 2:11:25this is what happens enter number one
  3551. 2:11:27five enter number two
  3552. 2:11:28zero you can't divide by zero idiot and
  3553. 2:11:31this will not interrupt our program
  3554. 2:11:33now you can add a catch block that just
  3555. 2:11:35catches everything but it's not
  3556. 2:11:37considered good practice to add by
  3557. 2:11:38itself
  3558. 2:11:39so sometimes i'll add a like catch all
  3559. 2:11:41block just in case if there's any
  3560. 2:11:43exceptions i do not anticipate
  3561. 2:11:45so at the end we can add exception e but
  3562. 2:11:48it's difficult to let the user know what
  3563. 2:11:50went wrong exactly because
  3564. 2:11:51it'll just print when any exception
  3565. 2:11:53occurs something
  3566. 2:11:54went wrong so this will catch everything
  3567. 2:11:58let's remove the first two catch blocks
  3568. 2:12:00so let's take
  3569. 2:12:02five divided by the word pizza something
  3570. 2:12:04went wrong
  3571. 2:12:05now catch exception e does catch
  3572. 2:12:07everything but it's considered poor
  3573. 2:12:09practice to have this by itself
  3574. 2:12:11because basically you're shrugging when
  3575. 2:12:13something goes wrong you should let the
  3576. 2:12:15user know exactly what went wrong
  3577. 2:12:17so it's better practice to catch
  3578. 2:12:19specific exceptions first
  3579. 2:12:21then at the end you can add you know
  3580. 2:12:23exception e in case there's anything you
  3581. 2:12:24don't anticipate
  3582. 2:12:26by the way i changed x and y back to
  3583. 2:12:28doubles now
  3584. 2:12:29the last thing we need to talk about is
  3585. 2:12:31the finally block
  3586. 2:12:32the finally block is optional it always
  3587. 2:12:35executes regardless if an exception is
  3588. 2:12:37caught or not
  3589. 2:12:38so at the end let's create a finally
  3590. 2:12:40block type finally
  3591. 2:12:41curly braces if there's anything you'd
  3592. 2:12:43like to do after
  3593. 2:12:45you're trying catch block regardless if
  3594. 2:12:47an exception occurs or not
  3595. 2:12:48you can place that here what people
  3596. 2:12:50typically use the finally block for
  3597. 2:12:52is to close any open files if they need
  3598. 2:12:55to reset anything
  3599. 2:12:57but for this example let's just print
  3600. 2:12:58something so let's display
  3601. 2:13:00a thank you message thanks for visiting
  3602. 2:13:05if we do not encounter any exceptions
  3603. 2:13:07this finally block will still execute
  3604. 2:13:09five divided by one is five thanks for
  3605. 2:13:11visiting
  3606. 2:13:12now if we do encounter and catch an
  3607. 2:13:15exception
  3608. 2:13:16well that finally block is still going
  3609. 2:13:18to execute as well
  3610. 2:13:20thanks for visiting so yeah those are
  3611. 2:13:22exceptions
  3612. 2:13:23they are errors that occur during
  3613. 2:13:25execution they interrupt the normal flow
  3614. 2:13:27of our program
  3615. 2:13:28any code that is considered dangerous we
  3616. 2:13:31should surround with a try block
  3617. 2:13:33and catch any exceptions when they occur
  3618. 2:13:35and finally can be added
  3619. 2:13:37and it will execute regardless if an
  3620. 2:13:39exception is caught or not
  3621. 2:13:40so yeah those are exceptions i will post
  3622. 2:13:43this code in the comment section down
  3623. 2:13:44below
  3624. 2:13:45if you can smash that like button drop a
  3625. 2:13:47random comment down below
  3626. 2:13:48and well yeah those are exceptions in c
  3627. 2:13:51sharp
  3628. 2:13:55all right welcome back we're talking
  3629. 2:13:56about the conditional operator which is
  3630. 2:13:58represented by a question mark
  3631. 2:14:00so the conditional operator is used in
  3632. 2:14:03conditional assignment
  3633. 2:14:04if a condition is true or false and we
  3634. 2:14:07follow this formula
  3635. 2:14:08we have some sort of condition or
  3636. 2:14:10expression then we add a question mark
  3637. 2:14:12kind of like we're asking a question
  3638. 2:14:14is this true if it is do x if not do y
  3639. 2:14:18so we'll write a simple program to check
  3640. 2:14:20the temperature outside
  3641. 2:14:22so we'll write this using an if-else
  3642. 2:14:23statement first and then later on we'll
  3643. 2:14:25use the conditional operator and take a
  3644. 2:14:27look at the differences
  3645. 2:14:28so let's say we have a double named
  3646. 2:14:31temperature
  3647. 2:14:32and i'll set this equal to 20 degrees
  3648. 2:14:35celsius
  3649. 2:14:36as well as a string message i will
  3650. 2:14:39declare this but not yet assign it
  3651. 2:14:41so what if we would like to assign our
  3652. 2:14:43variable message
  3653. 2:14:44a custom message based on the
  3654. 2:14:46temperature well if we're using
  3655. 2:14:48if else statements that would look like
  3656. 2:14:49this so we need a condition
  3657. 2:14:51if temperature is greater than or equal
  3658. 2:14:56to let's say
  3659. 2:14:5615 degrees celsius then we will assign
  3660. 2:15:00message a string message
  3661. 2:15:03equals it's
  3662. 2:15:07warm outside
  3663. 2:15:10else message
  3664. 2:15:13equals it's cold outside
  3665. 2:15:17and then at the end i would like to
  3666. 2:15:19display my message within a writeline
  3667. 2:15:21statement console that write line
  3668. 2:15:24message and this will work just fine
  3669. 2:15:27no issues so my temperature is currently
  3670. 2:15:2920
  3671. 2:15:30and this will print the message it's
  3672. 2:15:32warm outside now there is a way to
  3673. 2:15:34shorten this and that's by using a
  3674. 2:15:36conditional operator
  3675. 2:15:37so if we were to take that approach we
  3676. 2:15:39would have to follow this formula
  3677. 2:15:41so let me just copy this we need our
  3678. 2:15:43condition our condition
  3679. 2:15:44is temperature is greater than or equal
  3680. 2:15:47to
  3681. 2:15:4715. question mark like we're asking a
  3682. 2:15:50question
  3683. 2:15:51if it's true then do this so we will
  3684. 2:15:53take
  3685. 2:15:54its warm outside in place of x
  3686. 2:15:58if not then we do this it's cold outside
  3687. 2:16:02but these are returning values so we
  3688. 2:16:04need to assign them to something
  3689. 2:16:06so we will say message equals
  3690. 2:16:10this is our condition temperature
  3691. 2:16:12greater than or equal to 15
  3692. 2:16:13question mark if that's true assign this
  3693. 2:16:17if not assign that and that's all there
  3694. 2:16:19is to it and we don't really need all of
  3695. 2:16:20this anymore
  3696. 2:16:22and then i will display my message
  3697. 2:16:23within a redline statement
  3698. 2:16:25and this will do the same thing it's
  3699. 2:16:28warm outside
  3700. 2:16:29you could even take this a step further
  3701. 2:16:30too you could place just this within a
  3702. 2:16:32right line statement and get rid of the
  3703. 2:16:34message completely
  3704. 2:16:35we don't even really need that message
  3705. 2:16:36variable
  3706. 2:16:38so yeah that's the conditional operator
  3707. 2:16:40you can use this in conditional
  3708. 2:16:42assignment if you need to assign
  3709. 2:16:44some variable if some condition that you
  3710. 2:16:46set is true or false
  3711. 2:16:47it's kind of like a shortcut so yeah
  3712. 2:16:49that's the conditional operator
  3713. 2:16:51in c sharp
  3714. 2:16:55all right what's going on people string
  3715. 2:16:57interpolation string interpolation
  3716. 2:16:59allows us to insert variables or values
  3717. 2:17:02into
  3718. 2:17:03a string literal we precede a string
  3719. 2:17:06literal with a dollar sign
  3720. 2:17:08and then any placeholders within a
  3721. 2:17:10string literal
  3722. 2:17:11that is interpolated we can use to
  3723. 2:17:14insert values or variables within so
  3724. 2:17:16here's an example of where
  3725. 2:17:17string interpolation could be useful
  3726. 2:17:20let's say we have three variables
  3727. 2:17:21first name last name and age let's
  3728. 2:17:24declare those
  3729. 2:17:25string first name make up a first name
  3730. 2:17:31string last name make up a
  3731. 2:17:34last name and then an age
  3732. 2:17:39okay so what if we would like to display
  3733. 2:17:41somebody's first name last name and
  3734. 2:17:43their age
  3735. 2:17:43let's use a standard string literal
  3736. 2:17:45first and we'll look at the differences
  3737. 2:17:48so if i would like to display something
  3738. 2:17:50like
  3739. 2:17:51hello plus first name
  3740. 2:17:56plus i'll add a space plus
  3741. 2:18:00last name and then maybe add like a
  3742. 2:18:04period at the end well i would have to
  3743. 2:18:07concatenate all these separate strings
  3744. 2:18:09together
  3745. 2:18:10and then let me display age two
  3746. 2:18:12console.writeline
  3747. 2:18:14you are plus
  3748. 2:18:18age plus years old
  3749. 2:18:22so this works fine but an easier way is
  3750. 2:18:24to use string interpolation
  3751. 2:18:26so what we're going to do is create a
  3752. 2:18:28string literal
  3753. 2:18:30and then precede this with a dollar sign
  3754. 2:18:32and then write a string normally
  3755. 2:18:35hello wherever you would like to insert
  3756. 2:18:37a value or variable
  3757. 2:18:39add a set of curly braces as a
  3758. 2:18:41placeholder
  3759. 2:18:42and i would like to insert first name
  3760. 2:18:44here then add a space
  3761. 2:18:46and here within my string literal i will
  3762. 2:18:49insert
  3763. 2:18:50last name and then maybe add a period to
  3764. 2:18:53the end
  3765. 2:18:54and let's do the same thing with our
  3766. 2:18:55second line so we need a string literal
  3767. 2:18:58a double set of quotes precede this with
  3768. 2:19:00a dollar sign
  3769. 2:19:02you are curly braces
  3770. 2:19:05age years old and this will do the exact
  3771. 2:19:10same thing
  3772. 2:19:11however it's less work
  3773. 2:19:14so hello first name last name you are
  3774. 2:19:16age years old
  3775. 2:19:17another thing that you can do too is
  3776. 2:19:19that when you display one of these
  3777. 2:19:20values
  3778. 2:19:21you can allocate some room after the
  3779. 2:19:23variable name add a comma
  3780. 2:19:25and then how many spaces you would like
  3781. 2:19:27to allocate let's say that when i
  3782. 2:19:28display my age i would like to allocate
  3783. 2:19:3110 spaces worth of room to display my
  3784. 2:19:33edge
  3785. 2:19:34and this is what that looks like you are
  3786. 2:19:38then i have 10 spaces including the two
  3787. 2:19:41digits in my age
  3788. 2:19:42and if this was negative this would be
  3789. 2:19:45left aligned
  3790. 2:19:48so my age is all the way over here then
  3791. 2:19:50i have eight spaces afterwards
  3792. 2:19:52so yeah that's string interpolation it
  3793. 2:19:54allows us to insert variables into a
  3794. 2:19:57string
  3795. 2:19:58literal we precede a string literal with
  3796. 2:20:00a dollar sign
  3797. 2:20:02and then curly braces with then as
  3798. 2:20:04placeholders for values or variables
  3799. 2:20:07so yeah that's string interpolation in c
  3800. 2:20:09sharp if you found this video helpful
  3801. 2:20:11please be sure to smash that like button
  3802. 2:20:13leave a random comment down below and
  3803. 2:20:15subscribe if you'd like to become a
  3804. 2:20:16fellow bro
  3805. 2:20:20alright what's going on
  3806. 2:20:21multi-dimensional arrays so
  3807. 2:20:23just a fair warning this is a difficult
  3808. 2:20:25topic if you want you can skip this
  3809. 2:20:27video you won't hurt my feelings
  3810. 2:20:28but if you'd like to know what a
  3811. 2:20:29multi-dimensional array is well here we
  3812. 2:20:31go
  3813. 2:20:32so a multi-dimensional array is an array
  3814. 2:20:35of arrays
  3815. 2:20:36so i have three standard arrays i have
  3816. 2:20:38different car manufacturers
  3817. 2:20:39ford chevy and toyota so each array has
  3818. 2:20:43three different kinds of cars a
  3819. 2:20:45multi-dimensional array is good if you
  3820. 2:20:46need like some sort of grid or matrix of
  3821. 2:20:49data
  3822. 2:20:50so what i would like to do is to create
  3823. 2:20:51an array of arrays to simulate maybe
  3824. 2:20:53like a parking lot to park all of these
  3825. 2:20:55cars
  3826. 2:20:56so to create a multi-dimensional array
  3827. 2:20:58this will be a two-dimensional array
  3828. 2:21:00we need to list the data type of what
  3829. 2:21:02we're storing strings
  3830. 2:21:04then a set of straight brackets and
  3831. 2:21:07then within the straight brackets add a
  3832. 2:21:09comma
  3833. 2:21:11so now i'm going to name this array
  3834. 2:21:13let's say
  3835. 2:21:14parking lot
  3836. 2:21:17equals and then we add our curly braces
  3837. 2:21:19much like what we do with standard
  3838. 2:21:21arrays
  3839. 2:21:22however i'm going to add each array
  3840. 2:21:24within the set of curly braces
  3841. 2:21:26and then separate each with a comma
  3842. 2:21:33so this can be somewhat difficult to
  3843. 2:21:35read i like to kind of condense it all
  3844. 2:21:38and just make it look nice
  3845. 2:21:42so you can see that this resembles sort
  3846. 2:21:44of like a grid with rows and columns
  3847. 2:21:47and that visual is going to help us so
  3848. 2:21:49to access one of these elements
  3849. 2:21:51we need to list an index for the row and
  3850. 2:21:54an index for the column
  3851. 2:21:55so let's say i would like to change this
  3852. 2:21:57explorer to something else
  3853. 2:21:59so in order to access this element i'm
  3854. 2:22:02going to type the name of our
  3855. 2:22:03multi-dimensional array
  3856. 2:22:05straight brackets and then i need a row
  3857. 2:22:08and column
  3858. 2:22:09so these always start with zero this
  3859. 2:22:11would be row zero
  3860. 2:22:12row one row two i need to access
  3861. 2:22:15row one so that would be zero then comma
  3862. 2:22:18for column and then i need to count the
  3863. 2:22:21row number
  3864. 2:22:22zero one two so that would be
  3865. 2:22:25zero comma two for this element within
  3866. 2:22:28my
  3867. 2:22:28two-dimensional array so i'm going to
  3868. 2:22:30change this to something else
  3869. 2:22:32so let's say that this is a ford fusion
  3870. 2:22:36and then let's display these elements we
  3871. 2:22:37can use a for each loop
  3872. 2:22:40for each the data type is string
  3873. 2:22:43let's say car in parking lot
  3874. 2:22:48and then i will just iterate through
  3875. 2:22:49these console.writeline
  3876. 2:22:52card so this will iterate nine times
  3877. 2:22:55one for each element within our
  3878. 2:22:57two-dimensional array
  3879. 2:23:00so we have a mustang f-150 then fusion
  3880. 2:23:03so we updated
  3881. 2:23:04explorer to fusion so let's update maybe
  3882. 2:23:07this corolla to something else
  3883. 2:23:09so let's count the row number that would
  3884. 2:23:11be zero
  3885. 2:23:12one two parking
  3886. 2:23:15lot two and the column number is
  3887. 2:23:19zero and let's say that this is a tacoma
  3888. 2:23:25and try this again okay we have
  3889. 2:23:28instead of a corolla a tacoma
  3890. 2:23:32now if you would like to display this as
  3891. 2:23:34like a grid we can use
  3892. 2:23:35nested for loops for that so let's get
  3893. 2:23:38rid of this for each loop
  3894. 2:23:39i'll just turn this all into one giant
  3895. 2:23:41comment
  3896. 2:23:43okay so to display this as like a grid
  3897. 2:23:45we'll use
  3898. 2:23:46nested for loops the outer loop will be
  3899. 2:23:49four
  3900. 2:23:50and i equal zero we will continue this
  3901. 2:23:53as long as i
  3902. 2:23:55is less than parking lot
  3903. 2:23:58dot get length method and then pass in
  3904. 2:24:02the dimension so pass in 0 for the first
  3905. 2:24:05dimension
  3906. 2:24:07i plus plus and then we need a nested
  3907. 2:24:10for loop
  3908. 2:24:12so change i to j get
  3909. 2:24:15length one so this represents the second
  3910. 2:24:18dimension remember that these always
  3911. 2:24:20start with zero
  3912. 2:24:21and then change i to j plus plus so
  3913. 2:24:25during each iteration of the inner for
  3914. 2:24:27loop let's change this from
  3915. 2:24:29right line to right i will display
  3916. 2:24:33parking lot straight brackets
  3917. 2:24:37i comma j then i'm just going to add a
  3918. 2:24:40space to separate
  3919. 2:24:41each car and then when we escape our for
  3920. 2:24:44loop we
  3921. 2:24:45need to go down to the next row so i'll
  3922. 2:24:47use an empty right line statement
  3923. 2:24:49so by using nested for loops we can
  3924. 2:24:51display our
  3925. 2:24:53two-dimensional array as sort of a grid
  3926. 2:24:55or matrix
  3927. 2:24:57so we have our mustang f-150 our fusion
  3928. 2:25:00corvette camaro silverado tacoma camry
  3929. 2:25:04and rav4
  3930. 2:25:05so if you ever need like a grid or
  3931. 2:25:07matrix of data you can always use a
  3932. 2:25:09multi-dimensional
  3933. 2:25:10array and these are kind of the steps to
  3934. 2:25:12set it up
  3935. 2:25:13so yeah those are multi-dimensional
  3936. 2:25:15arrays if you found this video helpful
  3937. 2:25:17please be sure to smash that like button
  3938. 2:25:19leave a random comment down below and
  3939. 2:25:21subscribe if you'd like to become a
  3940. 2:25:22fellow bro
  3941. 2:25:25alright classes a class is really just a
  3942. 2:25:28bundle of related code that you would
  3943. 2:25:30like to organize together
  3944. 2:25:32it can be used as a blueprint to create
  3945. 2:25:34objects with object-oriented programming
  3946. 2:25:37but we'll discuss that in the next video
  3947. 2:25:40so my main method
  3948. 2:25:41is located within a class named program
  3949. 2:25:44the point of this class is well to group
  3950. 2:25:46my program together
  3951. 2:25:47but there are classes too that have
  3952. 2:25:49certain functions for example the math
  3953. 2:25:51class the purpose of the math class
  3954. 2:25:53is to contain a bunch of useful methods
  3955. 2:25:55related to mathematics
  3956. 2:25:57and let's take a look at this
  3957. 2:26:00so this class is named math and all
  3958. 2:26:03that's in here is a bunch of methods
  3959. 2:26:04related to
  3960. 2:26:05mathematics so that's kind of the point
  3961. 2:26:07of this class but we can create our own
  3962. 2:26:09classes too
  3963. 2:26:10now to create a class we can either do
  3964. 2:26:12so outside of our class program
  3965. 2:26:14or create a class within a separate
  3966. 2:26:16c-sharp file i'll show you both ways
  3967. 2:26:19so to create a class make sure that
  3968. 2:26:20we're not writing this within our class
  3969. 2:26:22program so that ends here
  3970. 2:26:24type class and let's create a class to
  3971. 2:26:27group a bunch of useful messages
  3972. 2:26:29together like message
  3973. 2:26:31methods class messages curly braces
  3974. 2:26:35and let's create a few methods related
  3975. 2:26:37to messages
  3976. 2:26:38so this is going to be more of like a
  3977. 2:26:40utility class
  3978. 2:26:42void hello and when we use this method
  3979. 2:26:45let's display a message like hello
  3980. 2:26:50welcome to the program and let's create
  3981. 2:26:54maybe two more
  3982. 2:26:55void hello void what about waiting like
  3983. 2:26:59we're waiting for something
  3984. 2:27:03i am waiting for something
  3985. 2:27:08and what about goodbye void bye
  3986. 2:27:14bye
  3987. 2:27:16thanks for visiting
  3988. 2:27:19if you would like to create a class
  3989. 2:27:20within a separate c-sharp
  3990. 2:27:22file i would recommend opening your
  3991. 2:27:24solution explorer if you don't have that
  3992. 2:27:25open you can find it here
  3993. 2:27:26go to view solution explorer
  3994. 2:27:30right click on your namespace add
  3995. 2:27:34class
  3996. 2:27:37class and then i will name this messages
  3997. 2:27:42add okay then i'm going to take
  3998. 2:27:46my code from within this class cut it
  3999. 2:27:50delete my class and then paste it within
  4000. 2:27:54this class messages so you can either
  4001. 2:27:58write your class within
  4002. 2:27:59the same c sharp file or a different one
  4003. 2:28:02if i would like to use some of these
  4004. 2:28:03methods found within
  4005. 2:28:04the messages class there's one of two
  4006. 2:28:06things that i need to do
  4007. 2:28:07i either need to create an object from
  4008. 2:28:09this class
  4009. 2:28:10or i need to precede my class definition
  4010. 2:28:14with the word
  4011. 2:28:14static followed by everything within my
  4012. 2:28:17messages class
  4013. 2:28:19so we'll talk about creating objects in
  4014. 2:28:20the next video so for now we'll just
  4015. 2:28:22have to
  4016. 2:28:23use the static keyword and i'll create a
  4017. 2:28:25separate video on this too
  4018. 2:28:27and we should now be able to use these
  4019. 2:28:28methods found within my messages class
  4020. 2:28:31so if i need to display a hello message
  4021. 2:28:34i would type the name of the class
  4022. 2:28:36that would be messages then dot to
  4023. 2:28:39access the
  4024. 2:28:40members however these are not visible so
  4025. 2:28:42we need to make them public
  4026. 2:28:44and that'll be a separate video on axis
  4027. 2:28:46modifiers so i would like this to be a
  4028. 2:28:48public method
  4029. 2:28:49so precede static with public
  4030. 2:28:53and we should now be able to see those
  4031. 2:28:55methods so type messages again
  4032. 2:28:57dot and here are those methods i would
  4033. 2:28:59like to use the hello method
  4034. 2:29:01and that will display my message hello
  4035. 2:29:04welcome to the program
  4036. 2:29:05i also have that waiting message
  4037. 2:29:09messages dot and that was waiting
  4038. 2:29:13as well as goodbye messages
  4039. 2:29:16dot bye so hello welcome to the program
  4040. 2:29:20i am waiting for something bye thanks
  4041. 2:29:22for visiting
  4042. 2:29:23so basically a class is just a bundle of
  4043. 2:29:26related code
  4044. 2:29:28and it can be used as a blueprint to
  4045. 2:29:30create objects
  4046. 2:29:31otherwise you can use a class more or
  4047. 2:29:33less as a utility class
  4048. 2:29:35but when you define your class you'll
  4049. 2:29:36have to precede the class definition
  4050. 2:29:38with the word static
  4051. 2:29:40and to make this public and accessible
  4052. 2:29:42you would have to precede each method
  4053. 2:29:44with
  4054. 2:29:44public and static so yeah those are
  4055. 2:29:47classes they're really just a bundle of
  4056. 2:29:49related code
  4057. 2:29:50you can use them as a utility class or
  4058. 2:29:53use them as a blueprint to create
  4059. 2:29:55objects
  4060. 2:29:55which we'll discuss in another video if
  4061. 2:29:58you found this video helpful please help
  4062. 2:29:59me by smashing that like button
  4063. 2:30:01leave a random comment down below and
  4064. 2:30:03well yeah those are classes
  4065. 2:30:05in c sharp
  4066. 2:30:09all right objects an object is an
  4067. 2:30:12instance of a class
  4068. 2:30:13that means that we can use a class as a
  4069. 2:30:16type of blueprint to create objects with
  4070. 2:30:18object oriented programming
  4071. 2:30:20so look around you right now you're
  4072. 2:30:21surrounded by objects next to me i have
  4073. 2:30:23a microphone a monitor a mouse
  4074. 2:30:27and a cup of coffee so we can use code
  4075. 2:30:29to mimic
  4076. 2:30:30real world objects objects can have
  4077. 2:30:33fields and
  4078. 2:30:34methods these are defining
  4079. 2:30:35characteristics and
  4080. 2:30:37actions that an object can perform they
  4081. 2:30:40describe what an object has
  4082. 2:30:41and what an object can do and in today's
  4083. 2:30:44video we will be instantiating human
  4084. 2:30:46objects but will need the help of a
  4085. 2:30:48human class
  4086. 2:30:49as a type of blueprint to design what
  4087. 2:30:51sorts of fields and methods that all
  4088. 2:30:53humans should have
  4089. 2:30:55so outside of our class program you can
  4090. 2:30:57either do this within the same
  4091. 2:30:58c-sharp file or a separate one within
  4092. 2:31:01your solution explorer
  4093. 2:31:02i'll keep it all within the same c-sharp
  4094. 2:31:04file this time let's create a class
  4095. 2:31:06named human then curly braces so let's
  4096. 2:31:09add some fields
  4097. 2:31:10fields are kind of like the defining
  4098. 2:31:12attributes of what all humans should
  4099. 2:31:14have
  4100. 2:31:15let's say that humans have a name and an
  4101. 2:31:17age
  4102. 2:31:18so i'll define those much like what we
  4103. 2:31:20do with variables
  4104. 2:31:21string name i will declare this but not
  4105. 2:31:23yet assign it
  4106. 2:31:24as well as int edge
  4107. 2:31:27so these are fields they're defining
  4108. 2:31:29characteristics of what an object has
  4109. 2:31:32now let's create some methods what kinds
  4110. 2:31:33of actions can humans perform
  4111. 2:31:35they can eat and they can sleep
  4112. 2:31:38void eat and then i will display
  4113. 2:31:41something when we invoke this method
  4114. 2:31:43let's say name plus
  4115. 2:31:47is eating and then let's create a
  4116. 2:31:51sleep method void sleep
  4117. 2:31:55name plus is sleeping
  4118. 2:31:58okay now this is what we need to do to
  4119. 2:32:00instantiate an object from a class
  4120. 2:32:03type the name of the class human we need
  4121. 2:32:05a unique name for this human object
  4122. 2:32:08let's call this human object just human
  4123. 2:32:10one
  4124. 2:32:11human human one equals new human again
  4125. 2:32:15add a set of parentheses and a semicolon
  4126. 2:32:18there we go we now have a human object
  4127. 2:32:20named human
  4128. 2:32:22one now there's just one problem the
  4129. 2:32:24fields and methods of this human object
  4130. 2:32:26are not visible
  4131. 2:32:27so if i type human one dot we can't
  4132. 2:32:30access the name
  4133. 2:32:32and age fields as well as the eat and
  4134. 2:32:34sleep methods
  4135. 2:32:35so normally i wouldn't recommend doing
  4136. 2:32:37this because this isn't secure but just
  4137. 2:32:39for learning purposes
  4138. 2:32:41let's precede our fields and methods
  4139. 2:32:43with the axis modifier of public
  4140. 2:32:46so that these are publicly accessible
  4141. 2:32:48from another class
  4142. 2:32:49so if we type human one dot we should be
  4143. 2:32:52able to access our name
  4144. 2:32:54and age fields as well as the eat and
  4145. 2:32:57sleep methods
  4146. 2:32:58so let's give humanoid a name
  4147. 2:33:01human1.name
  4148. 2:33:02and let's assign human1 a first name of
  4149. 2:33:06rick like from rick and morty and let's
  4150. 2:33:08give him an age too
  4151. 2:33:09human one dot h and he will be 65 years
  4152. 2:33:13old
  4153. 2:33:14and we should be able to use the eat and
  4154. 2:33:17sleep methods now
  4155. 2:33:19humanone dot
  4156. 2:33:22eat and then human
  4157. 2:33:25one dot sleep
  4158. 2:33:29so after invoking these two methods eat
  4159. 2:33:32and sleep this will display
  4160. 2:33:35rick is eating and rick is sleeping so
  4161. 2:33:37congratulations we have successfully
  4162. 2:33:39instantiated a human object
  4163. 2:33:42and the nice thing about classes is that
  4164. 2:33:44we can keep on using this class to
  4165. 2:33:45create more humans if we need more than
  4166. 2:33:47one
  4167. 2:33:48so this time let's create a second human
  4168. 2:33:50so follow this formula the name of the
  4169. 2:33:52class
  4170. 2:33:53human then we need a unique name for
  4171. 2:33:55this human object
  4172. 2:33:56human one is taken so let's name the
  4173. 2:33:58second human human to
  4174. 2:34:00equals new human and then we can assign
  4175. 2:34:03some of the fields
  4176. 2:34:04of our human class so we need a name and
  4177. 2:34:07an age
  4178. 2:34:08so human 2's name will be morty
  4179. 2:34:13and his age will be 16 and this time
  4180. 2:34:16let's have human 2 use the eat and
  4181. 2:34:19sleep method so rick is eating
  4182. 2:34:22rick is sleeping morty is eating morty
  4183. 2:34:25is sleeping
  4184. 2:34:26so that's basically what an object is
  4185. 2:34:28it's an instance of a class
  4186. 2:34:30and a class can be used as a blueprint
  4187. 2:34:32to create objects
  4188. 2:34:33and objects can have fields and methods
  4189. 2:34:36fields are what an object has
  4190. 2:34:38methods are what an object can do think
  4191. 2:34:40of it that way
  4192. 2:34:41so yeah those are objects they're an
  4193. 2:34:43instance of a class
  4194. 2:34:45and a class can be used as a blueprint
  4195. 2:34:46to create objects
  4196. 2:34:48so if this video helped you out help me
  4197. 2:34:50out by smashing that like button
  4198. 2:34:52leave a random comment down below and as
  4199. 2:34:54always subscribe if you'd like to become
  4200. 2:34:56a fellow bro
  4201. 2:35:00all right constructors a constructor is
  4202. 2:35:02a
  4203. 2:35:03special method found within a class it
  4204. 2:35:05has the same name as the class name
  4205. 2:35:07in the last video we created a human
  4206. 2:35:09class but it's missing a constructor
  4207. 2:35:11if we do not explicitly create a
  4208. 2:35:13constructor there is a hidden one behind
  4209. 2:35:15the scenes that is automatically called
  4210. 2:35:17and it is used to create
  4211. 2:35:18an object for us so let's explicitly
  4212. 2:35:21create a constructor
  4213. 2:35:22so you type public because we want this
  4214. 2:35:25to be publicly accessible
  4215. 2:35:26and then it has the same name as the
  4216. 2:35:28class name then add a side of
  4217. 2:35:30parentheses then a set of curly braces
  4218. 2:35:32this works just like a method and we can
  4219. 2:35:34even set up parameters
  4220. 2:35:36so let's set up name and age parameters
  4221. 2:35:38so we need to list the data type
  4222. 2:35:40string name and int edge now
  4223. 2:35:43in order to create a human object we
  4224. 2:35:45have to pass a matching set of arguments
  4225. 2:35:47we need to pass in a name and an age
  4226. 2:35:50well a string to function as a name and
  4227. 2:35:52an integer to function as an edge
  4228. 2:35:54so we don't manually need to assign
  4229. 2:35:56these fields anymore
  4230. 2:35:57we can just pass these values as
  4231. 2:35:59arguments so with human one human one's
  4232. 2:36:01name was rick
  4233. 2:36:02and his age was 65 so i will pass these
  4234. 2:36:05as arguments
  4235. 2:36:06rick and 65 and you can see that that
  4236. 2:36:09red underline went away
  4237. 2:36:10so let's do the same thing for human too
  4238. 2:36:13morty and his age 16
  4239. 2:36:14and we no longer need to manually assign
  4240. 2:36:17those fields
  4241. 2:36:17however within the constructor we'll
  4242. 2:36:19need to do so so
  4243. 2:36:21type this dot the name of the field we
  4244. 2:36:23would like to assign
  4245. 2:36:25equals the name of the parameter and in
  4246. 2:36:28this case they have the same name
  4247. 2:36:30this dot name equals name and this
  4248. 2:36:33dot age equals
  4249. 2:36:37whatever this is age and this will work
  4250. 2:36:40the same as before but we did not need
  4251. 2:36:42to
  4252. 2:36:42manually assign those values to those
  4253. 2:36:45fields so after calling
  4254. 2:36:46the eaton sleep method for both human
  4255. 2:36:49one and human too
  4256. 2:36:50this will display rick is eating rick is
  4257. 2:36:52sleeping morty is eating morty's
  4258. 2:36:54sleeping
  4259. 2:36:55so that's kind of what a constructor is
  4260. 2:36:56it's a special method found within a
  4261. 2:36:58class
  4262. 2:36:59and it's automatically called when we
  4263. 2:37:00instantiate an object if we do not
  4264. 2:37:03explicitly create
  4265. 2:37:04a constructor but if we do we can
  4266. 2:37:06manually assign
  4267. 2:37:07some values to fields of an object let's
  4268. 2:37:10try a different example this time
  4269. 2:37:11instead of humans let's create a car
  4270. 2:37:13class we will instantiate some car
  4271. 2:37:15objects so let's delete our current
  4272. 2:37:17class and create a new one
  4273. 2:37:18class car then curly braces
  4274. 2:37:22cars will have let's say four fields
  4275. 2:37:26a make a model a year and a color
  4276. 2:37:30string make string
  4277. 2:37:33model int
  4278. 2:37:37year and string
  4279. 2:37:40color then to create a constructor this
  4280. 2:37:44will have the same name as the class
  4281. 2:37:46name precede this with public car
  4282. 2:37:49parentheses curly braces and again this
  4283. 2:37:52is a special kind of method and we can
  4284. 2:37:54set up arguments and parameters
  4285. 2:37:56so we have string make
  4286. 2:38:00string model
  4287. 2:38:03integer
  4288. 2:38:06and string color
  4289. 2:38:10and then when we receive arguments we
  4290. 2:38:12can assign them to these fields
  4291. 2:38:14this dot make
  4292. 2:38:18equals make
  4293. 2:38:21this dot model equals model
  4294. 2:38:25this dot year equals year
  4295. 2:38:29and this dot color equals
  4296. 2:38:33color then let's create maybe a drive
  4297. 2:38:35method
  4298. 2:38:38public void drive
  4299. 2:38:43and we will display something
  4300. 2:38:46u drive the
  4301. 2:38:50plus make plus a lot of space to
  4302. 2:38:53separate make and model
  4303. 2:38:55plus model okay now let's instantiate
  4304. 2:38:58some car
  4305. 2:38:59objects so we type the name of the class
  4306. 2:39:02car then we need a unique name or
  4307. 2:39:04identifier for this car
  4308. 2:39:06let's call this first car car one equals
  4309. 2:39:08new
  4310. 2:39:09car parenthesis semicolon but to
  4311. 2:39:11instantiate a car object we have to pass
  4312. 2:39:13a
  4313. 2:39:14matching set of arguments to these
  4314. 2:39:16parameters we have to pass
  4315. 2:39:18a make model year and color so car one
  4316. 2:39:21let's say is a ford mustang ford
  4317. 2:39:26mustang the year will be 2022
  4318. 2:39:30and the color is red and then let's have
  4319. 2:39:34car one
  4320. 2:39:34use the drive method so type car one
  4321. 2:39:38dot drive then a set of parentheses to
  4322. 2:39:41invoke it
  4323. 2:39:42u drive the ford mustang now let's
  4324. 2:39:45create cartoon
  4325. 2:39:46it's the same process as before but we
  4326. 2:39:49need a unique name or identifier
  4327. 2:39:51car car 2 equals new car this will be
  4328. 2:39:54a chevy corvette
  4329. 2:39:58and the year will be 20 21 and the color
  4330. 2:40:01is blue
  4331. 2:40:04and then let's use the drive method of
  4332. 2:40:06car 2
  4333. 2:40:07car 2 drive you drive the ford mustang
  4334. 2:40:11you drive the chevy corvette
  4335. 2:40:13so yeah that's a constructor it's a
  4336. 2:40:15special method
  4337. 2:40:16found within a class and it has the same
  4338. 2:40:18name as the class name
  4339. 2:40:20class car and the constructor name is
  4340. 2:40:22car as well and it can be used to assign
  4341. 2:40:24arguments to fields when creating an
  4342. 2:40:26object there's a few other things you
  4343. 2:40:28can do with constructors too but we'll
  4344. 2:40:30discuss that in future videos
  4345. 2:40:31so yeah those are constructors if you
  4346. 2:40:34can smash that like button
  4347. 2:40:35leave a random comment down below and
  4348. 2:40:37well yeah those are constructors
  4349. 2:40:39in c sharp oh yeah let's do this
  4350. 2:40:43all right the static modifier the static
  4351. 2:40:46modifier is used to declare a
  4352. 2:40:48static member one which belongs to the
  4353. 2:40:51class itself
  4354. 2:40:52rather than any one specific object i
  4355. 2:40:54have a class named carr cars have
  4356. 2:40:57a field named model and a constructor to
  4357. 2:41:00assign
  4358. 2:41:00a model when we instantiate a car object
  4359. 2:41:03and i have two car objects car one and
  4360. 2:41:05car two car one is a mustang
  4361. 2:41:07car two is a corvette here's the
  4362. 2:41:09situation let's say that we're going to
  4363. 2:41:11have a race but we need to keep track of
  4364. 2:41:13how many cars are instantiated that are
  4365. 2:41:15entering
  4366. 2:41:16our race one way in which we can do that
  4367. 2:41:18is to create a static field to keep
  4368. 2:41:20track of how many cars are created
  4369. 2:41:22so let me show you what this looks like
  4370. 2:41:24with a non-static field first
  4371. 2:41:26so let's declare an integer variable
  4372. 2:41:29named number
  4373. 2:41:30of cars to keep track of how many cars
  4374. 2:41:32we create
  4375. 2:41:34so i will declare this but not yet
  4376. 2:41:36assign it
  4377. 2:41:37and within my constructor i will
  4378. 2:41:39increment number of cars by one
  4379. 2:41:41remember that with constructors you're
  4380. 2:41:43not limited to
  4381. 2:41:44only assigning values to fields you can
  4382. 2:41:46do like any sort of code that you want
  4383. 2:41:48remember that it's just another kind of
  4384. 2:41:50method okay so every time we instantiate
  4385. 2:41:53a car object we will increment
  4386. 2:41:54number of cars by one and then let's
  4387. 2:41:57display a number of cars
  4388. 2:41:59so we need to access this in a
  4389. 2:42:01non-static way by typing
  4390. 2:42:03the name of a car object like car 1 dot
  4391. 2:42:05followed by the name of the field
  4392. 2:42:07but it looks like we need to make this
  4393. 2:42:08public
  4394. 2:42:10public and number of cars car one
  4395. 2:42:14dot number of cars let's do the same
  4396. 2:42:16thing for
  4397. 2:42:17car two car two dot number of cars
  4398. 2:42:20now you would think that the number of
  4399. 2:42:22cars would be two
  4400. 2:42:23right and that's where you're wrong
  4401. 2:42:25number of cars
  4402. 2:42:26for both car 1 and car 2 are both one
  4403. 2:42:29because each car object has their own
  4404. 2:42:32copy of the number of cars field and
  4405. 2:42:35within the constructor when we
  4406. 2:42:37increment each copy of number of cars
  4407. 2:42:40well it's only ever going to be a
  4408. 2:42:41maximum of one
  4409. 2:42:43so one way in which we could fix that is
  4410. 2:42:45to change
  4411. 2:42:46this field to a static field after
  4412. 2:42:49public type
  4413. 2:42:50static public static int number of cars
  4414. 2:42:54and we can no longer access this field
  4415. 2:42:56in a non-static way
  4416. 2:42:58by typing the name of an object followed
  4417. 2:43:00by the name of the field
  4418. 2:43:02we would have to access this field in a
  4419. 2:43:04static way
  4420. 2:43:05by typing the name of its class followed
  4421. 2:43:08by the name of the field
  4422. 2:43:09so type the name of the class car
  4423. 2:43:11followed by you know dot
  4424. 2:43:13then the name of the field so this field
  4425. 2:43:16now belongs to the car class
  4426. 2:43:18and no one object has complete ownership
  4427. 2:43:21of it it's kind of like they're all
  4428. 2:43:22sharing the same variable and let's try
  4429. 2:43:25this again
  4430. 2:43:26so the number of cars that we have
  4431. 2:43:28created
  4432. 2:43:29is two now just to test everything let's
  4433. 2:43:32create a third car we have a third racer
  4434. 2:43:34within our race
  4435. 2:43:36so car 3 will be a what about a lambo
  4436. 2:43:39and we now have created three cars
  4437. 2:43:42so again by preceding this field with
  4438. 2:43:44the static modifier
  4439. 2:43:46the class now owns it we can also apply
  4440. 2:43:48the static modifier
  4441. 2:43:50to a method as well public static
  4442. 2:43:53and we'll create a method to begin our
  4443. 2:43:55race let's call this
  4444. 2:43:56start race
  4445. 2:44:00so we don't want each object to be able
  4446. 2:44:02to start the race on their own terms
  4447. 2:44:04right
  4448. 2:44:05so it would be better if the car class
  4449. 2:44:07itself has a start race method
  4450. 2:44:10so then if we would like to begin our
  4451. 2:44:11race we can access it in a static way
  4452. 2:44:14the race has begun
  4453. 2:44:18then if i need to invoke this method i
  4454. 2:44:21type
  4455. 2:44:21the name of the class car dot
  4456. 2:44:25the name of the static method start race
  4457. 2:44:28then a set of parentheses to invoke it
  4458. 2:44:30and this should now begin our race
  4459. 2:44:32the race has begun and then you can also
  4460. 2:44:36apply the static modifier to a class
  4461. 2:44:38itself but then you can't instantiate
  4462. 2:44:40objects from this class you can see that
  4463. 2:44:42we're getting errors
  4464. 2:44:43cannot declare a variable of static type
  4465. 2:44:46cart
  4466. 2:44:46so that's kind of like the deal with the
  4467. 2:44:48math class
  4468. 2:44:49in order to use a method of the math
  4469. 2:44:51class we wouldn't create like a map
  4470. 2:44:53object right like
  4471. 2:44:54math math 1 equals new math right
  4472. 2:44:57and then we wouldn't type math1 dot
  4473. 2:45:00round to rounded number
  4474. 2:45:01it's a lot easier just to use directly
  4475. 2:45:04the math class and type
  4476. 2:45:06math dot round so that's kind of the
  4477. 2:45:09idea behind
  4478. 2:45:09a static class you can't create objects
  4479. 2:45:12from a static class
  4480. 2:45:14and we kind of learned about that in the
  4481. 2:45:15video on classes
  4482. 2:45:17well in conclusion the static modifier
  4483. 2:45:20can be applied to a field
  4484. 2:45:22a method or a class itself anything that
  4485. 2:45:25is declared static
  4486. 2:45:26now belongs to the class and no one
  4487. 2:45:29object has
  4488. 2:45:30ownership of it so that is the static
  4489. 2:45:33modifier
  4490. 2:45:34if this video helped you out help me out
  4491. 2:45:36by smashing that like button drop a
  4492. 2:45:37random comment down below and well
  4493. 2:45:39yeah that's the static modifier in
  4494. 2:45:41c-sharp
  4495. 2:45:45all right overloaded constructors by
  4496. 2:45:48using overloaded constructors this is a
  4497. 2:45:50technique to create
  4498. 2:45:51multiple constructors within the same
  4499. 2:45:53class much like that video on overloaded
  4500. 2:45:55methods
  4501. 2:45:56so with overloaded constructors multiple
  4502. 2:45:59constructors can have the same name
  4503. 2:46:01but they need a different set of
  4504. 2:46:02parameters because
  4505. 2:46:04a method's name plus its parameters
  4506. 2:46:06equals a unique method signature
  4507. 2:46:08and each method must have their own
  4508. 2:46:10unique signature
  4509. 2:46:12so let's create a class named pizza
  4510. 2:46:17and there's going to be four fields
  4511. 2:46:19let's have string bread
  4512. 2:46:20for the type of bread we would like for
  4513. 2:46:22our pizza like is it flat bread
  4514. 2:46:24stuffed crust plain
  4515. 2:46:28a field for sauce cheese
  4516. 2:46:31and topping
  4517. 2:46:35then let's create a constructor
  4518. 2:46:38public pizza
  4519. 2:46:41and then we need to set up the
  4520. 2:46:43parameters
  4521. 2:46:45string bread string sauce
  4522. 2:46:49string cheese and
  4523. 2:46:52string topping and then we'll need to
  4524. 2:46:54assign these
  4525. 2:46:56this dot bread equals bread
  4526. 2:46:59this dot sauce equals sauce
  4527. 2:47:03this dot cheese equals cheese
  4528. 2:47:07and this dot topping equals
  4529. 2:47:11topping all right then let's construct a
  4530. 2:47:15pizza object
  4531. 2:47:18pizza let's call this pizza all lower
  4532. 2:47:20case
  4533. 2:47:21equals new pizza then we need to pass
  4534. 2:47:25in something for the bread the sauce the
  4535. 2:47:27cheese and the topping
  4536. 2:47:28because we can't construct a pizza
  4537. 2:47:31object without
  4538. 2:47:32bread sauce cheese and a topping we need
  4539. 2:47:35all four
  4540. 2:47:36in order to construct a pizza object so
  4541. 2:47:39let's pass in something for the bread
  4542. 2:47:41let's say i would like a stuffed crust
  4543. 2:47:43pizza stuffed crust
  4544. 2:47:45for the sauce let's say red sauce
  4545. 2:47:49and the cheese what about mozzarella i
  4546. 2:47:52don't remember how to spell mozzarella
  4547. 2:47:55mozzarella i'm taking a guess here
  4548. 2:47:58and a topping pepperoni
  4549. 2:48:02so we can construct a pizza object just
  4550. 2:48:05fine right
  4551. 2:48:06but what if we would like a pizza
  4552. 2:48:08without any toppings like a plain cheese
  4553. 2:48:10pizza
  4554. 2:48:10so i'm going to get rid of that topping
  4555. 2:48:12well we can't construct a pizza without
  4556. 2:48:14a topping we need to add something
  4557. 2:48:16because we only have one constructor set
  4558. 2:48:18up and we need to pass in something for
  4559. 2:48:20a topping
  4560. 2:48:21so one way in which we can solve this
  4561. 2:48:23problem is to have multiple constructors
  4562. 2:48:26overloaded constructors we can pass in a
  4563. 2:48:29varying amount of arguments
  4564. 2:48:30so if we would like a cheese pizza
  4565. 2:48:32without any toppings
  4566. 2:48:33we can use that specific constructor
  4567. 2:48:35that will construct a
  4568. 2:48:37plain cheese pizza so let's copy this
  4569. 2:48:39constructor
  4570. 2:48:40they can have the same name but they
  4571. 2:48:42need a different set of parameters
  4572. 2:48:44so i'm going to remove this topping
  4573. 2:48:46parameter
  4574. 2:48:47and we will not assign this dot topping
  4575. 2:48:50equals topping
  4576. 2:48:51and we can now create a plain cheese
  4577. 2:48:54pizza
  4578. 2:48:54and let's do the same thing with i don't
  4579. 2:48:56know maybe somebody doesn't want cheese
  4580. 2:48:58on their pizza
  4581. 2:49:01so pizza with only bread and sauce
  4582. 2:49:07and then another pizza with only bread
  4583. 2:49:10i'm not even really sure if it's
  4584. 2:49:11considered a pizza then maybe it's
  4585. 2:49:12garlic bread or something
  4586. 2:49:13but i don't know though we can now
  4587. 2:49:15create a pizza object that has
  4588. 2:49:17no toppings no cheese and no sauce
  4589. 2:49:21so by using overload constructors we can
  4590. 2:49:23construct objects with
  4591. 2:49:25varying amounts of fields maybe we would
  4592. 2:49:27like toppings
  4593. 2:49:28maybe we don't well we have a choice now
  4594. 2:49:31so yeah that's overloaded constructors
  4595. 2:49:33it's a technique to create
  4596. 2:49:34multiple constructors with a different
  4597. 2:49:37set of parameters
  4598. 2:49:38they can share the same name but you
  4599. 2:49:39need a different set of parameters
  4600. 2:49:42because a constructor's name plus its
  4601. 2:49:44parameters
  4602. 2:49:45equals a constructor's signature and
  4603. 2:49:47each constructor needs a unique
  4604. 2:49:49signature so yeah those are overloaded
  4605. 2:49:52constructors in c
  4606. 2:49:53sharp if this video helped you out help
  4607. 2:49:55me out by smashing that like button
  4608. 2:49:57leave a random comment down below and
  4609. 2:49:58well yeah those are overloaded
  4610. 2:50:00constructors
  4611. 2:50:01in c sharp
  4612. 2:50:06all right what's going on people
  4613. 2:50:07inheritance so inheritance is this
  4614. 2:50:10programming concept
  4615. 2:50:11of one or more child classes receiving
  4616. 2:50:14fields methods
  4617. 2:50:15etc from a common parent kind of like
  4618. 2:50:18how people
  4619. 2:50:19inherit genes from their parents well
  4620. 2:50:21with programming we can kind of do
  4621. 2:50:22something similar
  4622. 2:50:23parent classes can have children classes
  4623. 2:50:26and anything
  4624. 2:50:27that the parent class has the child
  4625. 2:50:29classes now have
  4626. 2:50:31so here's an example let's create a
  4627. 2:50:32parent class named
  4628. 2:50:34vehicle not any kind of specific vehicle
  4629. 2:50:36just a generic
  4630. 2:50:37vehicle class class vehicle and
  4631. 2:50:41there will be one field let's say public
  4632. 2:50:44and speed and i'll set this equal to
  4633. 2:50:47maybe zero and let's create a method
  4634. 2:50:50public void go all vehicles can go
  4635. 2:50:52somewhere
  4636. 2:50:54and all we'll display is
  4637. 2:50:57this vehicle is moving
  4638. 2:51:00we can inherit this speed field and this
  4639. 2:51:03go method from the vehicle class
  4640. 2:51:05so this vehicle class is going to have
  4641. 2:51:07children so let's create a car class
  4642. 2:51:09a bike class and a boat class so type
  4643. 2:51:12class
  4644. 2:51:13car then to inherit from another class
  4645. 2:51:15type
  4646. 2:51:16after the class name colon and then the
  4647. 2:51:19parent in which you would like to
  4648. 2:51:20inherit from
  4649. 2:51:21so car is going to be the child class
  4650. 2:51:24vehicle will be the parent class
  4651. 2:51:26so car will have access to its own speed
  4652. 2:51:29and go method
  4653. 2:51:30and then you can add anything additional
  4654. 2:51:32within the car class like number of
  4655. 2:51:34wheels
  4656. 2:51:35public and wheels and i'll set this to 4
  4657. 2:51:38for all
  4658. 2:51:38car objects now let's create a bicycle
  4659. 2:51:41class
  4660. 2:51:42it's the same process as before class
  4661. 2:51:45bicycle and this will inherit from the
  4662. 2:51:49vehicle class as well but let's change
  4663. 2:51:51wheels to two and then let's create a
  4664. 2:51:53book class
  4665. 2:51:54class boat and here it's from vehicle
  4666. 2:51:57wheels
  4667. 2:51:58zero because well it's a boat okay so
  4668. 2:52:00let's instantiate a car
  4669. 2:52:02bicycle and boat object so let's do that
  4670. 2:52:05here
  4671. 2:52:06car car equals new car
  4672. 2:52:11bicycle bicycle
  4673. 2:52:14keep on spilling bicycle wrong bicycle
  4674. 2:52:18equals new bicycle
  4675. 2:52:21and boat boat equals new boat
  4676. 2:52:25so if we take a look at this car object
  4677. 2:52:27car
  4678. 2:52:28dot there is a speed field as well as a
  4679. 2:52:31go method even though there's nothing
  4680. 2:52:33within
  4681. 2:52:34this car class besides wheels so since
  4682. 2:52:36car is the child class
  4683. 2:52:38and vehicle is the parent class car has
  4684. 2:52:41access to a go method and a speed field
  4685. 2:52:43even though there's nothing within here
  4686. 2:52:45except a wheels field
  4687. 2:52:46and of course car objects do have a
  4688. 2:52:48wheels field as well
  4689. 2:52:50which we stand within the car class so
  4690. 2:52:52let's just test these
  4691. 2:52:53i'm going to display car dot speed
  4692. 2:52:57car dot speed followed by
  4693. 2:53:02car dot wheels and i will have this car
  4694. 2:53:06use
  4695. 2:53:06its go method car.go
  4696. 2:53:10all right so our car speed is zero
  4697. 2:53:12that's because we set it to be zero
  4698. 2:53:14within the vehicle class
  4699. 2:53:16this car has four wheels which we stated
  4700. 2:53:18within the car class
  4701. 2:53:19and this car class has a go method that
  4702. 2:53:22states this vehicle is moving
  4703. 2:53:24so i bet the same thing applies to our
  4704. 2:53:26bicycle and boat object
  4705. 2:53:28so let's copy these three lines of code
  4706. 2:53:30change car to
  4707. 2:53:31bicycle bicycle.speed bicycle.wheels
  4708. 2:53:35and bicycle.go and the same thing
  4709. 2:53:37applies to both
  4710. 2:53:38boat dot speed boat dot wheels boat dot
  4711. 2:53:41go
  4712. 2:53:43so all three of these different types of
  4713. 2:53:45vehicles each have
  4714. 2:53:46a speed field a wheels field and a go
  4715. 2:53:50method
  4716. 2:53:50so the point of inheritance is that we
  4717. 2:53:52can reuse code
  4718. 2:53:54i mean we could just copy these fields
  4719. 2:53:56and methods and then paste them within
  4720. 2:53:58each of these classes
  4721. 2:53:59but imagine that you have hundreds of
  4722. 2:54:00different classes and we need to change
  4723. 2:54:02like go to start well it's a lot easier
  4724. 2:54:06just to make that change in one place
  4725. 2:54:07than having to manually go through all
  4726. 2:54:09of our classes
  4727. 2:54:10so yeah that's kind of the benefit of
  4728. 2:54:11inheritance we can keep
  4729. 2:54:13anything that these classes have in
  4730. 2:54:15common within a common parent class
  4731. 2:54:17and these classes can simply just
  4732. 2:54:19inherit these fields and methods
  4733. 2:54:21so yeah that's inheritance if you liked
  4734. 2:54:23this video be sure to smash that like
  4735. 2:54:25button
  4736. 2:54:26drop a random comment down below and
  4737. 2:54:28well yeah that's inheritance in c sharp
  4738. 2:54:34alright what's going on abstract classes
  4739. 2:54:36abstract is a keyword that we can apply
  4740. 2:54:39to classes properties and methods but in
  4741. 2:54:42this video we're going to focus on
  4742. 2:54:43abstract classes now you can apply the
  4743. 2:54:46abstract keyword it's a modifier
  4744. 2:54:48to a class and that indicates that this
  4745. 2:54:51class
  4746. 2:54:51is missing components or is an
  4747. 2:54:53incomplete implementation
  4748. 2:54:55so let's take a look we're using
  4749. 2:54:56inheritance here so i have a class
  4750. 2:54:58vehicle vehicle has a speed field as
  4751. 2:55:01well as a go method
  4752. 2:55:03and there's three children classes car
  4753. 2:55:05bicycle and
  4754. 2:55:06boat and these are complete
  4755. 2:55:08implementations
  4756. 2:55:09so car has four wheels and a max speed
  4757. 2:55:11of 500
  4758. 2:55:12bicycle has two wheels a max speed of 50
  4759. 2:55:15and
  4760. 2:55:16boat has zero wheels and a max speed of
  4761. 2:55:17100 i just made up some numbers for the
  4762. 2:55:19max speed
  4763. 2:55:20i can create a car object bicycle object
  4764. 2:55:23and boat object just fine
  4765. 2:55:24these are complete implementations but
  4766. 2:55:27unfortunately i can also create a
  4767. 2:55:29vehicle object
  4768. 2:55:30vehicle vehicle equals new vehicle
  4769. 2:55:33so vehicle is incomplete it's missing a
  4770. 2:55:36number of wheels and a max speed
  4771. 2:55:38so imagine that we're having a race and
  4772. 2:55:40somebody decides to pick
  4773. 2:55:42the vehicle but we want somebody to pick
  4774. 2:55:44a specific
  4775. 2:55:45implementation of a vehicle like a car
  4776. 2:55:48bicycle or boat
  4777. 2:55:49i would like to prevent people from
  4778. 2:55:51creating generic vehicle objects
  4779. 2:55:53so this is kind of like imagine somebody
  4780. 2:55:55picks the invisible vehicle in like a
  4781. 2:55:57racing game or something
  4782. 2:55:59one way in which i can prevent people
  4783. 2:56:00from instantiating vehicle objects
  4784. 2:56:02because it's not finished it's an
  4785. 2:56:04incomplete implementation
  4786. 2:56:06is to precede this class definition with
  4787. 2:56:08this abstract keyword
  4788. 2:56:10so if i precede class with abstract i
  4789. 2:56:13can no longer create a
  4790. 2:56:14vehicle object cannot create an instance
  4791. 2:56:17of the abstract type
  4792. 2:56:18or interface vehicle so that adds a
  4793. 2:56:21little bit of security to our programs
  4794. 2:56:23if there's a class that you do not want
  4795. 2:56:24to be able to instantiate objects from
  4796. 2:56:27you can just precede that class
  4797. 2:56:28definition with the abstract keyword
  4798. 2:56:31and you cannot create an object from
  4799. 2:56:33that class
  4800. 2:56:34and that's basically what the abstract
  4801. 2:56:35modifier does we can indicate that a
  4802. 2:56:38class
  4803. 2:56:38we can also apply this to properties and
  4804. 2:56:40methods as well but that's a video for
  4805. 2:56:42another day
  4806. 2:56:43so when we precede a class definition
  4807. 2:56:46with abstract
  4808. 2:56:47we are indicating that that class is
  4809. 2:56:49missing components
  4810. 2:56:50or is incomplete so we should not be
  4811. 2:56:52able to instantiate
  4812. 2:56:54objects from this class which adds a
  4813. 2:56:56little bit of security so yeah
  4814. 2:56:57that's the abstract keyword if you found
  4815. 2:56:59this video helpful help me out by
  4816. 2:57:01smashing that like button
  4817. 2:57:02leave a random comment down below and
  4818. 2:57:03well yeah that's the abstract modifier
  4819. 2:57:06in c-sharp
  4820. 2:57:10all right what's going on people so in
  4821. 2:57:12this video i'm going to show you all how
  4822. 2:57:14we can create an array of objects
  4823. 2:57:15i have a class car cars have one field a
  4824. 2:57:18model field
  4825. 2:57:19and a constructor to assign an argument
  4826. 2:57:21to this
  4827. 2:57:22model field so let's create some car
  4828. 2:57:24objects car
  4829. 2:57:25car one equals new car then pass in a
  4830. 2:57:29model of your choosing
  4831. 2:57:30let's say that car one will be a mustang
  4832. 2:57:33and let's create maybe two more cars
  4833. 2:57:36car two and car three car two will be
  4834. 2:57:39a corvette and car three will be a lambo
  4835. 2:57:43so we'll need an array to work with too
  4836. 2:57:46so in the past when we've created arrays
  4837. 2:57:48we will list the data type of what we're
  4838. 2:57:50storing within our array like strings
  4839. 2:57:52ins whatever so after the data type add
  4840. 2:57:55a set of
  4841. 2:57:55straight brackets but the data type is
  4842. 2:57:58going to be the
  4843. 2:57:59data type of the objects that we're
  4844. 2:58:00working with since we're working with
  4845. 2:58:02car objects
  4846. 2:58:03we'll change that to car car straight
  4847. 2:58:06brackets
  4848. 2:58:06then we need a name for this array let's
  4849. 2:58:09say garage it's kind of like we're
  4850. 2:58:10parking cars within a garage
  4851. 2:58:12equals new the data type again straight
  4852. 2:58:15brackets
  4853. 2:58:15then a size for this array let's say
  4854. 2:58:18three then we will assign
  4855. 2:58:20some of these indexes with an object so
  4856. 2:58:22let's begin with garage at index
  4857. 2:58:24zero this will equal let's say car one
  4858. 2:58:27it's as if car one is parking within our
  4859. 2:58:30garage at
  4860. 2:58:31this parking spot number of zero then
  4861. 2:58:33let's park
  4862. 2:58:34car two and car three so garage at index
  4863. 2:58:37of one
  4864. 2:58:38equals car two garage at index of two
  4865. 2:58:41equals car three then let's display
  4866. 2:58:43these
  4867. 2:58:44console.right console.writeline garage
  4868. 2:58:47at index of zero so let me just show you
  4869. 2:58:50what this looks like first we're not
  4870. 2:58:51accessing the model quite yet we're just
  4871. 2:58:53displaying whatever's within garage at
  4872. 2:58:55index of zero and what we get is the
  4873. 2:58:58namespace
  4874. 2:58:59followed by the data type of our object
  4875. 2:59:02so if we need
  4876. 2:59:03the model of this object whatever's
  4877. 2:59:04within garage index of zero
  4878. 2:59:06i'm going to follow this with dot model
  4879. 2:59:10and that will display my car's model
  4880. 2:59:13which is a mustang
  4881. 2:59:14then let's do the same thing for garage
  4882. 2:59:16at index of one and two
  4883. 2:59:18so garage index of one garage index of
  4884. 2:59:21two
  4885. 2:59:21then make sure to follow those with dot
  4886. 2:59:23model
  4887. 2:59:24so within our garage between indexes 0
  4888. 2:59:27through
  4889. 2:59:282 we have a mustang corvette and a lambo
  4890. 2:59:31now another way in which we could
  4891. 2:59:32display these is with a for each loop
  4892. 2:59:35for each parentheses curly braces
  4893. 2:59:39and with our for each loop we have to
  4894. 2:59:41list the data type
  4895. 2:59:42of whatever's within our array car
  4896. 2:59:46and let's call each car just car car car
  4897. 2:59:49in garage then during each iteration we
  4898. 2:59:53will display
  4899. 2:59:54car dot model and that will do the same
  4900. 2:59:58thing
  4901. 2:59:58however this time we will iterate
  4902. 3:00:00through our array
  4903. 3:00:02now there's another way in which we
  4904. 3:00:03could write this too we don't need to
  4905. 3:00:05necessarily
  4906. 3:00:06define a size for our array then create
  4907. 3:00:08some named car objects
  4908. 3:00:10and then assign those objects directly
  4909. 3:00:12to each index
  4910. 3:00:13we could just declare an array and then
  4911. 3:00:15immediately instantiate some objects all
  4912. 3:00:17in one step
  4913. 3:00:18so we're going to create a new array car
  4914. 3:00:20straight brackets i will call this
  4915. 3:00:22garage
  4916. 3:00:23equals curly braces and then we will
  4917. 3:00:26pass in
  4918. 3:00:27just new car then you need to pass in a
  4919. 3:00:29model
  4920. 3:00:32and do the same thing with your other
  4921. 3:00:33two cars
  4922. 3:00:36okay this will do the same thing it's
  4923. 3:00:38just written in less steps
  4924. 3:00:41so these are known as anonymous objects
  4925. 3:00:43they don't technically have a name
  4926. 3:00:44so i may or may not make a separate
  4927. 3:00:46video on anonymous objects
  4928. 3:00:48so within our garage we should have a
  4929. 3:00:50mustang corvette
  4930. 3:00:52and a lambo so yeah that's how to create
  4931. 3:00:54an array of objects
  4932. 3:00:56in c sharp
  4933. 3:00:59hey what's going on people in this video
  4934. 3:01:01i'm going to show you all how we can
  4935. 3:01:03pass
  4936. 3:01:03an object as an argument so here's the
  4937. 3:01:05rundown i have a class car
  4938. 3:01:07cars have two fields a model and a color
  4939. 3:01:10and a constructor to assign a model and
  4940. 3:01:12color argument that we receive
  4941. 3:01:14i have one car object car car one equals
  4942. 3:01:17new car
  4943. 3:01:18the model of this car is a mustang and
  4944. 3:01:20the color is red
  4945. 3:01:21so i'm going to create a method that
  4946. 3:01:23will change the color of my car
  4947. 3:01:25but i have to pass in a car object so
  4948. 3:01:28let's create a method to handle that for
  4949. 3:01:30us
  4950. 3:01:31public static void it's not returning
  4951. 3:01:34anything and let's call this method
  4952. 3:01:36change color and we'll need two
  4953. 3:01:39parameters
  4954. 3:01:40a car that we would like to change the
  4955. 3:01:42color of so the data type is going to be
  4956. 3:01:45car as well as a name for this parameter
  4957. 3:01:47let's name whatever car object that we
  4958. 3:01:49receive as just
  4959. 3:01:51simply car and we need a color as well
  4960. 3:01:53that would be a string
  4961. 3:01:54string color then to change the color of
  4962. 3:01:58this car
  4963. 3:01:59i would type car dot color
  4964. 3:02:02equals whatever color that we receive
  4965. 3:02:05and that's it
  4966. 3:02:06so now when we invoke this method we
  4967. 3:02:08have to pass in a car object as well as
  4968. 3:02:10a color
  4969. 3:02:12so that would be change color and the
  4970. 3:02:15car object
  4971. 3:02:16that i will pass in as an argument is
  4972. 3:02:18car 1 the name of the object
  4973. 3:02:20as well as a new color let's color this
  4974. 3:02:22car silver
  4975. 3:02:23and then let's display our cars color
  4976. 3:02:26and
  4977. 3:02:26model console.right
  4978. 3:02:30car one dot color
  4979. 3:02:34plus a lot of space plus car one
  4980. 3:02:38dot model and this should change the
  4981. 3:02:40color of our car
  4982. 3:02:41to a silver mustang so to pass an object
  4983. 3:02:45as an argument
  4984. 3:02:46you need to make sure that you have the
  4985. 3:02:47right parameter set up you type
  4986. 3:02:49the data type of this object followed by
  4987. 3:02:52a name for this parameter
  4988. 3:02:54then when you invoke this method you
  4989. 3:02:55have to pass in the name of the object
  4990. 3:02:57let's try something a little bit
  4991. 3:02:58different let's create a method to
  4992. 3:03:00copy an object so let's get rid of this
  4993. 3:03:03change color method but we'll keep our
  4994. 3:03:05car object
  4995. 3:03:07so we'll create a method to return a car
  4996. 3:03:10object so public static
  4997. 3:03:14instead of void the data type is the
  4998. 3:03:17type that we're returning we're
  4999. 3:03:18returning a car
  5000. 3:03:20object so replace void with car
  5001. 3:03:23then we need a name for this method
  5002. 3:03:25let's call this copy
  5003. 3:03:26so we will pass in a car object the data
  5004. 3:03:29type
  5005. 3:03:29is car and i will name this argument
  5006. 3:03:32that we receive
  5007. 3:03:33as car now one thing that we can do to
  5008. 3:03:36copy a car object
  5009. 3:03:37is return a new
  5010. 3:03:40car and then we'll need to pass in
  5011. 3:03:43a model and a color so the model
  5012. 3:03:47of this car is car dot model and the
  5013. 3:03:50color
  5014. 3:03:51is car dot color
  5015. 3:03:54now if i would like to copy car 1 and
  5016. 3:03:57create a second car
  5017. 3:03:58a copy of it i can type car car 2
  5018. 3:04:02equals copy and then i will pass
  5019. 3:04:05this object as an argument and then
  5020. 3:04:08let's display the fields
  5021. 3:04:09of car2 and i bet it's a red mustang
  5022. 3:04:13oh yeah there we go so we successfully
  5023. 3:04:15created a copy of car one
  5024. 3:04:18by returning a car object so yeah that's
  5025. 3:04:21how to pass objects as
  5026. 3:04:22arguments you just have to list the data
  5027. 3:04:24type of the object
  5028. 3:04:26since we're working with cars we had to
  5029. 3:04:27set up a cart parameter
  5030. 3:04:29then if you would like to return an
  5031. 3:04:31object instead of void you would replace
  5032. 3:04:33void with the data type of the object
  5033. 3:04:35you would like to return
  5034. 3:04:37and if you would like to return a new
  5035. 3:04:39object like what we did with this copy
  5036. 3:04:41method
  5037. 3:04:42you would return a new and then call the
  5038. 3:04:45constructor of whatever object you would
  5039. 3:04:47like to create and return
  5040. 3:04:48so that's why we called the car
  5041. 3:04:50constructor and then we returned that
  5042. 3:04:52new car
  5043. 3:04:53back to this spot here so it's kind of
  5044. 3:04:55like we replaced this method with
  5045. 3:04:58calling the car constructor something
  5046. 3:05:00like that so yeah that's how to pass
  5047. 3:05:02objects as arguments as well as return
  5048. 3:05:05objects from methods too
  5049. 3:05:07so yeah if this video helped you out
  5050. 3:05:08feel free to help me out by smashing
  5051. 3:05:10that like button
  5052. 3:05:11leave a random comment down below and
  5053. 3:05:13subscribe if you'd like to become a
  5054. 3:05:14fellow bro
  5055. 3:05:18right what's going on people we're
  5056. 3:05:19talking about method overriding
  5057. 3:05:21method overriding provides a new version
  5058. 3:05:24of a method
  5059. 3:05:25inherited from a parent class i have a
  5060. 3:05:27class animal this is a parent class
  5061. 3:05:29also known as a super class or base
  5062. 3:05:31class and i have
  5063. 3:05:33two child classes dog inherits from
  5064. 3:05:35animal and
  5065. 3:05:36cat inherits from animal so let's say
  5066. 3:05:38that we have a method within
  5067. 3:05:39our animal class so let's create one
  5068. 3:05:42public
  5069. 3:05:43void speak all animals can speak
  5070. 3:05:48and then when a class that inherits this
  5071. 3:05:50method
  5072. 3:05:51uses this method let's display a message
  5073. 3:05:54such as
  5074. 3:05:55the animal goes
  5075. 3:06:00okay let's create a dog object and a cat
  5076. 3:06:03object
  5077. 3:06:04dog dog equals new dog
  5078. 3:06:08cat cat equals new cat
  5079. 3:06:13dogs since they're inheriting from the
  5080. 3:06:16animal class they have a speak method
  5081. 3:06:18as well as cats cat dot speak
  5082. 3:06:22so when both this dog object and this
  5083. 3:06:24cat object
  5084. 3:06:25invoke their speak methods that they
  5085. 3:06:27inherit from the parent class of animal
  5086. 3:06:30we will execute this method and when my
  5087. 3:06:32dog
  5088. 3:06:33and cat object speak that will display
  5089. 3:06:36the animal goes
  5090. 3:06:37burr okay so we can actually provide
  5091. 3:06:40our own implementation of this method we
  5092. 3:06:43can override this method and do
  5093. 3:06:45something else
  5094. 3:06:46now to overwrite a method it needs the
  5095. 3:06:47same method signature
  5096. 3:06:49the same name and the same parameters so
  5097. 3:06:51if i would like to
  5098. 3:06:52override this speak method within my dog
  5099. 3:06:55class i'm going
  5100. 3:06:56to type public override
  5101. 3:07:01void the same type as the original
  5102. 3:07:03method
  5103. 3:07:04and the same name and signature now to
  5104. 3:07:07create an overriding method
  5105. 3:07:08the method that is inherited must be
  5106. 3:07:11virtual
  5107. 3:07:12abstract or already overridden so i'm
  5108. 3:07:15going to add this modifier
  5109. 3:07:17virtual so we can now override this
  5110. 3:07:19method
  5111. 3:07:20you can also do this with an abstract
  5112. 3:07:22method too
  5113. 3:07:24but the class needs to be abstract as
  5114. 3:07:26well
  5115. 3:07:27and there needs to be no body
  5116. 3:07:30but we'll just stick with virtual in
  5117. 3:07:32this video
  5118. 3:07:35okay so we have our virtual method that
  5119. 3:07:37means that
  5120. 3:07:38this method can be overridden and now we
  5121. 3:07:40can provide
  5122. 3:07:41our own implementation of the speak
  5123. 3:07:43method specifically for
  5124. 3:07:45dogs so let's display a different
  5125. 3:07:47message
  5126. 3:07:48console.rightline the dog
  5127. 3:07:51goes woof so notice that we don't have
  5128. 3:07:55anything within
  5129. 3:07:56our cat class now if both dog and cat
  5130. 3:07:59use their speak methods
  5131. 3:08:01the dog will instead speak and say the
  5132. 3:08:04dog goes woof
  5133. 3:08:05and our cat is still using the method
  5134. 3:08:07that inherits from its parent animal
  5135. 3:08:10if i would like to do the same thing for
  5136. 3:08:12my cat objects i can override this
  5137. 3:08:14method again
  5138. 3:08:15public override void
  5139. 3:08:19speak and then i can provide a different
  5140. 3:08:23implementation
  5141. 3:08:25the cat goes meow
  5142. 3:08:30so after both dog and cat use their
  5143. 3:08:33speak methods
  5144. 3:08:34this displays the dog goes woof the cat
  5145. 3:08:37goes meow
  5146. 3:08:38so by using method overriding we can
  5147. 3:08:40provide a new version of a method
  5148. 3:08:42inherited from a parent class however
  5149. 3:08:45the inheritance method
  5150. 3:08:46must be abstract virtual or
  5151. 3:08:49already overwritten and a method
  5152. 3:08:51overriding is commonly used with
  5153. 3:08:53the tostring method which we still need
  5154. 3:08:55to talk about
  5155. 3:08:56and polymorphism so yeah that's method
  5156. 3:08:59overriding
  5157. 3:09:00if you found this video helpful please
  5158. 3:09:01smash that like button leave a random
  5159. 3:09:03comment down below
  5160. 3:09:04and subscribe if you'd like to become a
  5161. 3:09:06fellow bro
  5162. 3:09:09all right welcome back everybody the two
  5163. 3:09:11string method the two string method
  5164. 3:09:13is an inherent method of objects its job
  5165. 3:09:16is to convert an object
  5166. 3:09:18to its string representation so that it
  5167. 3:09:20is suitable for display
  5168. 3:09:22so i have a class car cars have four
  5169. 3:09:25fields i make a model a year and a color
  5170. 3:09:27and i have a constructor to assign some
  5171. 3:09:30of these fields
  5172. 3:09:31so what we could do if we would like
  5173. 3:09:32some of this information we can override
  5174. 3:09:34the built-in tostring method of objects
  5175. 3:09:37that includes cars 2.
  5176. 3:09:39so let's create a car object car car
  5177. 3:09:42equals new card then i need to pass in a
  5178. 3:09:45make
  5179. 3:09:46model year and color let's say that this
  5180. 3:09:48is a
  5181. 3:09:49chevy corvette
  5182. 3:09:53the year is 2022 and the color
  5183. 3:09:56is blue so within a writeline statement
  5184. 3:10:00if i type the name of my object
  5185. 3:10:02car there's actually a tostring method
  5186. 3:10:05and when we invoke this method let's
  5187. 3:10:07take a look to see what happens
  5188. 3:10:09so this displays the namespace followed
  5189. 3:10:12by
  5190. 3:10:12the type of the object that we're
  5191. 3:10:14working with so we can actually
  5192. 3:10:16override this method so that when we use
  5193. 3:10:18the tostring method
  5194. 3:10:19we can return a string representation of
  5195. 3:10:22this object
  5196. 3:10:23maybe we can display the make model year
  5197. 3:10:25in color somehow
  5198. 3:10:26so we'll need to override this method to
  5199. 3:10:29string
  5200. 3:10:30so let's do that here we're going to
  5201. 3:10:32type
  5202. 3:10:33public then to override a method we have
  5203. 3:10:35to use that override keyword
  5204. 3:10:38then type to string and this might
  5205. 3:10:41auto-generate for you
  5206. 3:10:42so we're returning a string and by
  5207. 3:10:45default this will return
  5208. 3:10:46base to string what we can do instead is
  5209. 3:10:49return something else we can return
  5210. 3:10:51some sort of string representation
  5211. 3:10:53that's suitable for display
  5212. 3:10:55let's say that when i use the tostring
  5213. 3:10:57method of my car object
  5214. 3:10:59i'd like to return a sentence that says
  5215. 3:11:01this is a
  5216. 3:11:02make plus model a chevy corvette so
  5217. 3:11:05let's do something like this
  5218. 3:11:07let's say string
  5219. 3:11:10message equals this
  5220. 3:11:13is a plus make
  5221. 3:11:17then i'll just add a space plus model
  5222. 3:11:20really you can do whatever you want and
  5223. 3:11:22then i'm going to return
  5224. 3:11:23this string or i could take all of this
  5225. 3:11:27copy it and then return
  5226. 3:11:33this is a make plus model i could do
  5227. 3:11:35that too and that would work
  5228. 3:11:37so when i use the tostring method it's
  5229. 3:11:39going to return this sentence
  5230. 3:11:40and insert make and model this is a
  5231. 3:11:43chevy corvette
  5232. 3:11:45one thing you can do too you don't
  5233. 3:11:46necessarily need to directly invoke the
  5234. 3:11:49tostring method
  5235. 3:11:50you can just type the name of the object
  5236. 3:11:52and that will do the same thing
  5237. 3:11:54so this will behind the scenes called
  5238. 3:11:55the tostring method
  5239. 3:11:57so before when we displayed our object
  5240. 3:12:00with a writeline statement it would
  5241. 3:12:01display the namespace
  5242. 3:12:02plus the type of the object but if you
  5243. 3:12:04were to override
  5244. 3:12:06the tostring method you can return some
  5245. 3:12:08sort of sentence or string
  5246. 3:12:10representation of your object
  5247. 3:12:11that is suitable for display so yeah
  5248. 3:12:14that is the
  5249. 3:12:15tostring method it converts an object to
  5250. 3:12:18a string representation
  5251. 3:12:19that is suitable for display and really
  5252. 3:12:21you can type whatever you want here just
  5253. 3:12:23be sure to return a string
  5254. 3:12:24so yeah that is the two string method if
  5255. 3:12:27you found this video helpful please be
  5256. 3:12:28sure to give it a thumbs up drop a
  5257. 3:12:30random comment down below and subscribe
  5258. 3:12:32if you'd like to become a fellow bro
  5259. 3:12:34hey what's going on everybody it's you
  5260. 3:12:36bro hope you're doing well
  5261. 3:12:37and in this video i'm going to explain
  5262. 3:12:39to polymorphism in c sharp
  5263. 3:12:41so sit back relax and enjoy the show
  5264. 3:12:46all right what's going on people
  5265. 3:12:48polymorphism
  5266. 3:12:49polymorphism is a greek word that means
  5267. 3:12:52to have many forms
  5268. 3:12:53with programming objects can be
  5269. 3:12:55identified by more than one type
  5270. 3:12:57they can have many forms for example
  5271. 3:13:00let's say that we have a dog
  5272. 3:13:02a dog could be considered a canine an
  5273. 3:13:04animal
  5274. 3:13:05and an organism so a dog has more than
  5275. 3:13:07just
  5276. 3:13:08one form it has more than one data type
  5277. 3:13:10here's the situation we're going to have
  5278. 3:13:12a race
  5279. 3:13:13we'll enter in a car object bicycle
  5280. 3:13:15object and boat object
  5281. 3:13:17so i have four classes vehicle is the
  5282. 3:13:19base class
  5283. 3:13:20otherwise known as the parent class and
  5284. 3:13:23car bicycle and boat all inherit from
  5285. 3:13:25the vehicle class
  5286. 3:13:26so with my race i need to create a car
  5287. 3:13:28object bicycle object and
  5288. 3:13:30boat object so let's do that car car
  5289. 3:13:34equals new car
  5290. 3:13:37bicycle bicycle equals
  5291. 3:13:40new bicycle
  5292. 3:13:43and boat boat equals new boat
  5293. 3:13:47i would like to place all of these
  5294. 3:13:49objects within an array for my race
  5295. 3:13:51but what's the data type of the array
  5296. 3:13:53going to be so let's try and make an
  5297. 3:13:55array of cars and add our car
  5298. 3:13:57bicycle and boat and let's just see what
  5299. 3:13:59happens i will name this array
  5300. 3:14:01just vehicles equals and then within
  5301. 3:14:05curly braces
  5302. 3:14:06add whatever objects you would like i
  5303. 3:14:08would like to enter my car into the race
  5304. 3:14:10my bicycle and my boat
  5305. 3:14:13okay but we're running into some errors
  5306. 3:14:15here cannot implicitly convert
  5307. 3:14:17bicycle to car so i stated that the
  5308. 3:14:21data type of this array is going to be
  5309. 3:14:23cars i can only enter
  5310. 3:14:25car objects so i have no issue there but
  5311. 3:14:27i cannot enter in
  5312. 3:14:28my bicycle and boat objects into this
  5313. 3:14:31array
  5314. 3:14:31but one way in which we can solve this
  5315. 3:14:33is by using polymorphism
  5316. 3:14:36so we can find what they all have in
  5317. 3:14:38common they all
  5318. 3:14:39identify as vehicles because they all
  5319. 3:14:42inherit from the vehicle class
  5320. 3:14:44these objects have more than one form
  5321. 3:14:46they can be identified by more than one
  5322. 3:14:48type
  5323. 3:14:48so instead of my array being an array of
  5324. 3:14:50cars i'm going to change this
  5325. 3:14:52to vehicle because well these three
  5326. 3:14:55objects
  5327. 3:14:56identify as well a car a bicycle and a
  5328. 3:14:58boat respectively
  5329. 3:15:00but also vehicles because they inherit
  5330. 3:15:02from the vehicle class
  5331. 3:15:03so you can see that this is valid if you
  5332. 3:15:05need to create an array of
  5333. 3:15:07different types of objects you have to
  5334. 3:15:09find what they have in common
  5335. 3:15:10and then you can just use that as a data
  5336. 3:15:12type they are all also considered
  5337. 3:15:14vehicles
  5338. 3:15:15let's move on to part two what i would
  5339. 3:15:17like to do is have all of these vehicles
  5340. 3:15:19use a go method
  5341. 3:15:21so they will all begin the race at the
  5342. 3:15:23same time so let's create a go method
  5343. 3:15:25within each of these classes
  5344. 3:15:27public void go
  5345. 3:15:31and we'll just display the car
  5346. 3:15:35is moving place that within the car
  5347. 3:15:37class
  5348. 3:15:38let's make a go method for bicycles
  5349. 3:15:41change
  5350. 3:15:41car to bicycle
  5351. 3:15:44and then the boat is moving
  5352. 3:15:48okay now to iterate over this array of
  5353. 3:15:50vehicles
  5354. 3:15:51we can use a for each loop for each
  5355. 3:15:54now what's the data type of our vehicle
  5356. 3:15:57array
  5357. 3:15:57well it's vehicles vehicle vehicle
  5358. 3:16:01in vehicles and now i would like
  5359. 3:16:04each vehicle to use its go method
  5360. 3:16:08vehicle.go so each vehicle will use its
  5361. 3:16:11go method
  5362. 3:16:12but there's one more thing that we need
  5363. 3:16:14to do we need a go method within our
  5364. 3:16:17vehicle class
  5365. 3:16:18so all of these methods within our car
  5366. 3:16:21bicycle
  5367. 3:16:21and boat classes all are going to be
  5368. 3:16:24overriding
  5369. 3:16:25a go method found within the vehicle
  5370. 3:16:27class so let's create a
  5371. 3:16:29virtual method public
  5372. 3:16:32virtual void
  5373. 3:16:35go and we don't necessarily need to
  5374. 3:16:39define anything within the go method
  5375. 3:16:42because we're going to be overriding
  5376. 3:16:43this method okay
  5377. 3:16:45then we need to use this override
  5378. 3:16:48modifier
  5379. 3:16:52all right now when we iterate over this
  5380. 3:16:55array of vehicles
  5381. 3:16:56each vehicle will use its go method it's
  5382. 3:16:59as if when we would like to begin our
  5383. 3:17:01race
  5384. 3:17:01all of the vehicles within our array
  5385. 3:17:03will use their go methods
  5386. 3:17:05at the same time the car is moving the
  5387. 3:17:07bicycle is moving the boat is moving
  5388. 3:17:10so yeah that's polymorphism it's a greek
  5389. 3:17:12word that means to have many forms
  5390. 3:17:14objects can be identified by more than
  5391. 3:17:16one type
  5392. 3:17:18and in this example we created an array
  5393. 3:17:20of vehicles
  5394. 3:17:21our car identifies as a car but it also
  5395. 3:17:24identifies as a vehicle
  5396. 3:17:25and objects do identify as objects too
  5397. 3:17:28so cars identify as objects
  5398. 3:17:30and the same thing can apply with
  5399. 3:17:32bicycle and boats in our example
  5400. 3:17:34so yeah that's polymorphism if you found
  5401. 3:17:37this video helpful be sure to smash that
  5402. 3:17:39like button leave a random comment down
  5403. 3:17:40below
  5404. 3:17:41and subscribe if you'd like to become a
  5405. 3:17:43fellow bro
  5406. 3:17:46all right interfaces an interface is
  5407. 3:17:49much like a parent class
  5408. 3:17:51or base class it defines a sort of
  5409. 3:17:54contract
  5410. 3:17:54that all classes inheriting from need to
  5411. 3:17:57follow
  5412. 3:17:58an interface can contain properties
  5413. 3:18:00methods events
  5414. 3:18:01and if there's any of those declared
  5415. 3:18:03within an interface
  5416. 3:18:05anything that inherits from that
  5417. 3:18:06interface needs to be implemented within
  5418. 3:18:09an inheriting class
  5419. 3:18:10an interface declares what a class
  5420. 3:18:13should have
  5421. 3:18:14and the class that it inherits from
  5422. 3:18:16defines how it should do it exactly
  5423. 3:18:19so here's an example we'll create three
  5424. 3:18:21classes
  5425. 3:18:22rabbit hawk and fish and two interfaces
  5426. 3:18:26prey and predator
  5427. 3:18:33alright now let's create two interfaces
  5428. 3:18:36to create an interface you type
  5429. 3:18:38interface and then the name of the
  5430. 3:18:39interface however a common naming
  5431. 3:18:42convention with
  5432. 3:18:43interfaces is that you precede the
  5433. 3:18:45interface name
  5434. 3:18:46with capital i so if we would like a
  5435. 3:18:49prey interface
  5436. 3:18:50we would type i pray and then let's
  5437. 3:18:53create a predator interface
  5438. 3:18:55interface i predator
  5439. 3:18:58now with these interfaces we can pretty
  5440. 3:19:00much declare anything within these
  5441. 3:19:02interfaces but if one of these classes
  5442. 3:19:04would like to inherit from this
  5443. 3:19:06interface they need to implement
  5444. 3:19:08anything declared within them
  5445. 3:19:10so let's say that if you're prey then
  5446. 3:19:13you need a method to flee because you're
  5447. 3:19:15fleeing from predators
  5448. 3:19:17i'm going to declare a flea method void
  5449. 3:19:20flee we will declare it but not
  5450. 3:19:23implement it
  5451. 3:19:24implementing this method is the job of
  5452. 3:19:26the inheriting class
  5453. 3:19:27that would like to use this interface
  5454. 3:19:30let's say that
  5455. 3:19:30rabbits will inherit this interface i
  5456. 3:19:33pray
  5457. 3:19:34colon i pray and in order to do so we
  5458. 3:19:38need to implement
  5459. 3:19:39this method of fleet within the rabbit
  5460. 3:19:41class in order to use it
  5461. 3:19:44public void flea
  5462. 3:19:47and then be sure to add a body to this
  5463. 3:19:49method so when we invoke this method
  5464. 3:19:52let's write the rabbit runs away
  5465. 3:19:57okay now we can create a rabbit object
  5466. 3:20:01rabbit rabbit equals
  5467. 3:20:04new rabbit and i bet there's a flea
  5468. 3:20:08method
  5469. 3:20:08rabbit dot flea
  5470. 3:20:11and let's try it the rabbit runs away
  5471. 3:20:15now let's have our hawk class inherit
  5472. 3:20:17this predator interface
  5473. 3:20:19now with our predator interface let's
  5474. 3:20:21create a method
  5475. 3:20:23to hunt void hunt
  5476. 3:20:26we will declare it but not implemented
  5477. 3:20:28implementing this method is the job of
  5478. 3:20:30the classes inheriting
  5479. 3:20:31this interface so hawk will inherit the
  5480. 3:20:35i predator interface but we need to
  5481. 3:20:38implement
  5482. 3:20:39that hunt method public
  5483. 3:20:42void hunt
  5484. 3:20:46and then let's display the hawk
  5485. 3:20:49is searching for food
  5486. 3:20:52and we can create a hawk object hawk
  5487. 3:20:56hawk equals new hawk
  5488. 3:21:00so let's take a look at our hawk object
  5489. 3:21:03hawk
  5490. 3:21:03dot and there's no flea method because
  5491. 3:21:06we're not implementing
  5492. 3:21:07the prey interface we're implementing
  5493. 3:21:09the predator interface
  5494. 3:21:11so there is a hunt method and not a flea
  5495. 3:21:14method
  5496. 3:21:16hawk.hunt
  5497. 3:21:19the hawk is searching for food now with
  5498. 3:21:21interfaces
  5499. 3:21:22you can inherit more than one unlike
  5500. 3:21:24with standard inheritance
  5501. 3:21:25so fish they could be both prey and
  5502. 3:21:28predators
  5503. 3:21:29to inherit two interfaces after the
  5504. 3:21:31colon you can separate each with a comma
  5505. 3:21:34so fish will inherit the i prey and i
  5506. 3:21:37predator interfaces but now they need to
  5507. 3:21:40implement
  5508. 3:21:40both of these methods flea and hunt so
  5509. 3:21:43let's do so
  5510. 3:21:45public void flea
  5511. 3:21:51the fish swims away
  5512. 3:21:54and now we need to implement that hunt
  5513. 3:21:56method
  5514. 3:21:58public void hunt
  5515. 3:22:06the fish is searching
  5516. 3:22:09for smaller fish
  5517. 3:22:13all right now let's create a fish object
  5518. 3:22:16fish
  5519. 3:22:16fish equals new fish
  5520. 3:22:23so fish have both a flea and a hunt
  5521. 3:22:25method
  5522. 3:22:27fish dot flea fish dot hunt
  5523. 3:22:32the fish swims away the fish is
  5524. 3:22:34searching for smaller fish
  5525. 3:22:36so an interface defines a sort of
  5526. 3:22:38contract that all classes inheriting
  5527. 3:22:40from
  5528. 3:22:41should follow an interface declares what
  5529. 3:22:44a class
  5530. 3:22:44should have and the inheritance class
  5531. 3:22:47defines
  5532. 3:22:48how it should do it the benefits of
  5533. 3:22:50using interfaces
  5534. 3:22:51for one is that you can inherit multiple
  5535. 3:22:53interfaces
  5536. 3:22:55unlike with standard inheritance there's
  5537. 3:22:57added security
  5538. 3:22:59and this gives us a plug-and-play style
  5539. 3:23:01of coding because
  5540. 3:23:02anything that has the same interface
  5541. 3:23:05will definitely have the same
  5542. 3:23:06implemented methods and properties
  5543. 3:23:08so you can swap these out for like a
  5544. 3:23:10different creature if you would like
  5545. 3:23:12so yeah those are interfaces if you
  5546. 3:23:14found this video helpful please be sure
  5547. 3:23:15to give this video a thumbs up
  5548. 3:23:17leave a random comment down below and
  5549. 3:23:19subscribe if you'd like to become a
  5550. 3:23:20fellow bro
  5551. 3:23:24all right welcome back everybody a list
  5552. 3:23:27a list is a
  5553. 3:23:28data structure that represents a list of
  5554. 3:23:31objects
  5555. 3:23:31that can be accessed by index it's
  5556. 3:23:34similar to an array
  5557. 3:23:35but they can increase and decrease
  5558. 3:23:37dynamically in size during runtime
  5559. 3:23:40which rays cannot do so here's an
  5560. 3:23:42example of an array an array has a
  5561. 3:23:43static
  5562. 3:23:44fixed size and we cannot change it
  5563. 3:23:46normally after runtime
  5564. 3:23:48so let's create a standard array just
  5565. 3:23:50for this example let's create an array
  5566. 3:23:52of food
  5567. 3:23:52string food equals new
  5568. 3:23:56string and let's say that this array has
  5569. 3:23:58a size of three
  5570. 3:24:00so i'm going to add some elements to
  5571. 3:24:02this array
  5572. 3:24:11and then i can display these elements
  5573. 3:24:13with a symbol for each loop
  5574. 3:24:19pizza hamburger and hot dog so if i
  5575. 3:24:22attempt to add another element to this
  5576. 3:24:24array this is what happens
  5577. 3:24:29we'll run into an exception and index
  5578. 3:24:32out of range exception
  5579. 3:24:33because index was outside the bounds of
  5580. 3:24:35the array
  5581. 3:24:36so you can't normally change the size of
  5582. 3:24:38an array another option is to use a list
  5583. 3:24:40a list can increase and decrease
  5584. 3:24:42dynamically in size during runtime
  5585. 3:24:45so let's create a list step one is that
  5586. 3:24:48we'll need to import
  5587. 3:24:50system.collections.generic so include
  5588. 3:24:53this at the top i'll place it right
  5589. 3:24:54underneath
  5590. 3:24:55using system and this is how to declare
  5591. 3:24:58a list
  5592. 3:24:59list then within angle brackets list the
  5593. 3:25:02type of object you would like to store
  5594. 3:25:04strings and then i will name this list
  5595. 3:25:07food
  5596. 3:25:08equals new list
  5597. 3:25:12angle brackets the data type again
  5598. 3:25:16parentheses semicolon and we now have
  5599. 3:25:19a list named food and to add elements to
  5600. 3:25:22this list there is a built in add method
  5601. 3:25:25food dot add then within
  5602. 3:25:29this method pass in the object you would
  5603. 3:25:31like to store
  5604. 3:25:32i would like to store let's say a pizza
  5605. 3:25:35then a hamburger then a hot dog
  5606. 3:25:38so food dot add pizza hamburger
  5607. 3:25:42and hot dog
  5608. 3:25:46and let's take a look so we have our
  5609. 3:25:49pizza
  5610. 3:25:50hamburger and hot dog and i bet we can
  5611. 3:25:52add even more than three elements
  5612. 3:25:57so it appears that our list can resize
  5613. 3:25:59dynamically which is a bonus
  5614. 3:26:01then to access one of these elements
  5615. 3:26:03it's much like an array you use that set
  5616. 3:26:05of straight brackets
  5617. 3:26:06so i'm going to display whatever's
  5618. 3:26:08within element
  5619. 3:26:09number zero food index zero
  5620. 3:26:14and that would be pizza so accessing an
  5621. 3:26:17element is the same as an array you just
  5622. 3:26:19type the list name
  5623. 3:26:20and then add a set of straight brackets
  5624. 3:26:22then the index number okay so here's a
  5625. 3:26:24few other useful methods of lists
  5626. 3:26:27there is a remove method food dot remove
  5627. 3:26:31then type in what you would like to
  5628. 3:26:33remove i would like to remove
  5629. 3:26:34fries and let's see if that's still in
  5630. 3:26:37here
  5631. 3:26:39nope fries is missing pizza hamburger
  5632. 3:26:42hot dog
  5633. 3:26:43so that is the add method and remove
  5634. 3:26:45method but there's a few other useful
  5635. 3:26:47methods
  5636. 3:26:48so we can insert an object at a given
  5637. 3:26:50element
  5638. 3:26:51let's insert maybe sushi at index 1 via
  5639. 3:26:54the insert method food dot insert
  5640. 3:26:58then we need an index and an item
  5641. 3:27:02so at the beginning of our list at index
  5642. 3:27:050
  5643. 3:27:06i will insert sushi
  5644. 3:27:11sushi pizza hamburger hot dog fries
  5645. 3:27:14we can get the current size of our array
  5646. 3:27:17using the
  5647. 3:27:18count property so within a writeline
  5648. 3:27:20statement
  5649. 3:27:21i'm going to display food dot count
  5650. 3:27:24property and the current size
  5651. 3:27:28of our list is four so that is the count
  5652. 3:27:32property we can find the index of an
  5653. 3:27:35element
  5654. 3:27:37food dot index of
  5655. 3:27:41and let's find where pizza is
  5656. 3:27:46so that is at index zero you can also
  5657. 3:27:50find
  5658. 3:27:50the last index of a given item so what
  5659. 3:27:53if we have fries both
  5660. 3:27:54in the beginning and at the end food dot
  5661. 3:27:58add fries
  5662. 3:28:02so i have that in the beginning and at
  5663. 3:28:04the end so i'm going to use
  5664. 3:28:06the last index of method
  5665. 3:28:09food dot last index
  5666. 3:28:12of fries
  5667. 3:28:15and the last index surprise is at four
  5668. 3:28:19zero one two three four so let me get
  5669. 3:28:23rid of that okay we can check to see
  5670. 3:28:25if our list contains a given item
  5671. 3:28:28using the contains method food dot
  5672. 3:28:31contains pizza
  5673. 3:28:35this will return a boolean true if pizza
  5674. 3:28:38is within our list and false if not
  5675. 3:28:40pizza is within our list so that returns
  5676. 3:28:43true
  5677. 3:28:44and that is the contains method then we
  5678. 3:28:46have sword
  5679. 3:28:47food dot sword this will sort our list
  5680. 3:28:51alphabetically
  5681. 3:28:53so our list in alphabetical order is
  5682. 3:28:55fries hamburger hot dog pizza
  5683. 3:28:57and we can sort in reverse order
  5684. 3:29:00food.reverse
  5685. 3:29:04and our list in reverse order is fry's
  5686. 3:29:07hot dog hamburger pizza
  5687. 3:29:09we can clear our list food dot
  5688. 3:29:13clear
  5689. 3:29:16and now our list is empty and lastly
  5690. 3:29:20we can convert our list into an array so
  5691. 3:29:23i'm going to declare a new array
  5692. 3:29:25string let's say food array
  5693. 3:29:29equals food dot
  5694. 3:29:322 array
  5695. 3:29:36string item in food array so we have
  5696. 3:29:38pizza hamburger hot dog fries
  5697. 3:29:41well okay then everybody that is a list
  5698. 3:29:44it's similar to an array it's a data
  5699. 3:29:46structure that represents a list of
  5700. 3:29:48objects
  5701. 3:29:49that can be accessed by index it's
  5702. 3:29:51similar to an array
  5703. 3:29:52but they can increase and decrease
  5704. 3:29:54dynamically in size
  5705. 3:29:56but they waste more memory so yeah those
  5706. 3:29:58are lists in c
  5707. 3:29:59sharp if you found this video helpful
  5708. 3:30:00please be sure to give it a thumbs up
  5709. 3:30:02drop a random comment down below and as
  5710. 3:30:04always subscribe if you'd like to become
  5711. 3:30:06a fellow bro
  5712. 3:30:09all right what's going on everybody so
  5713. 3:30:11in this video i'm going to show you all
  5714. 3:30:12how we can create a list of objects from
  5715. 3:30:15a custom class of ours
  5716. 3:30:16in the last video i showed you all how
  5717. 3:30:18we can create a list of strings
  5718. 3:30:19but what if we would like to create a
  5719. 3:30:21list of players to keep track of the
  5720. 3:30:23amount of players in our game and their
  5721. 3:30:24names
  5722. 3:30:25so let's create a list list then we need
  5723. 3:30:28angle brackets and
  5724. 3:30:30the data type of the object we would
  5725. 3:30:31like to store within our list
  5726. 3:30:33i would like to store player objects and
  5727. 3:30:35i'll name
  5728. 3:30:36this list players because we're keeping
  5729. 3:30:38track of the players
  5730. 3:30:39players equals new list
  5731. 3:30:44angle brackets our data type again
  5732. 3:30:46player
  5733. 3:30:47parentheses semicolon so we now have a
  5734. 3:30:50list
  5735. 3:30:50that can store player objects so let's
  5736. 3:30:53instantiate some player objects
  5737. 3:30:54and i have a field setup username you
  5738. 3:30:57can also set up getters and setters too
  5739. 3:30:59if you prefer
  5740. 3:31:00so let's create some player objects
  5741. 3:31:01player and we'll call this player
  5742. 3:31:03player1
  5743. 3:31:04equals new player but we need to pass in
  5744. 3:31:07a username as an argument
  5745. 3:31:09let's say that this is chad and i'll
  5746. 3:31:11create maybe
  5747. 3:31:12two more players so player one
  5748. 3:31:16player two player three player two will
  5749. 3:31:18be what about steve
  5750. 3:31:20and karen okay now to add
  5751. 3:31:24our player objects to our list type the
  5752. 3:31:26name of the list
  5753. 3:31:27dot add and then pass in the name of the
  5754. 3:31:31object
  5755. 3:31:32players dot add player 1 then do the
  5756. 3:31:35same thing with player 2 and player 3.
  5757. 3:31:38then i'll use a for each loop to display
  5758. 3:31:40all these for each
  5759. 3:31:44the data type is player player
  5760. 3:31:48in players so let's take a look at this
  5761. 3:31:52i'm going to
  5762. 3:31:53console.writeline each player but since
  5763. 3:31:56we're displaying each object directly
  5764. 3:31:59what's going to be displayed is the
  5765. 3:32:01namespace
  5766. 3:32:02plus the data type of our object we can
  5767. 3:32:05access
  5768. 3:32:05the username field
  5769. 3:32:09and that will display each player's
  5770. 3:32:10username or we can overwrite the
  5771. 3:32:13tostring method so we have chad steve
  5772. 3:32:16and kieran
  5773. 3:32:17or we can override the tostring method
  5774. 3:32:20so let's try that too public
  5775. 3:32:24override to string
  5776. 3:32:28and i'm going to return
  5777. 3:32:31username so then when i use
  5778. 3:32:34console.writeline i can just type in
  5779. 3:32:36the object's name
  5780. 3:32:41so we have chad steve and karen now
  5781. 3:32:43another thing that you can do
  5782. 3:32:44when we instantiate these player objects
  5783. 3:32:47we can do so anonymously
  5784. 3:32:49so within the add method of our list
  5785. 3:32:52players
  5786. 3:32:52we can simply just pass in new
  5787. 3:32:56player then pass in a name and then we
  5788. 3:32:58don't necessarily need to create a
  5789. 3:33:00name for each of these objects so i'm
  5790. 3:33:02going to pass in
  5791. 3:33:03new player steve and new player karen
  5792. 3:33:08so this is optional but i like to create
  5793. 3:33:10anonymous objects
  5794. 3:33:11it's less work so we should have our
  5795. 3:33:14three players
  5796. 3:33:15chad steve and karen so yeah that's how
  5797. 3:33:18to add objects to a list
  5798. 3:33:20within the angle brackets you just list
  5799. 3:33:22the type of object that you would like
  5800. 3:33:24to store within your list
  5801. 3:33:25so yeah that's a list of objects in c
  5802. 3:33:27sharp if you found this video helpful
  5803. 3:33:29please be sure to smash that like button
  5804. 3:33:31leave a random comment down below
  5805. 3:33:32and subscribe if you'd like to become a
  5806. 3:33:34fellow bro
  5807. 3:33:37all right we have a lot to talk about
  5808. 3:33:40today getters and setters but before i
  5809. 3:33:42explain what these are let me explain
  5810. 3:33:44the situation
  5811. 3:33:45we have a class car and we have one
  5812. 3:33:48field within our car class
  5813. 3:33:49speed so when we construct a car object
  5814. 3:33:52we need to pass in something for the
  5815. 3:33:54speed
  5816. 3:33:55maybe this is miles per hour kilometers
  5817. 3:33:57per hour doesn't really matter
  5818. 3:33:59so let's say that somebody takes our car
  5819. 3:34:01object
  5820. 3:34:02and changes the speed to like whatever
  5821. 3:34:05this number is
  5822. 3:34:06100 million actually let's make that a
  5823. 3:34:09billion
  5824. 3:34:09how can we prevent people from doing
  5825. 3:34:11this because currently our car is going
  5826. 3:34:13way too fast
  5827. 3:34:15so one way in which we could do that is
  5828. 3:34:18to change
  5829. 3:34:18this field from public to private
  5830. 3:34:21but then we can't access it at all
  5831. 3:34:24another way is to set up
  5832. 3:34:25getters and setters and this adds
  5833. 3:34:28security to our program
  5834. 3:34:29via this concept called encapsulation so
  5835. 3:34:32we need to first
  5836. 3:34:33set up a property for this field the
  5837. 3:34:36property name
  5838. 3:34:37is the same as the field name except
  5839. 3:34:39it's capital
  5840. 3:34:40so type public if you're returning
  5841. 3:34:44something
  5842. 3:34:44list the data type then speed and then
  5843. 3:34:47let's make this capital
  5844. 3:34:49then add a set of curly braces a
  5845. 3:34:51property combines the aspects
  5846. 3:34:53of both fields and methods and it shares
  5847. 3:34:56the same name with the field
  5848. 3:34:57so it's kind of like something in
  5849. 3:34:59between a field and a method
  5850. 3:35:01and it contains accessors a get and
  5851. 3:35:04set accessor now to first assign a value
  5852. 3:35:07we'll need to change our constructor
  5853. 3:35:09instead of assigning the field speed we
  5854. 3:35:12will assign
  5855. 3:35:12the property speed so this has a capital
  5856. 3:35:16s
  5857. 3:35:16let's create a get accessor first when
  5858. 3:35:19we would like to
  5859. 3:35:20get the value of speed whatever is
  5860. 3:35:22contained within speed
  5861. 3:35:24we will use this get accessor so type
  5862. 3:35:26get
  5863. 3:35:27curly braces and we will return
  5864. 3:35:30speed and then we'll need to assign this
  5865. 3:35:33with
  5866. 3:35:34the set accessor then make sure you have
  5867. 3:35:36that semicolon at the end
  5868. 3:35:38so by using this get accessor this
  5869. 3:35:41will read whatever value is within our
  5870. 3:35:44field
  5871. 3:35:45then we'll need a set accessor if we
  5872. 3:35:47would like this to be
  5873. 3:35:48writable set curly braces so what we'll
  5874. 3:35:51do here
  5875. 3:35:52is set speed equal to value
  5876. 3:35:56so value is kind of like a parameter
  5877. 3:35:58properties combine the aspects of both
  5878. 3:36:00fields and methods
  5879. 3:36:02so when we assign a value to speed
  5880. 3:36:05well then this value is going to be
  5881. 3:36:08this so it's kind of like an argument
  5882. 3:36:10and a parameter
  5883. 3:36:11and within our set accessor we can
  5884. 3:36:13actually set up like some code or some
  5885. 3:36:15rules or something
  5886. 3:36:16so if somebody attempts to change the
  5887. 3:36:18speed we could write an if
  5888. 3:36:20statement like if value
  5889. 3:36:23is greater than let's say 500 then we
  5890. 3:36:26will limit this
  5891. 3:36:28let's change speed equal to 500 then
  5892. 3:36:32else speed
  5893. 3:36:35equals value
  5894. 3:36:40now car.speed is inaccessible due to its
  5895. 3:36:42protection level
  5896. 3:36:44so we need to access instead of the
  5897. 3:36:46field the property so that's
  5898. 3:36:48speed with a capital s and then if we're
  5899. 3:36:50going to
  5900. 3:36:51display the value of speed we'll access
  5901. 3:36:53the property
  5902. 3:36:54instead of the field so if we attempt to
  5903. 3:36:57set our speed equal to 1 billion
  5904. 3:37:00well we will access this set accessor
  5905. 3:37:03and then it's going to be limited to 500
  5906. 3:37:05then so if we attempt to change our
  5907. 3:37:07speed to 1 billion
  5908. 3:37:09it will be limited to 500 and then we
  5909. 3:37:11can still display the value
  5910. 3:37:13and access it because by having a getter
  5911. 3:37:16and setter
  5912. 3:37:16this is both readable and writable
  5913. 3:37:20so yeah those are getters and setters
  5914. 3:37:23they are accessors
  5915. 3:37:24found within a property a property
  5916. 3:37:26combines the aspect of both
  5917. 3:37:28fields and methods and if you have a
  5918. 3:37:30private field
  5919. 3:37:31you can also set up a property that has
  5920. 3:37:34getters and setters
  5921. 3:37:36for more security so yeah those are
  5922. 3:37:38getters and setters
  5923. 3:37:40in c sharp
  5924. 3:37:43all right everybody we have to talk
  5925. 3:37:45about auto implemented properties
  5926. 3:37:47this is an intimidating name but don't
  5927. 3:37:49worry it's actually fairly simple
  5928. 3:37:51they are shortcuts when no additional
  5929. 3:37:53logic
  5930. 3:37:54is required within a property you do not
  5931. 3:37:57have to define a field for a property
  5932. 3:37:59and you only have to write get and or
  5933. 3:38:01set inside the property
  5934. 3:38:03here's an example i have a class car and
  5935. 3:38:04what if we would like a
  5936. 3:38:06model field and property so that might
  5937. 3:38:08look something like this
  5938. 3:38:10make sure to not make your field public
  5939. 3:38:12for this example
  5940. 3:38:13so this will be a string named model for
  5941. 3:38:15a model of car
  5942. 3:38:17and i would like to set up a property
  5943. 3:38:19that has get and set accessors
  5944. 3:38:21so that would be public string
  5945. 3:38:26model with a capital m curly braces and
  5946. 3:38:30then i need a get and set accessor
  5947. 3:38:33all we'll do within the get accessor is
  5948. 3:38:36return
  5949. 3:38:38model and that's the field and a set
  5950. 3:38:41will be model r field
  5951. 3:38:45equals value and within my constructor i
  5952. 3:38:49will set
  5953. 3:38:49this dot model property make sure that
  5954. 3:38:52you have capital m
  5955. 3:38:54equals model whatever our parameter is
  5956. 3:38:58so if you're not doing anything else
  5957. 3:39:00within your getter and setter there's
  5958. 3:39:01actually a shortcut to all of this
  5959. 3:39:03and that's by using an auto implement
  5960. 3:39:05property so this is what we'll do we're
  5961. 3:39:07going to instead
  5962. 3:39:08get rid of all this and type
  5963. 3:39:12public string model
  5964. 3:39:16with a capital m curly braces
  5965. 3:39:20get semicolon set semicolon
  5966. 3:39:23and this will do the same thing however
  5967. 3:39:25it's a lot less to write
  5968. 3:39:27so if there's no additional logic that
  5969. 3:39:29you need besides
  5970. 3:39:30you know setting the value and getting
  5971. 3:39:33the current
  5972. 3:39:33field well you can just use an auto
  5973. 3:39:35implemented property
  5974. 3:39:36and this will do the same thing let's
  5975. 3:39:38test it so let's create a car object
  5976. 3:39:40car car equals new car then pass in
  5977. 3:39:44a model of car let's say a porsche this
  5978. 3:39:46time
  5979. 3:39:47and then i would like to display the
  5980. 3:39:49car's model car
  5981. 3:39:51dot model property with a capital m
  5982. 3:39:54so there will still be a model field but
  5983. 3:39:56it's hidden
  5984. 3:39:58and this car's model is a porsche
  5985. 3:40:01so that's like a shortcut you can do an
  5986. 3:40:03auto implemented property is a shortcut
  5987. 3:40:06when
  5988. 3:40:06no additional logic is required in the
  5989. 3:40:09property
  5990. 3:40:10you do not have to define a field for a
  5991. 3:40:12property and you only have to write
  5992. 3:40:14get and or set inside the property so
  5993. 3:40:17yeah that is an
  5994. 3:40:18auto implemented property if you liked
  5995. 3:40:20this video please give it a thumbs up
  5996. 3:40:22drop a random comment down below and
  5997. 3:40:23subscribe if you'd like to become a
  5998. 3:40:25fellow bro
  5999. 3:40:29all right what's going on people enums
  6000. 3:40:31enums are a special kind of class
  6001. 3:40:34that contains a grouping of named
  6002. 3:40:36integer constants we tend to use enums
  6003. 3:40:39when we have values
  6004. 3:40:40that we know will not change so here's
  6005. 3:40:42an example let's create an enum of
  6006. 3:40:44planets
  6007. 3:40:45and we will associate a planet number
  6008. 3:40:47with each named planet
  6009. 3:40:48so to create an enum type enum then a
  6010. 3:40:51unique name kind of like we're creating
  6011. 3:40:52a class
  6012. 3:40:53then curly braces so i'm going to add
  6013. 3:40:56the name of each planet within our solar
  6014. 3:40:58system beginning with mercury and then
  6015. 3:41:00separate each with a comma
  6016. 3:41:01so we have mercury venus earth then just
  6017. 3:41:04continue on in this pattern
  6018. 3:41:09this isn't necessary but i like to place
  6019. 3:41:11each of my members on a new line within
  6020. 3:41:13an
  6021. 3:41:14enum i think it's easier to read
  6022. 3:41:17okay we now have an enum of planets and
  6023. 3:41:20there's an associated integer with each
  6024. 3:41:22of these members
  6025. 3:41:23if we do not explicitly set an integer
  6026. 3:41:26by default the first
  6027. 3:41:28member is zero then one two three then
  6028. 3:41:31you just follow that pattern
  6029. 3:41:32so let's access one of these members of
  6030. 3:41:34our enum of planets
  6031. 3:41:36let's display a message planets
  6032. 3:41:39dot pluto
  6033. 3:41:43is a planet so taxes
  6034. 3:41:47a name of one of these members you type
  6035. 3:41:50the name of your enum planets dot the
  6036. 3:41:52name of your named member pluto
  6037. 3:41:54this will return the name not the
  6038. 3:41:57integer
  6039. 3:41:58let's try it pluto is a planet
  6040. 3:42:01this is no different from using the two
  6041. 3:42:04string method this will do the same
  6042. 3:42:05thing
  6043. 3:42:06pluto is a planet okay now if we need
  6044. 3:42:08the associated integer
  6045. 3:42:10with each of these named members we
  6046. 3:42:12would cast this member
  6047. 3:42:14as an integer let's change our message
  6048. 3:42:16around
  6049. 3:42:17let's say that planets.mercury
  6050. 3:42:21is planet number
  6051. 3:42:24than to access one of the integers
  6052. 3:42:27stored within a member
  6053. 3:42:28that would be planets dot the name of a
  6054. 3:42:31planet
  6055. 3:42:32mercury and then we will precede this
  6056. 3:42:34with parentheses
  6057. 3:42:36and this will convert our named member
  6058. 3:42:39into
  6059. 3:42:39an integer so our message is now
  6060. 3:42:43mercury is planet number zero so these
  6061. 3:42:46named members
  6062. 3:42:47always begin with zero but we can change
  6063. 3:42:49that and set them
  6064. 3:42:50let's say that mercury equals one venus
  6065. 3:42:54equals two and then continue on in this
  6066. 3:42:56pattern
  6067. 3:43:00let's display pluto as well because
  6068. 3:43:03pluto is feeling left out
  6069. 3:43:04planets dot pluto is planet number
  6070. 3:43:09planets dot pluto then cast as an
  6071. 3:43:12integer
  6072. 3:43:14okay mercury is planet number one pluto
  6073. 3:43:16is planet number nine
  6074. 3:43:18let's try something a little bit more
  6075. 3:43:19complex let's create an enum to keep
  6076. 3:43:22track of the radius of each planet
  6077. 3:43:25enum planet radius
  6078. 3:43:29and let me just copy this
  6079. 3:43:33okay here's the radius of each planet in
  6080. 3:43:35kilometers
  6081. 3:43:36mercury is two four three nine
  6082. 3:43:40six zero five one for venus
  6083. 3:43:44and then i'll just fast forward the
  6084. 3:43:45footage you can copy this down if you'd
  6085. 3:43:46like
  6086. 3:43:55all right we have the radius and
  6087. 3:43:57kilometers of each planet
  6088. 3:44:00so these are in name integer pairs
  6089. 3:44:03let's create a variable to store the
  6090. 3:44:05name of one of our planets
  6091. 3:44:07string name equals what about earth
  6092. 3:44:10planet radius the name of our enum dot
  6093. 3:44:13the name of our member
  6094. 3:44:15earth so if we're going to store this
  6095. 3:44:17within a string variable we need to use
  6096. 3:44:19the
  6097. 3:44:19to string method and then
  6098. 3:44:23let's display this just to test it let's
  6099. 3:44:26say
  6100. 3:44:27planet colon space plus name
  6101. 3:44:31and this should display just earth
  6102. 3:44:34planet
  6103. 3:44:35earth okay let's get the radius of earth
  6104. 3:44:40int radius
  6105. 3:44:43planetradius dot earth
  6106. 3:44:47then cast this as an integer
  6107. 3:44:53console.rightline let's say radius
  6108. 3:44:57colon space plus radius
  6109. 3:45:01radius 6371 let me add kilometers
  6110. 3:45:06so that's kind of the nice thing about
  6111. 3:45:08enums is that we don't necessarily need
  6112. 3:45:10to remember all of these numbers
  6113. 3:45:12it's kind of like restoring variables so
  6114. 3:45:14to say they are name
  6115. 3:45:15integer constants that will not change
  6116. 3:45:17through the life of a program
  6117. 3:45:19now let's move on to a challenge round
  6118. 3:45:20let's create a method that will
  6119. 3:45:22calculate
  6120. 3:45:23the volume of one of our planets when we
  6121. 3:45:25pass in a radius
  6122. 3:45:27public static will return a double
  6123. 3:45:32and this will be the volume method and
  6124. 3:45:35there is one parameter
  6125. 3:45:36the parameter is planet radius and we
  6126. 3:45:40will name this just radius
  6127. 3:45:43okay we need to return let's say volume
  6128. 3:45:49and double volume equals
  6129. 3:45:53and here's the formula to calculate the
  6130. 3:45:56volume of a sphere 4.0 divided by
  6131. 3:46:003.0 times math
  6132. 3:46:03dot pi times
  6133. 3:46:06our radius to the power of three so we
  6134. 3:46:08can use math
  6135. 3:46:10dot power pass in
  6136. 3:46:13our radius make sure to cast this as an
  6137. 3:46:16integer
  6138. 3:46:18to the power of three
  6139. 3:46:23and let's call the volume method and
  6140. 3:46:25store this within
  6141. 3:46:26a double named volume
  6142. 3:46:29double volume equals we will invoke the
  6143. 3:46:32volume method and pass in planet
  6144. 3:46:36radius dot earth
  6145. 3:46:40and then we'll display the volume
  6146. 3:46:42console.writeline
  6147. 3:46:44volume colon space plus
  6148. 3:46:48volume and i think that's in cubed
  6149. 3:46:51kilometers
  6150. 3:46:55all right and the volume of earth
  6151. 3:46:59is whatever number this is one zero
  6152. 3:47:02eight three something something
  6153. 3:47:03something something
  6154. 3:47:05cubed kilometers so yeah those are enums
  6155. 3:47:08they're like a special class that
  6156. 3:47:09contains a
  6157. 3:47:10set or grouping of named integer
  6158. 3:47:13constants
  6159. 3:47:14we tend to use enums when we have values
  6160. 3:47:16that we know will not change
  6161. 3:47:18and to get the integer value from an
  6162. 3:47:20item you must
  6163. 3:47:21explicitly convert it to an integer so
  6164. 3:47:24yeah those are enums if you found this
  6165. 3:47:26video helpful
  6166. 3:47:27please be sure to smash that like button
  6167. 3:47:29leave a random comment down below
  6168. 3:47:30and subscribe if you'd like to become a
  6169. 3:47:32fellow bro
  6170. 3:47:35if you wouldn't mind please like comment
  6171. 3:47:38and subscribe
  6172. 3:47:39one like equals one prayer for the
  6173. 3:47:41youtube algorithm
  6174. 3:47:43uh generics all right
  6175. 3:47:46let's get started generics anything
  6176. 3:47:48that's generic
  6177. 3:47:49is not specific to any particular data
  6178. 3:47:52type
  6179. 3:47:53we can make something generic by adding
  6180. 3:47:54a set of angle brackets
  6181. 3:47:56then t we can make classes generic
  6182. 3:47:58methods
  6183. 3:47:59fields whatever we want this allows for
  6184. 3:48:02greater code reusability for different
  6185. 3:48:04data types
  6186. 3:48:05let's say that we have three different
  6187. 3:48:06types of arrays an array of integers
  6188. 3:48:08doubles and strings let's create those
  6189. 3:48:11int
  6190. 3:48:12array i'll call this interray
  6191. 3:48:15equals i don't know one two three
  6192. 3:48:18the elements really aren't important the
  6193. 3:48:20data type is
  6194. 3:48:21okay then let's say we have an array of
  6195. 3:48:24doubles
  6196. 3:48:25i will name this double array and
  6197. 3:48:28the elements are 1.0 2.0
  6198. 3:48:32and 3.0 then let's say that we have an
  6199. 3:48:36array of strings
  6200. 3:48:38string array equals
  6201. 3:48:43one two
  6202. 3:48:46and three okay what if i would like to
  6203. 3:48:50display the elements of each of these
  6204. 3:48:51arrays so we can use a method for that
  6205. 3:48:54let's begin with a method to accept an
  6206. 3:48:56array of integers
  6207. 3:48:58so let's add that here public static
  6208. 3:49:02void we're not returning anything and
  6209. 3:49:04let's say that this method is named
  6210. 3:49:06display elements
  6211. 3:49:11so there is one parameter if we're
  6212. 3:49:13displaying an array of integers
  6213. 3:49:15well that will be our parameter int
  6214. 3:49:18array
  6215. 3:49:19and let's call this array and then we'll
  6216. 3:49:21need a for loop or for each loop to
  6217. 3:49:24iterate over the elements of this array
  6218. 3:49:26let's use a for each loop for each
  6219. 3:49:29int item in array
  6220. 3:49:33then we will write preferably not right
  6221. 3:49:36line
  6222. 3:49:38item then maybe i'll add a space okay
  6223. 3:49:40let's call the display elements method
  6224. 3:49:42that we just created
  6225. 3:49:44and then pass in our interray as an
  6226. 3:49:46argument
  6227. 3:49:48and let's see what happens okay we have
  6228. 3:49:51the numbers one
  6229. 3:49:52two three so what if we attempt to pass
  6230. 3:49:55in
  6231. 3:49:56our double array display elements
  6232. 3:49:59then we will pass in our array of
  6233. 3:50:01doubles
  6234. 3:50:02so we actually can't use this method for
  6235. 3:50:05doubles we can only
  6236. 3:50:06use this for integers cannot convert
  6237. 3:50:08from double to
  6238. 3:50:10int if i would like to display my array
  6239. 3:50:12of doubles
  6240. 3:50:13well i would need a completely different
  6241. 3:50:15method that accepts
  6242. 3:50:16an array of doubles as an argument so
  6243. 3:50:19i'm going to
  6244. 3:50:20copy this current method that we have
  6245. 3:50:22change into
  6246. 3:50:23double and change into double here as
  6247. 3:50:27well
  6248. 3:50:28and then we can call this version of our
  6249. 3:50:30display elements method this will do the
  6250. 3:50:34same thing
  6251. 3:50:35actually let me make one change i'm just
  6252. 3:50:37going to add a right line to the end of
  6253. 3:50:39these
  6254. 3:50:40so basically we have two methods that do
  6255. 3:50:43the exact
  6256. 3:50:43same thing however they accept a
  6257. 3:50:45different data type as an argument
  6258. 3:50:48let's add one more method for strings
  6259. 3:50:53public static void display elements
  6260. 3:50:56the parameter is an array of strings for
  6261. 3:50:58each
  6262. 3:50:59string item in array and then i can use
  6263. 3:51:03the string version of display elements
  6264. 3:51:06but pass
  6265. 3:51:06in our string array
  6266. 3:51:10so we have each of our three different
  6267. 3:51:12types of arrays all displayed
  6268. 3:51:15so this is a lot of work we have three
  6269. 3:51:17methods that do basically the same thing
  6270. 3:51:19however they accept
  6271. 3:51:20different arguments so what if we had
  6272. 3:51:22one method that could accept
  6273. 3:51:24basically any data type well that's
  6274. 3:51:26where generics come in
  6275. 3:51:28so let's take these first two methods
  6276. 3:51:30and delete them we will no longer need
  6277. 3:51:32them
  6278. 3:51:32we're going to turn this method into a
  6279. 3:51:34generic method
  6280. 3:51:35so after the method name add a set of
  6281. 3:51:38angle brackets
  6282. 3:51:39then add t so really you can put
  6283. 3:51:41anything you want between these angle
  6284. 3:51:43brackets i like to say thing because i
  6285. 3:51:45think it's funny
  6286. 3:51:46we're going to accept a thing as an
  6287. 3:51:48argument
  6288. 3:51:49so we need to change that here as well
  6289. 3:51:51so change any instance of
  6290. 3:51:53a data type to t or
  6291. 3:51:56thing if you're using thing okay so
  6292. 3:51:59change
  6293. 3:51:59string to thing we're accepting an array
  6294. 3:52:02of things
  6295. 3:52:03for each thing item
  6296. 3:52:06in array so now we have one method that
  6297. 3:52:09accepts
  6298. 3:52:10all data types we can reuse this one
  6299. 3:52:12method for an array of integers
  6300. 3:52:14doubles and strings
  6301. 3:52:18and it appears to work so that's where
  6302. 3:52:20generics are helpful
  6303. 3:52:22anything that's generic is not specific
  6304. 3:52:24to a particular data type
  6305. 3:52:26we can add angle brackets t or really
  6306. 3:52:29anything within these angle brackets
  6307. 3:52:31just make sure that it's consistent
  6308. 3:52:32so we can add this to classes methods
  6309. 3:52:35fields etc
  6310. 3:52:36and this allows for greater code
  6311. 3:52:38reusability for different data
  6312. 3:52:40types so instead of three different
  6313. 3:52:41methods that all accept different data
  6314. 3:52:43types we have one that accepts basically
  6315. 3:52:46all data types
  6316. 3:52:47so yeah those are generics if you found
  6317. 3:52:49this video helpful be sure to smash that
  6318. 3:52:51like button
  6319. 3:52:51leave a random comment down below and
  6320. 3:52:53subscribe if you'd like to become a
  6321. 3:52:55fellow bro
  6322. 3:52:56hey what's going on everybody it's bro
  6323. 3:52:59hope you're doing well
  6324. 3:53:00and in this video i'm going to explain a
  6325. 3:53:01multi-threading in c-sharp
  6326. 3:53:03so sit back relax and well enjoy the
  6327. 3:53:06show
  6328. 3:53:09if you find this video helpful please
  6329. 3:53:11remember to like
  6330. 3:53:12comment and subscribe your support will
  6331. 3:53:14help keep this channel running
  6332. 3:53:16all right all right all right threads a
  6333. 3:53:19thread is an execution path of a program
  6334. 3:53:22we can use multiple threads to perform
  6335. 3:53:25different tasks of our program
  6336. 3:53:27at the same time so when we begin a
  6337. 3:53:29program we have
  6338. 3:53:30one thread that is running and that is
  6339. 3:53:32named the main thread
  6340. 3:53:33twerk with threading include at the top
  6341. 3:53:36using system
  6342. 3:53:37dot threading so let's get the current
  6343. 3:53:40main thread that is running and i'll
  6344. 3:53:41assign this to a local variable of the
  6345. 3:53:43thread data type
  6346. 3:53:44let's say thread main thread
  6347. 3:53:48equals thread dot current
  6348. 3:53:51thread so this will assign the current
  6349. 3:53:54thread that is running
  6350. 3:53:55to a local variable named main thread or
  6351. 3:53:58whatever you want to name it
  6352. 3:54:00so i'm going to change the name property
  6353. 3:54:02of main thread to equal
  6354. 3:54:04let's say main thread
  6355. 3:54:07and then i'll display this with a
  6356. 3:54:08rightline statement main thread
  6357. 3:54:11dot name and the name of this thread is
  6358. 3:54:15main thread
  6359. 3:54:16here's our job let's say that we need
  6360. 3:54:18two timers to run
  6361. 3:54:19one is counting up from zero to ten and
  6362. 3:54:22the other is counting from ten to zero
  6363. 3:54:24one is counting up the other is counting
  6364. 3:54:25down but we need both of these timers to
  6365. 3:54:28run
  6366. 3:54:28concurrently so if we were to write
  6367. 3:54:31these both on the same
  6368. 3:54:32thread this is what this would look like
  6369. 3:54:34and then later on we'll
  6370. 3:54:35have these timers run on different
  6371. 3:54:36threads so let's create a countdown and
  6372. 3:54:39count
  6373. 3:54:39up method public static
  6374. 3:54:43void count
  6375. 3:54:46down and then i'll need a for loop to
  6376. 3:54:50iterate 10 times
  6377. 3:54:514 and i equals 10
  6378. 3:54:56then i will continue this as long as i
  6379. 3:54:58is greater than or
  6380. 3:55:00equal to zero and then
  6381. 3:55:03decrement i by one during each iteration
  6382. 3:55:06and during each iteration let's say that
  6383. 3:55:08timer number one
  6384. 3:55:10colon space plus i plus seconds
  6385. 3:55:15now we can actually have our current
  6386. 3:55:17thread that is running sleep for a given
  6387. 3:55:19amount of milliseconds by typing thread
  6388. 3:55:21dot sleep then pass in how many
  6389. 3:55:24milliseconds you would like your thread
  6390. 3:55:25to sleep
  6391. 3:55:26so 1000 milliseconds for one second and
  6392. 3:55:29then when we escape our for loop let's
  6393. 3:55:31display that
  6394. 3:55:33timer number one is complete
  6395. 3:55:36and we'll also need a count up method so
  6396. 3:55:39let's copy countdown
  6397. 3:55:41paste it change down to up
  6398. 3:55:44i is equal to zero we will continue this
  6399. 3:55:47as long as
  6400. 3:55:48i is less than or equal to ten
  6401. 3:55:52increment i by one during each iteration
  6402. 3:55:55timer number
  6403. 3:55:56two timer number two is complete so
  6404. 3:55:58let's invoke both
  6405. 3:56:00countdown and count up
  6406. 3:56:04so remember that these are both running
  6407. 3:56:06on the same thread
  6408. 3:56:07our main thread and when we complete our
  6409. 3:56:10main thread
  6410. 3:56:10let's display that main thread
  6411. 3:56:14dot name plus is
  6412. 3:56:17complete and i'm just going to hide this
  6413. 3:56:20line here
  6414. 3:56:22okay let's go so we have timer number
  6415. 3:56:26one that's running currently
  6416. 3:56:27timer number two has not started yet
  6417. 3:56:29because these are both running on the
  6418. 3:56:31same thread
  6419. 3:56:36so timer number one is complete and now
  6420. 3:56:38we're working on timer number two
  6421. 3:56:46and now timer number two is complete our
  6422. 3:56:48main thread is complete
  6423. 3:56:50so if i would like both of these methods
  6424. 3:56:52to execute at the same time i would need
  6425. 3:56:54to create some additional threads
  6426. 3:56:56and here's how to do so thread let's
  6427. 3:57:00name this thread 1
  6428. 3:57:01equals new thread and within the
  6429. 3:57:04constructor of our thread let's pass in
  6430. 3:57:06the method we would like to execute
  6431. 3:57:09thread one is in charge of counting down
  6432. 3:57:11and thread two is in charge of counting
  6433. 3:57:14up
  6434. 3:57:18so each of these threads has a built-in
  6435. 3:57:20start method which we need to invoke to
  6436. 3:57:22begin each of these threads
  6437. 3:57:24thread1.start and thread2.start
  6438. 3:57:30and we can get rid of these we now have
  6439. 3:57:33one thread in charge of counting down
  6440. 3:57:35and the other thread is in charge of
  6441. 3:57:36counting up
  6442. 3:57:37and our main thread is still running in
  6443. 3:57:39the background
  6444. 3:57:42so our main thread is complete and we
  6445. 3:57:44have one timer that's counting down
  6446. 3:57:46and the other that is counting up and as
  6447. 3:57:48you can see these are both running at
  6448. 3:57:50the same time concurrently
  6449. 3:57:55so if you have a method that has
  6450. 3:57:56parameters there's one additional step
  6451. 3:57:58let's say that we have i don't know a
  6452. 3:58:00string name or something
  6453. 3:58:02we don't necessarily need to use these
  6454. 3:58:04okay so we can't use these as they are
  6455. 3:58:07currently
  6456. 3:58:08so what we're going to do is within the
  6457. 3:58:10constructor of our threads
  6458. 3:58:11is pass in what is known as a lambda
  6459. 3:58:14expression
  6460. 3:58:15so parentheses arrow then the name of
  6461. 3:58:18our method
  6462. 3:58:19countdown parentheses then any
  6463. 3:58:22arguments if there are any let's say i
  6464. 3:58:24don't know timer
  6465. 3:58:26number one i probably won't use this at
  6466. 3:58:28all but this is just for an example
  6467. 3:58:30okay let's do the same thing with thread
  6468. 3:58:32two so we have a lambda expression
  6469. 3:58:35parenthesis arrow and this is in charge
  6470. 3:58:38of counting
  6471. 3:58:39up and passing i don't know timer number
  6472. 3:58:41two i guess
  6473. 3:58:43i might have to make a separate video on
  6474. 3:58:45lambda expressions
  6475. 3:58:46okay let's try this again so this should
  6476. 3:58:49work
  6477. 3:58:51all right there we go so yeah those are
  6478. 3:58:55threads
  6479. 3:58:56a thread is an execution path of a
  6480. 3:58:58program we can use
  6481. 3:58:59multiple threads to perform different
  6482. 3:59:01tasks of our program at the same time
  6483. 3:59:04when we begin a program we have one
  6484. 3:59:06thread that is running
  6485. 3:59:07and that is referred to as the main
  6486. 3:59:09thread so yeah that's multi-threading in
  6487. 3:59:11c sharp if you would like a copy of this
  6488. 3:59:13code i will post this to the comment
  6489. 3:59:15section down below
  6490. 3:59:16and well yeah that's multithreading in c
  6491. 3:59:19sharp
  6492. 3:59:20hey you yeah i'm talking to you if you
  6493. 3:59:23learned something new
  6494. 3:59:24then help me help you in three easy
  6495. 3:59:26steps
  6496. 3:59:27by smashing that like button drop a
  6497. 3:59:29comment down below
  6498. 3:59:30and subscribe if you'd like to become a
  6499. 3:59:32fellow bro
  6500. 3:59:40[Music]
  6501. 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.