INTRODUCTION
In this Article We will Explain about Following types of Concept
- What is Classes
- Purpose of Class Constructor
- OverLoading Class Constructor
- Destructors
What is Classes ?
Class Consists of Data and behavior.Class Data Reperseneted Its by fields and Behavior is Represented by Methods .if you want Create complex Custom types then we can use make Use Classes.What is Mean Complex custom type lets say for Example if i want store number if can do that with integer.if i want store Customer personal information i can use classes. how to do we create class we use Class KeyWord followed by the Name.
Purpose Of Class Constructor
The Purpose Of class Constructor is to initialize class Fields. A class Constructor is Automatically Called when an instance of class is created.
Constructor Do not have return values and always have same of the class .constructors are not mandatory.if we do not provide constructor default parameter less constructor is automatically Provided .Constructors can be overloaded by the number and type of parameters.
Constructor is not mandatory.if we not provide Constructor Class automatically Called Default Constructor.
Constructor Basicllay used Instalize class Fields.Let Say Example of the Code For Your clear Explain,
-
- public Student(): this("No First name Provided", " No Lastname Provided") {}
-
- public Student(string Firstname, string Lastname) {
- this._firstname = Firstname;
- this._lastname = Lastname;
- }
Destructor
Destructor have the Same name as the class with ~ symbol in front of them.Destructor have Dont take any parameter and do not return value . destructor normally called when the C# Garbage collector Decides to clean your object from memory. class have only on Destructor . we cannot called Destructor its invoke automatically .Destructor Dont have parameter.
Finally we see Whole code .how To invoke Class To main class. if want Class invoke Class we need To create Instance of Class we use new Keyword .when create Instance of Class Constructor Automatically called
- using ClassSample;
- using System;
- namespace ClassSample {
- public class Student {
- string _firstname;
- string _lastname;
-
- public Student(): this("No First name Provided", " No Lastname Provided") {}
-
- public Student(string Firstname, string Lastname) {
- this._firstname = Firstname;
- this._lastname = Lastname;
- }
-
- public void printfulname() {
- Console.WriteLine("FullName={0}", this._firstname + "" + this._lastname);
- Console.ReadLine();
- }
-
- ~Student() {
-
- }
- }
- }
- class Program {
- public static void Main(string[] args) {
-
- Student S2 = new Student();
- S2.printfulname();
-
- Student S1 = new Student("Thenn", "arasu");
- S1.printfulname();
- }
- }
the Compile The Run the We can See Two result One Default Constructor And other Parameter Constructor .its showing Console Application,
Conculsion
I hope your Understanding How To use Class ,Constructor and Default Constructor.please share yours valuable feedback and Comments to Improve my Future Article. thanks For Reading My Article.