Constructors | C# | Tutorial 26

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome to draft Academy my name is Mike in this tutorial I want to talk to you guys about using constructors in c-sharp classes now a constructor is basically a special method that we can put inside of a c-sharp class which is going to get called whenever we create an object of that class so anytime I create an object of a specific class the constructor method will get called and we can do different things so I'm gonna show you guys how we can leverage that constructor method in order to make it a lot easier for us to create objects off of the classes that we create in c-sharp so down here if you're not following along with the course I basically created a class over here called book and I just created this book class and I basically said that a book is gonna have a title and author and a number of pages that's kind of all this book is and then over here my program about CS and my main method I created two books so I created a Harry Potter book and I created a Lord of the Rings book now I want to show you guys how we can use a constructor and we're actually going to be able to use a constructor to make it a lot easier for us to create these objects but before we do that I just want to give you a introduction into what constructors are and kind of show you what's happening so over here in my class I'm actually gonna create a method down here I'm just gonna say public book I'm gonna make an open and closed parenthesis and open and close curly bracket now you'll notice that I said public and then I said the name of the class and this is what's called a constructor so anytime I make a method like this where I'm saying public and then the name of the class this means that this is going to be the constructor for this class and like I told you guys before the constructor is basically a special method inside of our class that's gonna get called when we create an object of this class so let me prove that to you guys I'm gonna come down here and I'm just gonna type out console.writeline and I'll make an open and close parenthesis and down here I'm just gonna write creating book okay very simple now over here in my program in my main method I'm gonna run this and what you're gonna see is every time I create a new book this line and this that is gonna get executed so I'm gonna run the program and you'll see over here it says creating book and creating book essentially what's happening is when I come down here and I say new book what I'm doing here is I'm actually calling that constructor method so this right here this line where I'm saying new book this book is directly linked to this method over here and again I'm gonna prove this to you so over here what I'm gonna do is I'm gonna say that this book method this constructor should accept one parameter so I'm gonna say string name that's all I'm gonna say and then down here I'm just gonna print out name so this method is accepting one parameter name and then down here we're gonna print it out now over here you'll notice I'm already getting errors and that's because I didn't include a parameter so I could come over here and say like Mike and then down here we can pass another name like Jean so now when I run my program it's gonna print out Mike and it's gonna print out John because I'm calling that book constructor awesome so not only does this call that constructor but from here we can also pass parameters into that constructor and that is extremely powerful so I'm gonna show you guys what we can do with that information now let's talk about the code that we have over here you'll notice that I have a bunch of code here so when I created this Harry Potter book it took me four lines of code right when I created this book to this lord of the rings' book took me another four lines of code total of eight eight lines of code just to create two objects right just to create two books I had to write out eight lines of code not only that I had to manually type out book one title is equal to Harry Potter book one author is equal to JK Ron like that's a huge drag you know having to do that every single time we want to create a book is extremely annoying and imagine if we were instead of creating two books we were trying to create ten or twelve or twenty books all right it would take forever because we'd have to manually go through and you know assign a title and an author and a number of pages so this is a totally valid way to create our object and give it some attributes but there's actually another way and we can use that constructor to do it so essentially what I want to do is I want to allow the caller to pass in a title an author and a number of pages into this book constructor so when they create the book I want them to be able to tell me what the title is gonna be what the author is gonna be and what the number of pages is gonna be that way they can just tell me right away and I can handle it up front and I did and then I don't have to say like book one title is equal to whatever but one dot author is equal to whatever so let's see if we can wire that up over here again this book constructor is gonna take in three parameters and we already saw before that over here we can pass parameters into here right so I can pass a parameter into here and it's gonna get passed into that constructor so I'm gonna say that this constructor is gonna take three parameters the first is gonna be a string and I'm gonna call this a title and this is gonna stand for argument title and you guys will see why I'm naming this differently in a second and then I'm gonna make another string I'm gonna call it a author and then I'm gonna make it int and I'm gonna call it a pages now essentially what's happening is whenever we create a book we need to pass in the title the author and the number of pages and so what I can do now is from inside of this constructor I can just come down here and I can say title is equal to a title and basically what I'm saying is this title up here the title of the object is going to be equal to the title that they passed in so whenever we create a new book now we're gonna pass in a title and down here I'm gonna say that the title of the object is gonna be equal to a title and so now instead of having to say like book 1 dot title is equal to whatever now I can just pass the title in over here and I'll just assign the title from over here so essentially doing this is the same as saying book 1 dot title is equal to whatever it's the same exact thing except I can just do it straight from in here so what we want to do is do this for each of these attributes so I can say author is equal to a author and then we can say pages is equal to a pages and I just want to point out you can name these whatever you want so I could even name this title if I wanted or I could name this you know whatever it doesn't matter I'm just using a title because it kind of shows that this is the argument that got passed in and it kind of distinguishes it from the actual attribute of the object but you can name it whatever you want all right so now when we go back over to our program you'll notice we're getting errors here and it's basically saying there's no argument given that corresponds to the required formal parameter in other words hey you need to pass in all this information now so what I can do is I can take this information from down here and I can pass it into here so I can pass in the title Harry Potter I can pass in the author JK Rowling and then I can pass in the number of pages 400 so instead of having to do that manually I can just get rid of all this and now we have the same thing so I'm passing in the title the author and the number of pages and then over here in the book I'm taking those parameters that got passed in and I'm assigning them to the attributes of the object and that is super cool and super useful so let's do the same thing for Lord of the Rings we can copy this paste this bad boy up here so I can get rid of all of this stuff now and essentially we have the same exact thing so we went from having eight lines of code to create these two objects so now we just have two lines of code one each and we're passing in all of these different attributes so this should actually work and I'm gonna go ahead and prove that to you so I'm gonna say console.writeline and I'm gonna go ahead and just print out like book two dot I don't know title and so you'll see that this attribute got set so now I can run my program and we get a load of the Rings so it's doing exactly the same thing now I also want to show you if you want you can modify any of these values so I can technically come down here and say book two dot title is equal to the Hobbit and now this is gonna update book twos title so now when we print it out it's not gonna be Lord of the Rings anymore it's gonna be the Hobbit so that's kind of how this constructor works and that constructor is super powerful it's an amazing way for us to be able to create these objects really quickly and I also do want to point out that you can have more than one constructor so I have this constructor book and it takes a title an author and a number of pages but if I wanted I could just create another one so I could say like public book and this could take no parameters and I'd still be able to do this and so now I can have that and I'll still be able to create a book just by saying book book 3 is equal to new book so now I'm able to create the book like this with no parameters with no arguments or I can create the book with a bunch of arguments so you can create as many of these constructors that you want and yeah that's kind of the basics of constructors these are very powerful we want to do is just play around with these right I mean as long as you understand what's going on like this constructor over here is directly related to me saying book over here then you kind of like have a good understanding of what these are 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 released 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 Kadim e-comm forward slash contribute and invest in our future
Info
Channel: Mike Dane
Views: 32,704
Rating: undefined out of 5
Keywords: Programming
Id: Reeefq-Nxkk
Channel Id: undefined
Length: 10min 29sec (629 seconds)
Published: Wed Nov 08 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.