Lesson 15: Training CONSTRUCTOR and DESTRUCTOR in Language #C

This is our lesson no 15 and today I will Teaches you about Training CONSTRUCTOR and DESTRUCTOR in Language #C.

Training CONSTRUCTOR and DESTRUCTOR in Language #C

When an object or a class of objects runs, a particular procedure called the constructive function or function Object() { [native code] } is called. In fact, the constructive function is used to set up variables or run a fixed programme.

Because a manufacturer’s function never contains the amount of output or return, no variable direction RETURN is defined in its definition.

The following is the overall structure of the function Object() { [native code] } definition function in classrooms #C.

public string Describe()

You can also define the builder function as follows:

public Car()

In this lesson’s practical example, we have such a class called Class Car, which contains a constructive function that takes a text parameter as input and returns a string.

Constructive functions in #C can, of course, be overloaded. That example, a class may have many builder functions, but their input arguments may differ. An action code is shown in the class below:

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

A constructive function can invoke another manufacturer function, such as the code below. In this regard, an example has been provided:

public Car()
{
Console.WriteLine(“Constructor with no parameters called!”);
}
public Car(string color) : this()
{
this.color = color;
Console.WriteLine(“Constructor with color parameter called!”);
}

When you run the sample method above, you’ll notice that the function Object() { [native code] } function, which has no parameters as input, is the first to run. Objects (s) are initialized with a default builder function in this mode.

If you really want to call the parameter’s function Object() { [native code] } function, write the following code:

public Car(string color) : this()
{
this.color = color;
Console.WriteLine(“Constructor with color parameter called!”);
}
public Car(string param1, string param2) : this(param1)
{
}

Note: If you call a builder function with two parameters, the first parameter is utilized to call the function with one parameter.

Teaching work with Destructive function or Destractor in #c:

The destructive function or Destractor in Language #C is a method that runs when the object from the class Become Language #C is an automated position of the system or collector Garbage, which means Object Clears that you do not need any other program to empty memory and release the system.

On the other hand in some cases maybe you need to do a UP Clean in the system, here is that Destructor degradation functions are used.

Destructive functions are not like other methods in the #C language. In the action code below an example of the destruction function The show is shown:

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

As soon as the object or Object created from the class, collected by automatic cleaner collector Garbage The above method is called.

%d bloggers like this: