Constructor

xiaoxiao2021-04-07  315

C # programmer reference

Use Constructor (C # Programming Guide)

The constructor is a class method that executes when creating a given type object. The constructor has the same name as the class, which typically initializes the new object's data member.

In the following example, a class with a simple constructor is defined, a class named Taxi. Then use the New operator to instantiate the class. After allocating memory for the new object, the New operator immediately calls the Taxi constructor.

C #

Copy code

Public Class Taxi

{

Public bool isinitialized;

Public taxi ()

{

Isinitialized = true;

}

}

Class testTaxi

{

Static void main ()

{

Taxi t = new TAXI ();

System.console.writeline (T.isinitialized);

}

}

The constructor without parameters is called "default constructor". Whenever, just use the New operator to instantiate the object, and not provide any parameters for the New, the default constructor is called. For more information, see the instance constructor.

Unless the class is static, the C # compiler will provide a public default constructor for classes without constructor so that the class can be instantiated. For more information, see Static Class and Static Categories.

By setting the constructor to a private constructor, you can prevent the class from being instantiated, as shown below:

C #

Copy code

Class nlog

{

// Private Constructionor:

Private nlog () {}

Public static double e = system.math.e; //2.71828 ...

}

For more information, see Private constructor.

The constructor of the structural type is similar to the constructor of the class, but Structs cannot include explicit default constructor because the compiler will automatically provide a constructor. This constructor initializes each field in the structure to the default value displayed in the default table. However, this default constructor is called only when the structure is instantiated with a NEW. For example, the following code uses the default constructor of INT32, so you can confirm that the integer has been initialized:

INT i = new int ();

Console.writeLine (i);

However, the following code causes the compiler error CS0165 because it does not use new, and attempts to use objects that have not been initialized:

INT I;

Console.writeLine (i);

Structs-based objects can be used after initialization or assignment, as shown below:

INT a = 44; // Initialize the value type ...

INT B;

B = 33; // or assocign it before using it.

Console.writeline ("{0}, {1}", A, b);

Therefore, the default constructor of value type call is not required.

Class and Structs can define constructor with parameters. The constructor with parameters must be called via a new statement or base statement. Class and Structs can also define multiple constructor and both do not need to define default constructor. E.g:

C #

Copy code

Public Class Employee

{

Public int sales;

Public Employee (int Annualsalary)

{

SALARY = Annualsalary;

}

Public Employee (int WeeklySalary, int Numberofweeks) {

SALARY = WeeklySalary * Numberofweeks;

}

}

This class can be created using any of the following statements:

C #

Copy code

EMPLOYEE E1 = New Employee (30000);

EMPLOYEE E2 = New Employee (500, 52);

The constructor can use the base keyword to call the constructor of the base class. E.g:

C #

Copy code

Public Class Manager: Employee

{

Public Manager (int Annualsalary)

: Base (AnnualSalary)

{

// Add further instructions here.

}

}

In this example, the constructor of the base class is called before the constructive block is executed. Base keyword can be used with parameters, or without parameters. Any parameters of the constructor can be used as the parameters of the base, or as part of an expression. For more information, see Base.

In the derived class, if the base keyword is not used to explicitly call the base class constructor, the default constructor (if any) is invoked. This means that the following constructor declaration is the same in the effect:

C #

Copy code

Public Manager (int initialdata)

{

// Add further instructions here.

}

C #

Copy code

Public Manager (int initialdata): Base ()

{

// Add further instructions here.

}

If the base class does not provide the default constructor, the derived class must use the base to explicitly call the basis function.

The constructor can call another constructor in the same object using the THIS keyword. Like BASE, the THIS can be used as parameters can also be used without parameters, and any parameters in the constructor can be used as the parameters of this, or as part of the expression. For example, you can use this to rewrite the second constructor in the previous example:

C #

Copy code

Public Employee (int WeeklySalary, int Numberofweeks)

: this (WeeklySalary * Numberofweeks)

{

}

This constructor is called: This constructor is called to use the use of the THIS keyword:

C #

Copy code

Public Employee (int Annualsalary)

{

SALARY = Annualsalary;

}

The constructor can be marked as public, private, protected, internal, or protectedinternal. These access modifiers define the user of the user to construct this class. For more information, see Accessing the modifier.

Use the Static keyword to declare the constructor as a static constructor. The static constructor will be automatically called before accessing any static fields, which are usually used to initialize static classes. For more information, see Static Construction Functions.