Lesson 15 Teaching Constructor and Destructor in C#

This is our 15th lesson, and now I’m trying to demonstrate you how to teaching constructor and destructor in C#. The function Object() { [native code] } function is a specific method that is called every time an object from the class is created. In actuality, the function Object() { [native code] } function is used to set up variables or run a set of instructions.

Teaching Constructor and Destructor in C#

A constructor function never has a return value and therefore no definition variable in its definition
return is not defined. The general structure of the constructor definition function in C # classes is as follows.

public string Describe()

You can also define the constructor function as follows:

public Car()

In the practical example of this lesson, we have a class called Class Car that has a constructor function that is a text parameter Receives a string as input. Of course, constructor functions in C can also be overloaded.

This means that we have several constructor functions with the same name for a class, but their input parameters are the same be different. The following class shows an action code:

public Car()

{

}
public Car(string color)
{
this.color = color;
}

A constructor function can call another constructor function, which in different ways may be code under one
An example is provided in this regard:

public Car ()
{
Console.WriteLine (“Constructor with no parameters called!”);
}
public Car (string color): this ()
{
this.color = color;
Console.WriteLine (“Constructor with color parameter called!”);
}
If you run the method of the above example, you will see that the constructor function that has no parameters as input, first Runs. From this mode to set objects of a class with a default constructor function Used.

If you want the constructor function of the parameter to be called, you can do the following code: public Car(string color) : this()
11
{
this.color = color;
Console.WriteLine(“Constructor with color parameter called!”);
}
public Car(string param1, string param2) : this(param1)
{
}

Note: If you call a constructor function that has 2 parameters, the first parameter to call the function
A constructor that has 1 parameter is used.

Learn how to work with the Destractor function in C #: The Destractor function in C # is a method that executes when an object in a class disappears.

Be. C # is an collector garbage language, meaning object Clears items you no longer need in the program to free up memory and free up the system.

On the other hand in some cases you may need to do a clean up on the system, here it is
Destructor functions are used.

Destructive functions are not very similar to other methods in C #. In the practical code below is an example of a destruction function Shown:

~ Car ()
{
Console.WriteLine (“Out ..”);
}

As soon as the object created from the class is collected by the collector garbage automatic cleaner
The above method is called.

%d bloggers like this: