6: How To Create Classes And Objects In C# | C# Tutorial For Beginners | C Sharp Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
when we create projects using c-sharp we're gonna have to create classes at some point because we use classes as blueprints in order to create many different types of objects inside our projects so in this episode we're going to be discussing what exactly classes are and how we can create them for the sake of this episode and went and created a small diagram to sort of illustrate how a class would actually look like if we were to create a class now you should see a class as a type of blueprints and based on that blueprint we can create many different objects that are just copies of that specific blueprint it's kind of like building a house you know you have a blueprint of how the house is supposed to look like and then the construction worker can go ahead and start building many different houses based on that blueprint and that's basically what a class is now I will be looking down here when I have to describe this diagram because on my screen is over there but on your screen is going to be over there so just so you don't get confused there but as you can see inside the diagram you have a couple of different things first of all I created a class called person and as the title says this is basically just going to be a blueprint of a person so what we can do here is we can fill this blueprint with different fields and different methods now a field is a type of information so it's for example what is the name of the person what is the age of the person does the person have any sort of pets through a false statement so we can fill in a bunch of information here now when we want the person to do something or something has to be updated about this person then we use method so that's basically what we do when we create a class you know this sort of blueprint you create fields and we create methods now in a previous episode I did mention properties and methods instead of fields and methods the basic difference here is that a field is just an empty field that we create inside one of these classes here as part of the blueprint whereas the property is when we later on need to add some kind of value to it by actually using these fields in some kind of way so a properties when we later on have to actually use it and add some value to it or change something regarding these fields here just in case you got a little bit confused about that after we created a class we can create objects that are going to be copies of that specific class or the blueprint just created and we do that by instantiating the class now this has to be done in a specific way and I will show you how to do but just know for now that when we have a class we can create many different objects we can create you know one two and create a thousand different objects based off this class if you want to might not be good for the memory to do that but we can if you want to so when we have this blueprint we can create many different copies and just fill in the information that we added into the class so we can create a person based off the diagram here that might be called John and John might have a certain name you might have a certain age you might have a certain number of pets and we can fill that information into a new object and then create John based off this class here so I'm going to show you how to do that now before we can actually get to create a class and objects we also need to learn about something called access modifiers and this is something you're going to be using constantly whenever you're going to create classes when you're going to create different types of fields methods that sort of thing now what an access modifier actually is it's a keyword that we put in front of our classes we put them in front of our methods in front of our fields in order to tell the program where we can access this specific piece of information from and in most cases we're not gonna be using all these different access modifiers that I'm showing here on the side we're just going to be using just about three of them which is going to be public private and protected for now I'm not gonna talk too much about access modifiers because we already talked about classes and objects and how to instantiate a class but we haven't even tried doing it yet inside our code so we're not gonna talk too much about it but just know for now that when we used to pop the keyword it means that we can gain X's it's basically just a way for us to tell the the code how we can access this piece of information so when we write public it means that we can access it from anywhere within the assembly if it were to make it private then we can only access this information from within the same class if we were to make it protected it means that we can only access this information from the same class or any sort of classes that is derived from this specific class and again don't get too much into it if you don't understand what I just said just for now know that we're going to be talking about mostly public for these examples here which means that we can access the information from within this assembly that were working inside of right now so as you can see we're back inside visual studio what we're going to do in here so we're going to create our own class because right now we do already have a class in here but this is the class that the program created for us and when we set up the project for the first time and the reason we don't have a public access modifier in front of this one or just any kind of access modifiers then what to do this right here it's because this specific class is what we actually run we run the application so we don't actually access any information from inside this class it just simply gets run so when we create any other classes ourselves then it might contain information we have to use within this class here when we run the application so then we need to tell it how should we actually access this specific class should it be public should it be protected should it be blah blah blah so when we create additional classes here we do need to create a access modifier and this is basically why we need to talk about access modifiers before we got into it because we have to create an access modifier so even though we don't get too much into it we need to just briefly discuss it here so what we're going to do is we're going to create our first class which is going to be a blueprint and we're going to just go and take the same example as we did with the diagram that I had so I'm just gonna go and create a class called person so the first thing I'm going to do here is I'm going to create a public class then I'm going to call it person then we're going to use the curly brackets just to sort of layout that we have a scope for this class a scope is whatever is going to be within the curly bracket just simply tells it that whatever is inside this scope belongs to this class here so inside the scope we can actually go and create these fields we can create methods we can you know sort of insert information that has to be part of the blueprint so we can create a field we might call this a public string name then we could also create one that might be called public maybe not a string what did we have inside the diagram we had an age so this has to be an integer so public int age and then we also had whether or not we had a pet or not so we can save public then we can say boolean so that will be bull has hits and there we go you can also go and create a method so we can go ahead and say we have a public then we have a method we can just go ahead and call this one something like let's just gotten colleague readings for now because we need to just have some kind of name here so we're gonna say greeting parenthesis because it's a method so we need to add parenthesis behind it because we need to be able to pass in information if we need to later on then we'd say curly brackets to give it a scope now there is something else we need to talk about it before we continue with just a keyword called void which means nothing now what we need to determine here is whether or not we're going to return some kind of value inside this method here if we don't plan on returning a value then we need to use the void keyword so we need to go up here and actually write void greeting and that could be an example where we just simply write console the dots right line it's right there and if we were to write something out into the console I'm writing stuff into the console but I'm not returning a value we take and then use later on to do something else with we're just simply writing stuff into the console which is not returning something so if I were to do something else and again this is just an example because I want to see what void actually means because you will be using void quite often if I were to go in here and say well instead I do actually want to return a value I can go in here and say that I want to when we use this method have a that's an integer called X and I want to have an integer called Y and what I want to do using this specific method here which I could call calc instead to calculate something what I could do is I could go in here and I could create a integer variable type so I could say we have an int called number and I could set this one equal to X plus y and then underneath that I could return that value so if I were to return it and say I want to return number like so we also need to go ahead and add what kind of because right now it's going to give me an error message because it will actually return earning something but I am using avoid keywords which means that we don't plan on returning something so if I go ahead and delete this and then instead add the data type that I'm planning on returning so in this case it would be a integer then in this case here we don't need to use for it because instead I'm creating a method that I plan to return some kind of data from and I do that with the return keyword and by not using void as a keyword up here when I create the method instead I just simply write what kind of data a plan on returning so I just wanted to mention that because there has been some discussions whether or not we should use void or not use void because it did showed in a previous example and so what I'm just going to do here is I'm just going to go ahead and just go back again to our previous method and I'm just gonna stick with public void greetings because we're not going to return any sort of information but I just wanted to give you an example here so what I'm going to do is I'm going to go ahead and say we want to console dot write line and then I want to give it some kind of information inside the console here so what I could do is that could return some kind of string that says hi my name is and then we can go ahead and just add some information here so we use the plus symbol in order to do that and then I can just go and use some of the information that I have up inside our field now I know we haven't assigned any sort of name or anything to this yet but remember this is just a blueprint that we're going to use for later so later on we're gonna start assigning values to this so what I could say is I want to just simply say that my name is name then I want to add another string by using the plus key word here so we're just gonna you know attach this together if you could say that I did add a space here if you just notice that I added a space because otherwise it's not gonna be a space after the name and the next string they were adding in here and then we can say and my age is space then we add a another piece of data here which could be the age and then we just simply return this so having this is actually a class we can actually do something with now now of course if we were to try and run this is not really going to give me any sort of information inside the console because remember the one class that we do actually run when we do actually run this program is the one that is down here and we haven't told it to run anything regarding this up here this is just by itself sitting up here so we need to do something here so what we need to do is we need to go down inside the actual program class which is going to get run and in here we need to instantiate the class if it just created up there and the way we do this is by referring to the actual class that we created called person so if we were to go up here you can see it's called person up there and then we need to give it some kind of name just like when we create any kind of variable so if we were to create a string or an integer then we always write int and then the name of it and then we do something with it set it equal to something so the same here we need to create what type of data are we creating well we're creating a instantiated object from this class up here and we need to give it some kind of name so in this case I'm just going to go ahead and call it something like this is going to call it person just for now then we need to set it equal to new person parenthesis now you might be asking what exactly did we do at the end here with the new keyword to be wrote here well basically we just allocated a new memory inside our application for this specific object that is based off the class you created up there so now we stored off some memory inside the application so it has said for what we want to create this object here that's basically all we had to do and this is something we need to do every single time we want to instantiate a new object or stands here a new class to create an object inside applications so now that we have an object we can actually go and start doing things with it so for example we do have some fields up here that doesn't have any kind of data inside of it we could start filling that in if you wanted to do that so we would to go back down here we could actually go ahead and start filling in that information so we could say well we do have this optic called person and I want to fill in the information for for example the name so we say dot name it's going to be equal to some kind of string because remember we wrote that it had to be a string datatype up here so what I'm going to do is I'm just simply gonna that were creating a name called John and then I could start filling in the other information as well so we have person we just named the optic were referring to and then I want to fill in the age set it equal to let's say 28 and then I want to fill in if he has a pet or not so we can go ahead and say person dots has pets it's going to be equal to let's just say it's true and there we go so now we have some information filled in about this specific person here now just to sort of separate this that's actually going to do that so this is when we instantiate the object and then we start filling in information and we can also if you want to do so actually console lock this out because remember this specific method we created up here just simply takes the name and it takes the age that we have filled in regarding this object or at least the optic-based of this class up here and writes it out into the console so if I were to go down here and just simply run this method that we created up here I could go ahead and say we have the person object then I'm going to write the name of the method so it's called reading without s parentheses and now it is going to write that out inside our console if you were to go ahead and run the application and it's going to do that because we have the console.writeline inside the method and we're running the method inside our starting class down here that gets run once you do actually start up the application so versus a control f5 well actually notice that we have hi my name is John and my age is 28 inside the console so this is basically what I wanted to serve in this episode in the next episode we're going to talk about moving around some of these classes in different files and creating some different directories for them because this is also something that we have to do from time to time then afterwards we're going to start talking about something called static methods which is a way for us to gain access to certain methods without having to instantiate the class first and there's a different constant that goes behind when you do this and when you do not do this and we're going to talk about that in that episode so for now this is what I wanted to show you this is how we create classes and how we create objects based off a class which is a blueprint like we talked about so hope you enjoyed and I'll see you in the next episode [Music] [Music]
Info
Channel: Dani Krossing
Views: 152,395
Rating: 4.9528275 out of 5
Keywords: visual studio, visual studio setup c#, C# Tutorial For Beginners, c sharp tutorial for beginners, What Is C#, C# Explained, what is c sharp, c sharp explained, learn c#, learn c sharp, tutorial, mmtuts, beginner friendly c sharp course, c#, c sharp, c# tutorial, c sharp tutorial, beginner course in c#, beginner course in c sharp, learn c sharp easily, learn c# easily, class, object, classes in c#, objects in c#, c sharp class, c sharp object, instantiate class in c#
Id: t2SPg6IuT3k
Channel Id: undefined
Length: 15min 59sec (959 seconds)
Published: Tue Mar 05 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.