C# Tutorial 5 Classes & OOP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello internet and welcome to part 5 of my c-sharp video tutorial and this part of the tutorial we're going to cover structs classes fields methods constructor static static classes nullable types and a whole bunch more like always all the code and a transcript of the video is available in the description underneath of the video and I have a lot to do so let's get into it okay so basic setup again this is visual studio of course if you are on a Mac you can use the marin now the very first thing I'm going to talk about are Struck's now outside of main and down still inside my class I am going to define a struct and a struct is just a user-defined type that is going to allow you to contain multiple fields as well as methods so we are going to create a struct and we are going to call it rectangle and inside of this guy we are going to define some different fields and a field is just a way of describing whatever you are creating here here we're describing rectangles so I am going to define both a length as well as a width now you are also going to be able to create what is called a constructor method and it will allow you to set values to these fields that you have right here and the constructor method has exactly the same name as your struct so that will be rectangle and I'm going to define a double here and length and I'm going to give it a default value of 1 and then I'm going to create another one called W and it will have a default value of 1 as well now inside of here I can then take in any passed values or lack thereof and assign those to these field values right here and as you can see a constructor is just a way to initialize a new struct that is being created and then like I said we can also come in here and create methods and I'm going to create one that's going to return a double and it's going to be called area and it's just going to return whatever the length is times whatever the width is very simple now up inside of main now we are going to be able to in here and go and create a rectangle struct so we'll go rectangle now let's just call this rekt one we're going to be able to come in here and add values to it like I said like you saw right there I did not create one using this constructor but I'm going to show you how to do that in a second so I'm going to be able to go rect one and length and give this a value of 100 or 200 or whatever you want it to be and then go and also define the width and let's make this 50 and now what I'll be able to do is come in here and go and print out the area so we can go area of rect one and go and get that value and how we do it is just go rect one and we call our area method and that's going to output that on the screen now what we're going to do also is we're going to create another rectangle here but this time we are going to create one using our constructor and to do so you just go new rectangle we can pass 140 inside of there or whatever you want you actually don't need anything there because we define default values in our constructor and another thing that is important to know is if you assign one rectangle to another you are going to be setting the values and not creating a reference so if we would go something like rectangle 2 is equal to rect one and then change the value for length in rect one to say 33 or something like that that that is not going to affect anything that occurs in rec to just a couple different things and we can go here now let's go and print out the length instead so we'll say rec two links and then let's go and get the length from rec two and that's also how you would get values out of a struct Boop's I want to make sure this is set to rec too and what we're going to do here is just verify that rec twos length is not 33 even though it was assigned to the value of rect one just to prove that it's not reference and it's a value and now if we run this I'm going to see right here that the area was calculate dated for our rectangle and also that the link stayed at 200 just like we defined whenever the rect was first created all right so there's a rundown of how to create a struck how to define struct a struct constructor struct methods and how they pass by value instead of reference and a whole bunch of other different things and now what I want to talk about is object-oriented programming or more specifically how classes work now a class is going to model a real world object by defining the attributes of that real world object which we are going to call fields and as well the capabilities of that real world object which we're going to call methods now unlike with structs you're actually going to be able to inherit from a class and create a more specific subclass and as an example let's say we have a class called animal and we go and hair it from that and add additional capabilities to create a canine class and we're going to get more into that in the next part of the tutorial right now I just want to focus in on classes and how you create one is you come over here to project and you come down here and click on add class and that's going to create one and what I'm going to call this guide is animal like I just said there before so let's get rid of that and go animal and click on add and it's automatically going to open up all this stuff I don't need any of this so I'm just going to get rid of it to clear up of the desktop all right so the very first thing I'm going to do here is I am going to show you how to create the attributes or fields that all of our animal objects are going to have and also what I'm going to do here is I'm going to make these public right now in this part of the tutorial I'm going to explain some things about classes and objects and then in the next part of the tutorial I'm going to explain how to improve upon what we cover here I'm just going to keep everything very simple right now so what we're going to say is every animal object we ever create is going to have a name and also that it is going to have a sound that it can make so very very simple course there's more things an animal can do but our animals are going to just do these two things now also I am going to be able to create a constructor which is going to set default values for fields whenever any objects are created and there is a default constructor that is going to be created for every class that you create but I'm going to override that right now and I'm going to say that I want to go public and once again just like we did with Struck's the constructor is going to have exactly the same name as the class name and then inside of here I'm going to define some default values so I'm going to say no name is going to be the default value for name if it isn't passed in and sound is going to have a default value of no sounds now you're going to be able to create additional constructors as well and because I defined both these default values in here I technically don't have to create them but I just want to show you the fact that you can create them and you can overload constructors just like you're able to overload any other type of method like we saw previously so I'm going to go public and animal and string and with this one I am going to come in here and define what happens whenever they pass in only one strings which is going to be no name in this situation and how we're going to handle that now the very first thing I want to do is I want to be able to refer to my objects aversion and name but I also want to be able to refer to this version and name this attribute up here how I can do that is I can come in here and go this name is equal to the name that was passed inside of it now if this was called n like that I could just come in here and call this and and get rid of this and everything would still work as you can see there's no errors however I would prefer in this situation have the names be a little bit easier to read so I'm going to leave them exactly like that and then I'm also going to come in here and define the default sounds equal to no sounds because with this constructor they didn't pass in a sound all right pretty simple stuff and then I can handle the situation in which they passed in both a name and a sound remember don't need to do these because I I hope these default values up here just doing it just to show you that it can be done all right so then we can say string and sound is equal to no sounds like this and then we'll be able to come in here and have this B sound as well okay so a little bit of extra information in here that's not needed but just want to cover it just so it makes sense and of course make sure you have that quote in there now I said that our objects are going to define the capabilities for these different objects we're trying to model and one of them that would make sense here is to be able to allow our animals to make a sound so let's do that and it's not going to receive any parameters and it is just going to whatever is called anyway is going to come in here and print out whatever the name of our animal is and says and whatever that animals sound is and then if we want to go and get those guys we can just say name and sound we don't need to be this part in that situation we're also going to be able to have what are called a static fields as well as methods and these of course belong to the class and not the objects created for the class and it's very important to remember that a static field has the same value for all objects of the animal type that means if its value changes for one animal object it is going to change for all of them so let's say I want to keep track of every single animal that was ever created I'm going to give this a default value of zero and then I want to be able to operate or get that value out of the class so I'm going to say that I want to create a method that's going to get the gnome of animals that were created and also I am going to well we can just go return and here we can just say no more animals exactly like that well one thing we're going to have to do is go an increment number of animals inside of our constructor so that we know that a new animal object has been created and we could paste that right and we can this right here again there's a lot of repetitive code just because I'm trying to keep everything very simple next part of the tutorial I'll show you how to streamline this stuff a little bit just want to make it very easy to understand though ever now what I'm going to be able to do is come up inside of our main area or leave this file and go back into program dot CS and now I can create an animal object so let's say I want to create a fox I can zoom in here and I can go animal and fox is equal to new animal and this is going to call the constructor for us and then what I'm going to go do is say define our different values so I'm going to say name is red and the sound is equal to a wrong that's what a fox sounds like if you ever did wonder and you can do that and then throw a semicolon here at the end of it you'd also be able to come in here and just create animal Fox and new animal and then come in and do something like Fox name is equal to red like that but I like to do it in the other way just so that everything is all nice and neat okay so you can do it that way or you can do it this way you can do it any way that you makes you happy now we want to go and call our static method and output information on the screen about how many animal objects have been created so we're going to go number of animals and let's say we want to go and print that out on the screen just to show you how that static method is going to work and how you refer to static methods is you have to put the class name first and then get numb of animals and then of course it's a method so we put the parentheses there at the end and that is going to print that information out and if we run this guy let's see exactly what's going on a number of animals is equal to one and we would also of course be able to come in and let's print out let's refer back to this guy remember we created a method inside of here called make sound and it prints that information out we could be able to come in here back in the program and we can just go Fox make sounds that and it's automatically go print out the name and the sounds so read says wrong like that alright and if we create more animal objects this number is going to increment as well so there is an example of a static method in a static field inside of a class but I also want to show you how to create a static utility classes you're going to do it the same way you're going to go project and add class and there this comes up and let's say that I want to call or create a utility class called shape math and let's go and create it and there it is I don't need any of this stuff alright so here is shape map and just so you're aware well anytime you create a static class I'm going to call this public static class anytime you create a static class everything inside of it has to be a static method or a constant value so we're going to do is we're going to create a public static double and this is going to be called get area and it's going to receive a shape string which by default we are going to set to nothing and then it can receive this is some time just right now to my head so it can receive a length which is equal to zero and it can receive another length which is also we're going to set by default equal to zero then inside of it make sure you have this public here otherwise you won't be able to call this from outside of the class itself and here we can do for an example let's say we want to do a string comparison and ignore case we can go string equals and let's say they pass in rectangle as the beginning of the string we want to compare that to shape which is this guy right here was passed in obviously and then we want to define our string comparison and we're going to say ordinal and ignore case because we want them to be able to type in any type of rectangle and if it comes back that that indeed is true we are going to say return length one times length to see just a real simple example we're also going to be able to come in here and let's say we wanted to be able to work with triangles we could say else if and we could say string and we're going to do exactly the same things when let's just copy this equals to the little blue let's go to get all that paste that in there and if it comes back that this is going to be triangle well then we're going to be able to handle that as well and here we can just say return length one times and then we want to go length two divided by two of course we can do the same for other guys here now let's just go copy all this again if and let's say they type in a circle that is everything else there's fun and in this situation where you can go return 3.14159 times and here is the power function for our math functions and you just pass in whatever you want it to be to the power of two in this situation and otherwise I'm going to say else and I'm going to do something like return negative one just to return something all right so there you go there is an example of a static class and a static method inside of that now let's jump over into the program and use it alright so inside of main once again here we can just say console and right line and we can say area of rectangle and we're going to get that from our static class and to do so you just go shape math and call the function you want to work with and I can throw a rectangle in here like this and then the length and the width and if we run it we're going to see that that indeed works area of a rectangle comes back in 30 okay so there you go static method static class of static everything now because I have a little bit of time left I want to talk about nullable types now data types by default cannot have a value of null and why that's sometimes bad is null is often needed whenever we are willing to work with databases and it is very easy to make it so a data type will be able to hold a value of null all you need to do is put the question mark after it like that and then we could do something like random number is equal to null and that's going to be okay if we got rid of this you're going to see that that's not possible so the question work inside of there like this and then we'll be able to check for the value of an oldest by going if you can do it in a couple ways I'm going to show you two you can say equal to null like that and in this situation we could come in and put something out on the screen something like ranch num is null and also let's go and copy this we could come in and do something like if and then we'll put an exclamation mark there remember exclamation mark is going to turn throughs in two falses and falses intros here we're going to have random number and here we're going to command and go has value which is going to tell me if this actually has a value or if it is an all by default and here we're also going to Reeve random number is null and we're going to save it and run it and you're going to see it comes back as null both times so there you go guys that is an introduction destructs as well as classes and nullable types and all the static classes a whole bunch of other different things and in the next part of the tutorial I'm going to get very heavily into object-oriented programming and like always please leave your questions and comments below otherwise till next time
Info
Channel: Derek Banas
Views: 136,899
Rating: undefined out of 5
Keywords: C# Tutorial, C#, Learn C#, C# Programming, C# Classes, C# Static Classes
Id: GAvhe6oe-_4
Channel Id: undefined
Length: 19min 16sec (1156 seconds)
Published: Sat Jan 21 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.