C # Constructors

Source: Internet
Author: User

One, C # constructors? Construct,function

A constructor is a special member function that is used primarily to allocate storage space for an object and initialize a data member.

Constructors have some special properties:

(1) The name of the constructor must be the same as the class;

(2) The constructor has no return type, it can take parameters, or it can take no parameters;

(3) When declaring a class object, the system automatically calls the constructor, and the constructor cannot be explicitly called;

(4) Constructors can be overloaded to provide different methods of initializing class objects;

(5) If a constructor is not defined at the time of declaration, the default constructor is automatically generated and the function body of the constructor is empty. If a constructor is defined, no default constructor is added.

(6) Static constructor, with static decoration, used to initialize static variables, a class only allows a constructor, when the class is instantiated, when the modifier public, private loses effect.

(7) You can use public, protected, private modifiers.

(8) Referencing the parent class constructs (): the base () method, which refers to its own overloaded construction using (): this (int para).

(7) You can use public, protected, and private modifiers.

(9) Constructors are not inherited: Execution order: If this constructor is available, execution starts from the top-level parent class. Note If the constructor has parameters, it is necessary to use base to pass arguments to the parent class.

What is the structure hierarchy relationship or execution order of C # constructors? Layer,transfer,execute

Constructs an object starting from a base class.


public class MyBaseClass
{
Public MyBaseClass ()
{
Console.WriteLine ("In MyBaseClass ()");
}
public MyBaseClass (int i)
{
Console.WriteLine ("In MyBaseClass (int i)");
}
}
public class Myderivedclass:mybaseclass
{
Public Myderivedclass ()
{
Console.WriteLine ("In Myderivedclass ()");
}
Public Myderivedclass (int i)
{
Console.WriteLine ("In Myderivedclass (int i)");
}
Public Myderivedclass (int i, int j)
//{
Console.WriteLine ("In Myderivedclass (int i,int j)");
//}
Public Myderivedclass (int i, int j)
: Base (i)
{
Console.WriteLine ("In Myderivedclass (int i,int j): Base (i)");
}
}
Class Program
{
static void Main (string[] args)
{
Event1
Myderivedclass myObj1 = new Myderivedclass ();
Console.WriteLine ();
Event2
Myderivedclass myObj2 = new Myderivedclass (4);
Console.WriteLine ();
Event3
Myderivedclass myObj3 = new Myderivedclass (4,8);
Console.WriteLine ();
Console.readkey ();
}
}

Program output:

In MyBaseClass ()

In Myderivedclass ()

In MyBaseClass ()

In Myderivedclass (int i)

In MyBaseClass (int i)

In Myderivedclass (int i,int j): Base (i)

Obviously, the program will not call the constructor without parameters by default only when the constructor of the parent class is displayed with base.

======= Additional ============

< static constructors >

A static constructor is a method member that implements initialization on a class. It is typically used to initialize static data. A static constructor cannot have parameters, cannot have modifiers, and cannot be called, and the static constructor of a class is called automatically when the class is loaded. Such as:

Using System.Data;
Class Employee
{
private static DataSet DS;
Static Employee ()
{
ds = new DataSet (...);
}
...
}

Declares a class employee with a static constructor. Note A static constructor can initialize only static data members, not non-static data members. However, a non-static constructor can either assign a value to a static data member or initialize a non-static data member.

If the class contains only static members, you can create a private constructor: Private TestClass () {...}, but private means that the constructor cannot be accessed from outside the class. Therefore, it cannot be called, and no object can be instantiated by the class definition.

======= Additional 2======

< how constructors for base classes and derived classes are used >

When you create an object of a derived class, the system calls the constructor of the base class and the constructor of the derived class, which executes the constructor of the base class first, and then the constructor of the derived class. If the derived class has an object member, then the constructor of the base class is executed, the constructor of the member object class is executed, and the constructor of the derived class is executed last.

As for what constructor of the base class is executed, by default it is the parameterless constructor that executes the base class, and if you want to perform a parameter constructor for the base class, you must indicate it in the member initialization table of the derived class constructor. Such as:

Class A
{private int x;
Public A () {x = 0;}
Public A (int i) {x = i;}
};

Class B:a
{private int y;
Public B () {y = 0;}
Public B (int i) {y = i;}
Public B (int i, int j): A (i) {y = j;}
};

b B1 = new B (); Executes the constructor of the base class A (), and then executes the constructor of the derived class B ()
b b2 = new B (1); Executes constructor a () of base Class A, and then executes constructor B (int) of the derived class
b B3 = new B (0,1); Executes the constructor a (int) of the base class A, and then executes the derived class's

Constructor B (Int,int)

The order in which constructors are executed here must be analyzed clearly. In addition, if the non-parametric constructor public A () {x = 0} is not provided in base class A, then the parameter constructor a (i) of base class A must be indicated in all constructor member initialization tables of the derived class as follows:

Class A
{private int x;
Public A (int i) {x = i;}
};

Class B:a
{private int y;
Public B (): A (i) {y = 0;}
Public B (int i): A (i) {y = i;}
Public B (int i, int j): A (i) {y = j;}
};

C # Constructors

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.