Part 19 - C# Tutorial - Introduction to classes.avi

Part 19 – C# Tutorial – Introduction to classes.avi



Text version of the video

Slides

All C# Text Articles

All C# Slides

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic

In this part of the c sharp tutorial we will learn
1. What a class is
2. Purpose of a class constructor
3. Overloading class constructor
4. Understanding this keyword
5. Destructors

hello welcome to prism technologies I am rancor this is part 19 introduction to classes in this session we will learn what it classes purpose of a class constructor overloading class constructors understanding this keyword and destructors so what is a class so far in this video tutorial we have seen very simple data types like individual flu double boolean etc now if you want to create a complex custom type then we can make use of classes what do we mean by a complex custom type let's say for example if I want to student number I can do that using an integer variable but if I want to do my customer information then I can make use of classes what kind of information this customer has has first name last name date of birth email etc now using the buildin feels we can create a class and also not only a customer class you know it not only contains data it can also do certain things like save the customer to the database print the customer full name so essentially a class consists of data represented by its fields and behavior represented by the methods ok so a class has state and behavior status nothing with the data behavior is nothing but what is the class capable of doing for example if we can save a customer to a database we can print his full name etc let's look at an example you know which will make things clearer okay so how do we create a class to create a class we use the class keyword followed by the name of the class so class customer and all of us know customer will have first name and last name so let's create them string first name actually we will use an underscore string first name string last name so these two fields represent the state the data that this class is having okay the first name and last name okay now to initialize these fields a class can also have a constructor so how does a constructor look like a constructor will have the same name as that of your class okay so public public is nothing but the access modifier we will be talking about access modifiers in a later session in a great detail so access modifier and then the name of the class public customer and what is the purpose of constructors in life constructors are basically used to initialize your class fields okay we will later understand and embed how we use this constructor okay so for now I am passing two parameters into this constructor string first name string last name okay so this is the constructor it's having the same name as that of your class okay and it can take parameters but a constructor does not return a value whereas a method can have a return type and if your method doesn't return anything you know the return type is void otherwise you can have any return type like int float double boolean etc but a constructor you know the way you can identify this as a constructor is by looking at its name it will have the same name as that of class and it will not have a return type but a construct can take parameters now I am actually using this constructor to initialize these two class fields first name and last name so I can simply say underscore so this field is equal to the parameter that we are passing it in to this constructor similarly underscore last name is equal to the parameter that we are passing in to this constructor okay now you can refer to this this class field and I'll just play its name or you can say this dot and look at this the moment I say this dot the intellisense choose both the class fields underscore first name underscore last name okay so now this keyword actually refers to an instance of this class to an object of this class okay so you can either say you know underscore first name or you know for better readability you can use this keyword this dot last name okay so basically we are using this constructor to initialize both of these class fields with these parameters that we are passing in to this constructor and this constructor will get called automatically when we create an instance of this class we'll have a look at that in a bit okay so so far we have two fields and a constructor to initialize those two fields so a class has some state right now represented by these two fields let's say I want to add some behavior in the sense the class should be capable of doing something for example I want this class to be in a position to print the full name of a customer okay so for that let's have a function let's have a method and the return type of the method is void public void and I'm going to call this method print full name so what is this method going to do this method is going to pray the full name of this customer okay so how do we do that console dot write line full name is equal to underscore first name have its place in between first name and last name underscore last name again if you want to refer to this class Fields you can just you know type in the fields just like this or for better readability you can use the disk keyword and as we know this keyword refers to an instance of this class to an institution to an object of this class okay so if you look at our class now it has got two wheels a constructor and a method apart from that a class can also have destructors and destructors will have the same name as that of your class but they don't take parameters they cannot have written type and they have it tilled in front of them now usually in c-sharp we don't require destructors and the objects you know basically we use destructors to clean up the resources to clean up any resources your class was holding on to during its lifetime okay so any code to clean up those resources goes in this destructor and these destructors we don't have to call them these destructors are called automatically by the garbage collector you know when it when it kind of tries to clean objects from the memory we will be talking about destructors and garbage collection in a very great detail in a later session for now just understand that a destructor will have the same name as that of your class it cannot take parameters and it has a tilde symbol in front of that and destructors are basically used to clean up any resources that your class was holding on to during its lifetime and these are automatically called by the garbage collector when turns up to clean your objects from the memory okay so your cleanup core goes here and this is your method you know which represents the behavior this is your constructor you know basically use to initialize your class fields now if I want to use this class I can do that in our main method okay so we can go ahead and create it an instance of this class customer c1 is equal to new customer now look at this I am creating c1 is nothing but an instance of this class so I'm creating an instance of customer class using the new keyword this instance is also called s object object an instance these terms are used interchangeable so I'm creating a new customer object now look at this the moment I open up this paranthesis it shows up our constructor this constructor which takes in first name and last name and let's say for example I am passing in the first name is praveen and the last name as technologies so what's going to happen now this Pragya will be passed into this parameter technologies will be passed into this type parameter and these parameters are then used to initialize these two class wheels so understand that you know this is the constructor which is getting called automatically when we are creating an instance of this customer class okay so what our constructors constructors are called automatically when you create an instance with your class and what is the purpose of this constructor it is basically used to initialize your class fields you know this dot first name this dot last name will now be initialized to first name to regime and last name to technologies so that you know makes it clear the definition of a constructor is called automatically when you create an instance of this class and the constructor is basically used to initialize that those class members okay and then if you want to print the customer full name you just say c1 dot print full name and if you look at this you know after we create this object we are calling the print full name so now in this customer object the state which is the first name and last name fields are initialized to presume and technologies and your print full name is actually concatenating them together and writing them out to the console okay so your constructor is ensuring that this fields are populated before we actually use those fields in this method okay so that proves that a constructor is used to initialize your class fields and it's automatically called and you try to create an instance with your class okay so if we go ahead and run this program as you might expect full name is equal to present technologies now is it mandatory that we have to provide a constructor every time we create a class not necessarily for example let's comment this in our constructor and you can do that by clicking that button so once I comment that obviously you know since we don't have that constructor you know it gives an error there is no you know if you look at the error customer does not contain a constructor that takes two arguments there is no constructor which takes two arguments so now we can't do that but what I can do is look at this when I say new customer automatically I am getting a constructor without any parameters if you look at this customer open parentheses close balances it doesn't take any parameters so what does that mean if you do not write a constructor if you do not provide a constructor for your class dotnet framework will provide one for you for free okay and it is a default parameter less constructor okay there are no parameters the default constructor whether by dotnet framework is a parameter less default constructor okay and this default constructor what it does is basically walks through all the fields in the class and initialize them to their defaults now look at this when I call this default customer this first name and last name they are not initialized to anything okay they are basically empty so now if I go ahead and run this application look at what's going to happen full name is equal to empty and now somebody is able to use you know call this method in spite of those fields not being initialized that's why you have that full name is equal to empty okay so this default parameter les constructor is not really that useful and if you want to make that a little more useful what you can do is basically you can you can provide you know let's say I have this constructor but another thing to keep in mind look at this the moment I provide a constructor and I have provided a constructor for my own class and the moment I have done that look at look at what happens to the default parameter les constructor no customer does not contain a constructor that takes zero arguments so what's happening here is that once we provide our own constructor to this class you know dotnet takes away the default parameter les constructor which it has provided when your class doesn't have any constructor okay so so it's taking away that constructor that's why it says the data message customer does not contain a constructor that takes zero arguments okay so now let's say we want to provide that capability you know somebody should be able to create a customer object without passing in any parameters to the constructor so how do you do that okay you can actually write a constructor which does not take any arguments something like this public customer and then what you can basically do is when you call this customer I mean when somebody calls that constructor I want basically I want basically the constructor which takes two parameters to be called but then I'm going to initialize them to their defaults like you know no first name provide it and similarly for the last name we will say no last name provided okay so what we are doing here is basically okay if somebody calls this default you know if somebody wants to create a customer object without passing in parameters that's fine they can do that like this okay customer c1 is equal to new customer now when this if you right-click on that and when you say koutou definition comes to this constructor but if you look at this constructor it's calling this customer this constructor this refers to this instance of the class so this here refers to this particular constructor and passing in no first name provided first name and no last name provided for last name properties and these are then used to initialize these class fields so obviously now if somebody is creating a customer object like this and if you go ahead and run that look at that no first name provided is the first name no last name provided is the last name okay so if you want to make that default parameter less constructor a little more useful and you want to make sure that your class fields are initialized to something before you know somebody calls methods and do something in that class then you can provide an implementation for that parameter less constructor and look any of the you know parameter to construct any of the constructors with parameters to initialize your class fields the way you want them okay but on the other hand you know if somebody else wants to create a customer another customer object they can do customer c2 is equal to new customer and now look at this there are two constructors now one constructor which does not if you look at this one of two constructors one constructor if you look on the right side no parameters a parameter less constructed there is no need to pass parameters I can do that or there is another constructor which takes first name and last name so there are two constructors with the same name now differing by just a number of parameters so this is called as overloading constructors so this class has two constructors one which does not take parameters at all another one which takes two parameters okay basically we can overload a constructor you know on the number and type of parameters okay say for example P is my first name and maybe T is my last name and if I say c2 dot print full name so for the second customer it will say PT P is the first name T is the last name so that's it let's go back to the slides so bubbles of a class constructor the purpose of a class constructor is to initialize your class fields a class constructor is automatically called when an instance of a class is created constructors do not have written values and always have the same name as that of your class yeah that's that's one important difference to keep between a constructor and a class methods I'm sorry constructor and methods methods will have written type it as your constructor it doesn't have your doesn't have any return type and they have the same name as that of your class constructors are mandatory if we do not provide a constructor a default parameter less constructor is automatically provided by dot net framework but once we provide an implementation you know of a constructor the default parameter less constructor a constructor provided by dotnet framework is taken away and constructors can be overloaded by the number and type of parameters and we have seen an example of that as well so what our destructors destructors have the same name as that of your class with a tilde symbol in front of them they don't take any parameters and they don't have written values as well destructors are places where you put could release any resources your class was holding on to during its lifetime we will actually talk about destructors and the process of garbage collection in a later session that's it for today thank you for listening have a great day

