C# Programming | In One Video

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome to the draft Academy my name is Mike this video is part of a series of videos where I'll be going over the basic syntax of different programming languages and one of the most annoying things about picking up a new programming language is having to learn all the syntax from scratch and my own experience it generally manifests in a lot of google searching and Stack Overflow from experienced developer the problem isn't understanding the concepts of programming it's knowing how to implement those concepts in the new language so things like variables data types data structures loops conditionals an object orientation all of these things are done a little bit differently in each programming language and having to research how to do it all can be extremely annoying and tedious in this video I'll go through and show you the basic syntax for the most common parts of the language I'll give you a brief history of the language and we'll touch on some of the best practices and conventions used in that language now this is meant to be a quick run-through I won't be showing you how to install or configure anything and we won't really have time to learn how to do everything you see I've distilled it down into what I consider the core concepts so the goal for this is to be a quick video so I can't cover every aspect of the language without being said this video is meant for programmers who already understand the core concepts of programming it's not going to teach you any programming concepts I'll simply show you how to do all the common things in the new language so if you argue to programming draf Academy has hundreds of videos where I hold your hand and I walk you through everything you need to learn to program but this is meant for developers who already understand programming so without further ado let's get started c-sharp is a general-purpose statically typed object-oriented programming language that was founded in 2000 by Microsoft as part of their dotnet initiative c-sharp was designed to be portable and fast and is tightly coupled with Microsoft's dotnet framework it was created alongside the dotnet framework in the late 90s she char was one of the most popular programming languages in the world and can be used to write applications on almost every platform including Windows Mac iOS Android and Linux in fact more people are using c-sharp to write cross-platform apps every day because it allows for a shared codebase across platforms basically you could write all your back-end business logic and database management code once and use it on your iPhone Andrew and desktop all c-sharp code is compiled down into an intermediate language called common language which is then translated and executed by the dotnet runtime also called the common language runtime this ensures that c-sharp programs can be run on virtually any combination of hardware and operating system the underlying c-sharp runtime uses an automatic garbage collector to manage memory and the syntax of c-sharp is largely influenced by Java most developers choose to write C term using a specialized integrated development environment called Visual Studio Visual Studio is an IDE that's maintained and developed by Microsoft so it's the best choice and realistically your only choice for writing c-sharp applications in fact most c-sharp developers are gonna use a Visual Studio in some way shape or form so in this tutorial we are gonna go ahead and look at some of the basics of c-sharp we're basically just gonna look at all the common syntax and how we can implement some of the most common programming things inside of c-sharp now over here I have my command line this is where I'm gonna to be compiling and running all of my c-sharp and then over here I have my text editor which is where I'm gonna be writing all of my c-sharp so you'll see over here I have a basic skeleton and this is just it's called app dot CS and the CS as the c-sharp extension and over here I'm just using a couple different things so I'm using system and I'm using system collections basically just means I'm importing a different code and I just need these in here because I'm gonna be using some of the stuff from those libraries and anytime in c-sharp we're writing code we're always gonna put everything inside of a class so I have this class called app and then in here I have this method it's public static void main and this is the main method basically this is the method where I can put any code that I want to get run so when I run my c-sharp program any of the code that's inside that main method will get executed and because C sharp is class-based generally we're referring to these as methods and not functions although you can refer to them as both so the first thing we'll look at is printing how do we write something out onto the screen and in c-sharp it's pretty simple we're just gonna say console dot write line and I can either say write line or I can say write and then inside parentheses I can just put the text that I want to print out so write line is gonna print out this text and that's going to print out a new line after it write is just gonna print out the text no Lu and so you'll see over here the result with printing that we get hello world with an exclamation point now let's talk about variables now variables are basically containers where we can store pieces of information and variable names in c-sharp are case-sensitive and they can begin with a letter an underscore or in at symbol generally you want to start them off with letters that's kind of convention and then after that first character we can have letters numbers or an underscore convention says that we want to start our variables with the lowercase word and then additional words are gonna be uppercase so this is sometimes referred to as camelcase so I have like my first variable you can see that it's named where the second and third words are capitalized and c-sharp is statically typed which means we're gonna have to rigorously define the data types of the variables that we want to create so I'm creating some variables here the first one is a string it's called name and I'm just storing a name and strings are actually objects so they're not actually like primitive data types but you can create a string just with these quotation marks and then we can also have a character which is like a single 16-bit Unicode character and you can see what the character we use a single quote just like that and then down here we have our numbers so two basic types of numbers whole numbers and decimal numbers whole numbers are integers first is a byte it's an 8-bit unsigned integer then we have a short which is a 16-bit signed integer the int which is kind of like the default is a 32-bit signed integer and then we have a long and you'll notice with the long I'm putting a capital L here after it that'll denote that it's a long and it's a 64-bit signed integer if you want you can prepend these data types with you to make it on the sign so I can put in you here and that would mean like an unsigned short and then down here we have our floating points so floating points are decimal numbers we start off with this float this is a 32-bit floating point and you can see I'm putting a capital F after here that would signify that it's a float we also have a double which is gonna be the default and it's a 64-bit double-precision floating put and then finally we have a decimal and you'll notice I'm putting an M here that's a 128 bit precise decimal so if you're doing anything with like money or you know something where you really need to be precise and you want to use a decimal but for the most part people are gonna be using doubles then finally we have our one bit boolean which is gonna be a true or false value and a lot of times you're gonna name this starting with like is or are so like is tall or are something you know basically that's how you can name it and if you want you can just declare the variable and then you can give it a value or like I did up here you can just assign it a value right away and we can also modify these variables just like I did down here then if we want to print out variables you have a couple options your first is concatenation so I could console dot write line and I could print out a string and I can concatenate onto the end of it the value inside the variable or if you put a dollar sign here in front of the quotation marks that means you can just interweave your variables just like this inside of curly brackets and this is actually pretty useful and a lot of other languages will do something similar to that so you can see over here we're printing out your name is John and your name is John we can also have a constant so I could come over here and say like Co NS T like this Const modifier and basically what this will do is it'll make this a constant and a lot of times in c-sharp we'll name our constants in uppercase so it'll be like that with words delineated by this underscore and now this constant because of the constant actually won't be able to be modified so that's just gonna be like set as it is can't be modified now let's talk about casting and converting so we have all these data types how can we swap data types or you know convert different data types to other data types the first is with casts so I have a floating-point number here and I can actually cast it to an integer so I can just put int inside of open and close parentheses here and then right after it the number or I can do the same thing for double so this will cast this to an int and this to a double you can see over here we get 3 and then and then this is still 3 but it's actually gonna be double now and then also a lot of times if you have numbers trapped inside strings you want to be able to get them out so I have over here a string with a number inside of it I can just say convert dot to int 32 and there's actually a bunch of these like two and 32 you can also do like to double or you can even do like 2 in 16 or stuff like that basically what this will do is it'll convert this into whatever we specify here so we'll actually be able to convert this into a double and convert this into an integer and then you can see I store these guys inside of the corresponding variables and then down here I'm adding a hundred to them so you can see over here we get 150 and 199 so it's actually able to do the math on so we were able to convert strings into instant doubles now strings are one of the most important data types that we'll be dealing with and you know everybody loves strings everybody loves to work with them so I'm gonna show you guys some of the basics with strings I can just create a string variable like this down here again we're using the quotation marks and strings start off with index position 0 so this is pretty common across programming languages even though H is technically the first character in the string it's at index position 0 so there's five characters in this string but the index positions end it for because we start counting at zero and you can do a bunch of these different methods on here so I could say like greeting dot length and this will give me the length so we get five if I want to access a specific character in the string I can just make these open and close square brackets and then put the index in there so greeting zero is going to give me this H because it's at index position zero we can also do index of and that'll tell us at what position a specific sub string or character is in the string so I have llo and index of llo gives me a two and that's because llo starts at index position two if you put in a string or a character here that's invalid so that's not in the string you'll get a negative one and we can also do like a substring so I could say greeting dot substring two and if I just put one number in there it's gonna give me the substring starting at that index and going to the end of the string so starting at two we get LLO if I put another number in here this will give me a length so it'll basically be started in X position one and give me three characters so we get e l-l and that's why we get e ll over here now there's a ton of these string methods obviously I'm not gonna show you got all of them but all the string methods that you would expect in a normal programming language you're gonna have access to in c-sharp maybe just check out the API for more detailed information on that now let's take a look at numbers so in addition to strings we also have numbers are extremely useful in any programming language so I'm gonna show you guys just some stuff we can do with them so over here I'm writing out two times three and that'll actually do that operation for us so we get six you can do addition subtraction division and multiplication you can also use the modulus operator modulus operator we'll take two numbers and divide them and give you the remainder so if I say 10 mod 3 this will give me a 1 back because 10 divided by 3 is 3 with a remainder of 1 you can also specify order of operations so generally like if I have this equation down here 1 plus 2 times 3 C sharps gonna do the multiplication first so it's 3 times 2 6 plus 1 is 7 we get 7 over here but if I wanted I could change the order of operations by putting parentheses around here so I can put a parenthesis around the one in the 2 and now when I run the program instead of getting 7 we're gonna get 9 because it's gonna be 1 plus 2 is 3 times 3 is 9 and I want to show you guys how instant doubles are gonna interact so over here I have an integer and I have a double if I take this integer and divided by a double you'll notice that I'm getting a double back so I'm getting a float floating-point number back but if I take an integer and just divide by another integer so like 10 divided by 3 now you'll notice that we're just getting an integer back we're just getting 3 so two floating-point numbers if they're added or multiplied or divided together you're gonna get a floating-point back and you have a floating-point in int you're gettin a float back if you have to answer getting an int back and that's pretty standard across all programming languages all right so now down here I'll show you some other stuff so I'm creating an integer called num giving it a value of 10 and I'm saying num plus equals 100 so plus equals is a shorthand for num is equal to num plus 100 so basically this will take num and it'll add a hundred to it so over here now the value of num is gonna be 110 I can also increment it using this plus plus operator so that can give me 1 11 because I'm incrementing the value of num you can also use - - to decrement and then down here I just have some math methods so there's this math class that we can use and you can just say math it's a capital M dot and then there's a bunch of method so like powa the power so it'll do 2 to the power of 3 which is gonna be 8 square root of 144 is gonna be 12 and then rounding to point 7 will give us 3 and like I said all the standard math methods or functions that you'd expect are gonna be available there you can just check out the API online for a more detailed description now let's talk about getting user input obviously in any programming language there's tons of ways you can go about getting input but in our case we're just gonna get input from the console over here so I have the first thing I'm doing is printing out prompts I'm just saying console dot right enter username and then what I'm gonna do is I'm going to create a string so I'm saying string username and I'm gonna set it equal to console dot read line so unlike console dot right or console dot write line read line is gonna allow the user to enter information into the program so when I say console dot read line this will allow me to enter something in and I can just put my name over here hit enter and it says hello Mike so it's storing whatever the user entered inside of this variable and then it's printing it out down here and you can use those conversions that I showed you guys before to get numbers and obviously like you get both a little calculator or do something like that all right now let's talk about arrays so arrays are extremely important data structure where you can store multiple pieces of information so the way we create an array in C sharp is I can just say the type of information that I want to store and open and closed square bracket and then the name of the array and if you want you can give it some initial information so I could say lucky numbers is equal to and then inside of these curly brackets I'm giving it some initial information and just like strings array indexes are gonna start at 0 so the first element in the array is going to be an index position 0 you want to access the elements in the array you can just refer to the arrays name and then in square brackets you can put the index of the element that you want to access and so in this case I'm modifying it but you could also print it out like I did down here so over here I'm printing out lucky number to 0 which is gonna be 90 and then lucky numbers 1 which is gonna be 8 you can also print out the length lucky numbers dot length is gonna be 6 now a lot of times you're not going to know what information you want to put inside the array right away so we could come up here and do something like this I could say and again square brackets lucky numbers and I can set this equal to a new int and then over here I need to specify how many elements I want to be able to store inside of this array so in this case this array will be able to sort n elements and if I run my program now you'll see lucky number 0 has been given a value of 90 but lucky numbers 1 which we're printing out here hasn't and it just gets initialized to 0 so anytime you do something like this all the array indexes are gonna get initialized to 0 unless you or until you set them to different numbers like we did over here and you'll see the length is 10 because that's the length that we specified right there we can also work with dimensional arrays so n dimensional arrays are basically two three four five dimensional arrays something like that down here I just have a two dimensional array just to kind of show you guys how this works and I'm just saying int and then instead of one square bracket I have two and that's going to refer to the dimension so two square brackets would mean two dimensional and then number grid is equal to and then in here I'm creating two arrays so this is how I can initialize an array I can just say new int and then square brackets and then inside of here obviously like assign all the values so I'm doing this twice and then to access them you can just refer to this is going to be the index position of the element in number grid and this will be the index position of the element inside of the element so zero one is gonna be this two over here so I'm setting that equal to 99 and down here I'm printing out zero zero which is a one you can see up there and then zero one which we set to 99 now I want to talk to you guys about a more dynamic array it's called an array list and this is actually a class inside of c-sharp but the ArrayList is basically just an array where it's a lot more flexible than a normal array one of the downsides to normal arrays is that you have to like rigorously specify how many elements you want to have in there and this ArrayList is going to allow us to basically just like dynamically add elements and in order to use this you're gonna have to use include this up here using system dot collections and then down here I can just say ArrayList friends is equal to new ArrayList and then open and close parentheses and down here I can use a bunch of methods on this ArrayList so I could add elements like friends up at Oscar Angela and Kevin and then down here you'll see that I'm printing out like friends zero friends one so printing out Oscar and Angela you can also check to see if something is inside of the array so I could say like friends doc contains Oscar and you'll see over here we get a true back so if if asker wasn't in there then we get false and then we can also get the count so that's how many elements are in there if I wanted I could remove an element so I could say like friends don't remove Oscar and now this will go ahead and remove Oscar from the list and you'll see down here it says false because friends no longer contains Oscar now there's a lot more array methods mean pretty much all the standard methods that you would expect in something like a dynamic array you would have this ArrayList class again you can just check out the API for more information now move on to methods methods are little blocks of code that we can call and we cannot we can kind of reuse the code that's inside of them so over here I have my main method and this is like I said it's a special method that is going to get called when we execute our c-sharp program but if you want to make other methods you can so down here below the main method I created another method and you'll see there's a bunch of like keywords before this I'll kind of walk you guys through public public is a access modifier it basically it basically determines what code can see this function a lot of times you can just save public and we'll talk about some of the other ones later static is another keyword and actually a lot of times when you create methods like this you're not gonna need that static keyword we'll talk more about what static is later but just know that if you're putting a method alongside of this main method and you want to be able to call it like I am up here you're gonna need this static keyword and then finally we want to put a return type here so int is gonna be the type of information that's getting returned from this method if you don't want to return type you can just say void and that'll mean you're not returning anything in my case I'm returning an integer so over here I can just give this a name and generally the convention is that in c-sharp whenever you have methods you're gonna name them starting with a capital letter so you'll see add numbers is it capitalized and then over here I can specify some parameters that I want to take in so like aunt num1 and num2 you takin any data type you know strings doubles boolean whatever and then down here I'm just saying return on 1 plus num2 you could have as much code as you want in there this return keyword will return you from the function and then anything that you specify over here is gonna get returned as a value as long as you have the corresponding data type in the return block right there alright so up here I created an integer called sum and I set it equal to the result of adding these two numbers and then down here I just printed it out so sum is actually gonna get the value of doing this and then we'll print it out and you can see we get 64 right there now I'll talk to you guys about conditionals so first thing we're gonna look at our if statements if statements allow your programs to respond to different things you'll see I have two boolean's here is student and is smart both equal to false and down here I can write an if statement which is going to respond to those two variables so depending on the those variables it'll do different things I'm basically checking here if is student and is smart you can use this double ampersand in order to check more than one condition I could also use two vertical bars like that it's gonna stand for or so there's and and or and then if this condition is true then obviously we'll come down here and execute that code down here we can also use else--if so that will get executed if this isn't true and then we'll come down here and check this condition so I'm saying is student and not is smart so here is the negation operator it's that exclamation point basically that'll only gate a condition so for example these are boolean so if I said not is smart it'll just negate that boolean value and then finally we have an else so that'll get executed when not none of the above conditions are true in that case we were using boolean x' but you can actually get boolean values by comparing different values so over here I can say if for example one is greater than three so I you know this comparison will result in a true or false so it'll result in a boolean value so that's another way that we can define conditions so one greater than three we can use greater than less than greater than equal to less than equal to not equal to and double equals we're just gonna be equal to so if I said one less than three now this will be true and we'll execute the code so says the number of comparison was true and that's kind of going off the screen a little bit so that's the basics of if statements it's everything that you would expect from if statements another type of conditional that we can represent is a switch statement and a switch statement is essentially just a specialized type of if statement where we can check one value against a bunch of other values so over here I have a character my grade it's equal to a and I'm using the switch statement and I'm passing in my grade into it basically what the switch name will do is it'll check to see if my grade is equal to a bunch of different cases so I can say like case a is this is in the case that my grade is equal to a will come down here and execute this code so console.writeline you pass and then if you want you can include a break statement here that'll break you out of the switch statement and then we also have another case for F which is console dot write line and then we also have a default so this is kind of like an else basically this will get executed if none of these cases are true and then you can put whatever code but if you have a default you always need to put this break there so it's that's required otherwise a compiler error so for example like since migraine is a you'll see I'm getting you past here if I set this to F ran the program again you'll see it says you fail if I set it to like a not valid grade like Z and now it'll tell me that so it'll be able to respond to all the different values of my grade a lot easier than it would with like an if statement c-sharp also allows us to use loops so over here I have a really basic while loop and a while loop is a loop that'll keep looping through a block of code while a certain condition is true so I have an integer index is equal to 1 and here I'm saying Wow index is less than or equal to 5 and then inside of these open and closed curly brackets we can just put whatever code we want to keep iterating over so I'm just writing out the index and then incrementing it you can see we get 1 through 5 so once that gets to 6 then the loop terminates and we're no longer going always on watch out for infinite loops so if I was to comment that guy out now we have an infinite loop because index is never getting incremented so it's always going to be less than equal to 5 so just keep that in mind when you're writing your while loops and another type of loop which is similar as a do-while loop so do while loops exactly like a while loop except the order is switched around so in a while we check the condition first and then we execute the code and a do-while loop we execute the code and then we check the condition so even if index up here was equal to 6 and you'll notice I have the same condition as I did in the while loop I'm still gonna be able to execute one iteration of the do-while loop because we execute the code first so I actually could code before I check the condition and you know what do while loops and while loops are essentially the same thing but in certain circumstances you're gonna want to use one over the other so now we can move on to another type of loop which is a for loop for loop similar to a while loop although it allows us to do a little bit more so and the for loop will allow us to declare a variable or define a variable that we can access throughout the loop so here I'm just saying for and then inside of these parentheses I want to specify three things the first thing is gonna be a variable declaration or instantiation so down here I'm just saying in I is equal to 0 and then this is gonna be like our indexing variable or a loop variable and then I can separate it with a semicolon here I'm specifying the loop condition of the loop guard so while I is less than 5 and then again separated by semicolon we have I plus plus so that's going to be a line of code that gets executed after every iteration of the for loop down here I'm just printing out console dot write line so we're printing out zero through four so the loop executes five times and these can be really useful you know just when you want to loop a specific number of times all right so in addition to a four loop we all can also have like a for each loop this is like what you call like an iterating loop so I have a array up here of lucky numbers and it's just all these numbers I could say for each and then I want to create a iterating variable so this is gonna be a variable that changes every time we go through the loop so it's gonna be an int called lucky num and I can say int lucky num in lucky number the structure that we want to loop through in our case it's an array and down here I can just say console.writeline lucky num so now when I run my program we're printing out all of the elements inside of that array using the for each loop so a while loop is a very general loop and then we have a more specific for loop and then we have an even more specific for each loop so you want to use whichever ones are appropriate at the appropriate time now let's take a look at exception catching so a lot of times in our c-sharp programs certain things are going to come up exceptions are gonna get thrown and our programs are gonna get terminated but the question becomes how do we mitigate that how can we handle those exceptions so over here I'm gonna spawn an exception for us you'll see here what I'm doing is I'm writing this is like a little program which is gonna allow the user to enter in a number so I'm dividing 10 by whatever number the user enters so over here when I run the program you'll see it's basically prompting me for a number if I put a 0 in here what this is gonna do is it's gonna try to divide 10 by 0 so this would be a situation where I'm trying to do a division but I'm doing division by 0 which is invalid so if I click enter you'll see that we get this little message here it says unhandled exception system dot / 0 exceptions so actually that makes this a little bit bigger / 0 exceptions so we get a specific exception so what about you guys how to do is mitigate that so we can use like try catches so let's get rid of this and down here I basically took that same line of code and wrapped it up in a try-catch block so over here it's basically saying same thing this is the same exact line up here I'm prompting the user to enter a number to divide but down here I'm saying catch and I'm specifying the specific exception that got thrown so right here what this is gonna do is it's gonna catch the / 0 exception so I'm specifying these specific exceptions so now if I divide by 0 if something gets thrown we'll execute the code down here but if any other exception gets them and this won't get executed and then over here I can just put exception II and basically this is gonna catch like any exception under the Sun so if you wanted your program to be like completely bulletproof you could put something like this and you can put multiple catch blocks like that now generally just putting the general exception here is a bad practice just because it's gonna catch everything and a lot of times you don't want to catch everything you're gonna want to you know only catch specific things or do specific things when certain exceptions occur but the point is now when I run my program and I put a 0 in here it'll catch it but you'll notice that it didn't crash like it's I'm printing out obviously e which is the error but I could also print out like you know something random and now if I enter it into 0 it just prints out hello so I'm able to prevent the program from crashing so is how we can catch exceptions but sometimes you're gonna want to throw exceptions so for example I could throw a division by 0 exception I can say throw new divide by 0 exception can't add numbers this is just gonna be like the message and these are actually classes so if you just kind of look up like the exception class in c-sharp these are all subclasses of that so now if I run my program we're actually going to throw an exception well throw that exception you can see the app stopped working and that message that I typed out is can't add numbers so that actually got displayed as well so that's kind of how we can work with exception catching ID now I want to talk to you guys about object orientation so we're gonna look at the ways that we can use objects and classes and stuff like that in c-sharp c-sharp is a fully open orion programming language which is awesome you can do everything that you could normally do in an object-oriented programming language so up here I actually created another class now I have class app over here and then up above it I created another class now a lot of times in c-sharp when you create a class you're going to want put it inside of its own file in fact that's like a generally a best practice is to put each class inside of its own file for our purposes I'm just putting it up in the same file so it's easier for us to see but we basically just say public class book and you could also put private here and we'll talk about what private does in a little bit but generally classes are gonna all gonna be capitalized just like book is up here so this is a class that would represent like a book and you can see we have a couple of attributes here like a title and author and these are both strings and then I also have a static attribute and a static attribute is an attribute that belongs to the class so both of these attributes are what we would call like instance attributes so these attributes will belong to a specific instance of a class but the static attribute belongs to the class itself and then down here we have a method which is just public void read book and what this does is it actually just prints out like what book the user's reading so it says reading and then I'm saying this dot title by this dot author and when I say this that keyword refers to the title or the author for the specific instance of the class that's calling it so that would be like the specific title or the specific author and then down here actually created an instance of the book class so I created a book object and I stored it inside of a variable so here I'm creating a variable and I'm storing inside of it a book object so when you create a variable we define the datatype I'm giving it a name book one I'm setting it equal to a new book object I can just say new book and then down here I can give this information so I can say like book one title is equal to Harry Potter book one author is equal to JK Rowling and then down here on that book one I could call that function so I could say book one dot read book and that's gonna print out reading Harry Potter by JK Rowling so this dot title represented Harry Potter and this dot author represented JK Rowling for that specific book instance and then down here I can print out the title so we're printing out Harry Potter and I can also print out the static attribute so remember the static attribute was defined on the class level so I can just say book this is the name of the class dot static attribute so with the static attribute I don't have to create an instance of the book class in order to access it I can just access it like I said at the class level just like that now I want to talk to you guys about constructors and a doctor is a special method that we can define in a class which is going to get executed when we create an instance of that class so up here in the book class I actually created another little method here it's just public book and you'll notice that the name of this method is the name of the class and when we name it like that it means that we're creating a constructor and again this methods gonna get called whenever we create an instance of the class and over here I'm passing I'm having the constructor taking a title and an author and I'm setting this title equal to the title that got passed in and this not authored or equal to the author that got passed in this is just an easier way for us to initialize our objects with initial information so down here I created a couple of books book book one is equal to new book and you'll see here when I say book right here this is me calling the constructor so since I'm calling the constructor I have to pass it a title and an author and then I'm just printing out the title you can see Harry Potter and then I made another one for the Lord of the Rings and it's important to note that you can have as many constructors as you want so I could have a constructor that takes in a title and an author I could have a constructor that just takes in a title or I could have a constructor that doesn't take in any information you could have multiple constructors so that's basically how you can use constructors to make your life easier so now I want to show you guys how we can implement getters and setters in c-sharp my getters and setters it's actually a design pattern that we can use which allows us to control access to the attributes of our classes so up here in the book class I added some getters and setters and you'll notice a couple of things I changed so up here these two attributes these used to be public but now they're private and that private keyword basically means that only code inside of the book class is gonna be able to access these attributes so if I have code outside the book class for example down here in the app class I'm not gonna be able to directly access those class attributes so I can't directly access the title and the author so in order to work around that what we can do is create public getters and setters so I have a method cat title and basically this is just gonna return the title from the class so this method is able to access the title because it's in the same class as a title but outside we wouldn't be able to access it so now if I want to get the title I can just say like book wand get title and you'll see I'm getting the title and then also one thing you want to make sure that you do is over here in the constructor whenever you're setting the title the author you want to make sure that you go through that setter so now I'm saying this top subtitle and I'm passing in the title so that's how you can implement getters and setters in c-sharp all right now let's take a look at another object-oriented programming concept which is inheritance and inheritance basically allows a class to inherit all the functionality and attributes of another class so up here I created two classes I have a public class chef and I have a public class Italian chef so these are two chefs that I'm representing in my program a general chef and an Italian chef the general chef has three methods so it has a make chicken method to make a salad method and a make special dish method and basically all those methods do is just print different things up onto the screen and then down here in my Italian chef class I'm actually inheriting all of the attributes and functions and methods from the chef class so I can just say public class Italian chef colon and then the name of the class that I want to inherit from and then now the Italian chef will inherit all the functionality from the chef class and you'll see the chef the Italian chef class has two methods the first is a make pasta method so the Italian chef can make pasta and then the second is that make special dish method which the Italian chef is overriding so I'm gonna come back to that in a sec but I just want to show you guys down here I created a chef object I created an Italian chef object and I'm having the chef make chicken and I'm also having the Italian chef and make chicken and they're both able to do that and you'll see down here I'm overriding this make special dish method so over here in the normal chef I have a make special dish and then down here in the Italian chef I have a make special dish and the way that I can specify that this can be overridden is up here in in this make special dish method in the superclass I can put this virtual keyword there that basically means that this method can be overridden and then down here in the Italian chef in the subclass I can put override here and that'll basically tell c-sharp that we're overriding a method from the superclass and you guys will see now if I change this to make special dish and I'll change this one to make special dishes well they're both going to make a different special dish so the chef makes us dish and the chef makes chicken parm so the chef just made a generic special dish and the Italian chef made chicken parm all right now I want to talk to you guys about another concept with inheritance which is using constructors and attributes so over here I have my public class chef and this is just like what we have before except now I'm adding in some additional attributes so the chef can have a name and can also have an age and see down here I have a constructor so passing in a name and an age and then just setting them pretty standard constructor and then down here in subclass so in the Italian chef class which is inheriting from the chef class I added in an extra attribute which is country of origin and so over here I have the constructor which I'm basically just defining and then I'm taking in a name and age and then that country of origin so down here I'm also putting a colon and then I'm saying the base name and age and this colon is coming right after this parentheses right there I just put it on a new line and what this is gonna do is it's gonna call the constructor from the superclass so when I say base and then I pass in name and age this is calling the appropriate constructor inside of the superclass inside of that chef class so I don't have to initialize the name in the age I'm gonna go ahead and let the superclass constructor do that for me but we still have this additional attribute country of origin so down here I'm just saying this country of origin is equal to country of origin so that's basically how I can have attributes in the superclass and in the subclass and then you know work with the different constructors and then down here and the main function or the main method I just have a might you know my chef's object passed in Gordon Ramsay and 50 then I have my Italian chef object and I passed in the name the age and I also passed in the country of origin and then over here I can just say my Italian chef country of origin and you can see we get Italy so basically all that's working and then also I could say like my Italian chef age and then if I rerun the program you'll see we get 55 so all that is wired up and that's basically how we can work with constructors and attributes in inheritance in c-sharp I now want to talk to you guys about another object-oriented programming concept abstract classes and attributes so over here I created an abstract class now for our class is basically a class where you can specify a specific methods where the subclasses have to implement them so here I have this abstract class of vehicle and you'll see I have a public abstract void move method so I declared this method but I didn't actually give it any information like I didn't actually define the method and then down here I have a public would get descriptions this is another method which I you know gave information to so this is a fully functional method and this up here is an abstract method basically that means that any sub classes like this bicycle class or this plane class that I created which are inheriting the vehicle class are gonna need to implement that so down here in each one of these like in this bicycle class that's a public override void move so down here I'm overriding that move method and you can see it just says the bicycle pedals forward and then in the plane class same thing or in the move method and it just says the plane flies to the sky sent down here I can come down create a plane object and I can say like my plane move or my plane get description and it says for move it says my the plane flies through the sky and for get description it says vehicles are used for transportation so that's kind of how we can use abstract classes and attributes and methods in c-sharp alright finally the last concept I want to talk to you guys about in c-sharp object orientation are interfaces and interface inheritance basically I can define a general interface which is essentially a fully abstract class with all abstract attributes and methods and then I can create sub classes which will implement that interface and they'll have to implement all of the methods and stuff like that so over here I have a public interface animal and I just defined one method which is just void speak and you'll notice that I'm not actually like giving this in any information or not really defining it and then down here I have a sub class dog which implements animal and you'll see over here I have a speak method and I'm just printing out woof woof because that's what a dog says and then I have another one for cat which again public bullying speak and it's printing out meow meow because that's what a cat says and then down here in my main program I created an array of animals so I said animal I called it animals and inside of this array I put two animal objects so I put a dog and I put a cat now I can put the dog and the cat inside of this array because technically they're both animals and then what I can do is I could say for each animal and animals I'll tell it to speak and you can see over here we run the program and it's the dog barking and the cat meowing so polymorphism or interface inheritance is really great because I can basically define a like overall interface and then other classes can implement that interface and then they're all considered to be the same type which in this case was animal so that's kind of everything that I wanted to touch on in this video obviously I didn't cover everything in c-sharp I mean you know you can spend hours and hours just going over the different things you can do but I think this gives you an overview of how to do a lot of the common things that you would want to do in a programming language in c-sharp and hopefully this kind of demystify c-sharp a little bit for you and gives you an idea of how it works some of the conventions and how we can do different things hey thanks for watching if you enjoyed the video please leave a like and subscribe to drop acad to be the first to know when we release new content also we're always looking to improve so if you have any constructive criticism or questions or anything leave a comment below finally if you're enjoying chopped Academy and you want to help us grow head over to draft Adam EECOM forward slash contribute and invest in our future
Info
Channel: undefined
Views: 19,002
Rating: 4.9872813 out of 5
Keywords: Programming
Id: irBHuMTDsNg
Channel Id: undefined
Length: 40min 55sec (2455 seconds)
Published: Tue Dec 05 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.