26 thoughts on “Part 19 – C# Tutorial – Introduction to classes.avi

  1. after few yrs of confusion i just understand now few things i was unable to grasp. very useful vid thanks alot.

  2. why do we need to give 'this' in 'Print Method'? can some one tell me? I used the local variables with out 'this'. in print method. instead of this._firstName, i have declared ._firstName. this gave me same output.

  3. if we use static keyword for PrintFullName() method and if we call this in main method using class name, error is shown in PrintFullName() method….can u plz help me to solve??

  4. My gratitude to the best teacher ever you can not imagine how helpful your videos are for beginners thank you again

  5. I had a spam advertisement for GRAMMARLY that you use to correct spelling? This is software that can listen for credit card numbers or anything you type, ever, its sent to the cloud for review. You think its only going to listen to when you type letters and its not going to track anything? whatever you type in secret? or buy? the app is able to catch any and all keyboard keys, then send it to some website. In the old days you would download the file that ran local on your computer, cloud processing was virtually impossible, now its easy

  6. why are there not 2 placeholders (6:51) in WriteLine("Fullname= {0}",this._firstname + this.Lastname) is this.lastname also not an variable?

  7. I got the Concept of Class, Constructor, and Destructor but it's a bit confusing. It's difficult to absorb everything at once.
    But as always, very good.

Leave a Reply