Analysis on the Application of C # Constructor

Source: Internet
Author: User

C # What is the use of constructor? C # What is the specific effect of using constructor? This article will introduce you to this aspect.

C # The Name Of The constructor must be recognized by the compiler before it can be automatically executed. Its naming method is simple and reasonable: Let the C # constructor and class have the same name. Except for the name, the C # constructor has no return value type, which is different from the void function. If it has a return value type, the compiler will be overwhelmed. Before you can access a class's methods, attributes, or anything else, the first statement to be executed contains the C # constructor of the corresponding class. Even if you do not write a C # constructor, a default C # constructor is provided to you.

  • ClassTestClass
  • {
  •  PublicTestClass ():Base() {}// Provided by CLR
     
  • }

The following lists several types of C # Constructor

1) default C # Constructor

  • ClassTestClass
  • {
  •  PublicTestClass ():Base(){}
  • }

As described above, it is provided by the system (CLR.

2) instance C # Constructor

The instance C # constructor is a method member that implements initialization of the class instance. For example:

  • UsingSystem;
  • ClassPoint
  • {
  •  Public DoubleX, y;
  •  PublicPoint ()
  • {
  • This. X = 0;
  • This. Y = 0;
  • }
  •  
     
  •  PublicPoint (DoubleX,DoubleY)
  • {
  • This. X = x;
  • This. Y = y;
  • }
  • ...
  • }
  •  
     
  • ClassTest
  • {
  •  Static VoidMain ()
  • {
  • Point a =NewPoint ();
  • Point B =NewPoint (3, 4); // use C # To construct a function to initialize an object
     
  • ...
  • }
  • }

Declares a class Point, which provides two C # constructor. They are overloaded. One is the PointC # constructor without parameters and the other is the PointC # constructor with two double parameters. If the C # constructor is not provided in the class, the CLR automatically provides a default C # constructor. However, once the class provides custom C # constructor, such as Point () and Point (double x, double y), the default C # constructor will not be provided, note This.

3) Static C # Constructor

Static C # constructor is a method member that implements class initialization. It is generally used to initialize static data. Static C # constructor cannot have parameters, modifiers, and call. When a class is loaded, the static C # constructor of the class is automatically called. For example:

  • UsingSystem. Data;
  • ClassEmployee
  • {
  •  Private StaticDataSet ds;
  •  StaticEmployee ()
  • {
  • Ds =NewDataSet (...);
  • }
  • ...
  • }

Declared a class of Employee with a static C # constructor. Note that the static C # constructor can only initialize static data members, rather than non-static data members. However, non-static C # constructor can assign values to static data members or initialize non-static data members.

If the class only contains static members, you can create a private C # constructor: private TestClass (){...}, However, private means that it is impossible to access the C # constructor from outside the class. Therefore, it cannot be called and no object can be instantiated by the class definition.

The above is a simple application of several types of C # constructor. The following section focuses on the class hierarchy (that is, the inheritance structure) how to Use the C # constructor of the base class and derived class. The initialization of a derived class object is completed by the base class and the derived class. The base class members are initialized by the C # constructor of the base class, and the members of the derived class are initialized by the C # constructor of the derived class.

When creating an object of A derived class, the system will call the C # constructor of the base class and the C # constructor of the derived class. The execution order of the constructed function is: first, execute the C # constructor of the base class, and then execute the C # constructor of the derived class. If the derived class has an object Member, first execute the C # constructor of the base class, then execute the C # constructor of the member object class, and finally execute the C # constructor of the derived class.

As for the C # constructor that executes the base class, the C # constructor without a parameter of the base class is executed by default. If you want to execute the C # constructor with a parameter of the base class, it must be specified in the member initialization table of the C # constructor of the derived class. For example:

  • ClassA
  • {Private IntX;
  •  PublicA () {x = 0 ;}
  •  PublicA (IntI) {x = I ;}
  • };
  •  
     
  • ClassB:
  • {Private IntY;
  •  PublicB () {y = 0 ;}
  •  PublicB (IntI) {y = I ;}
  •  PublicB (IntI,IntJ): A (I) {y = j ;}
  • };
  •  
     
  • B b1 =NewB ();
  • // Execute C # constructor A () of the base class A, and then execute C # constructor B () of the derived class ()
  • B b2 =NewB (1 );
  • // Execute C # constructor A () of the base class A, and then execute C # constructor B (int) of the derived class)
  • B b3 =NewB (0, 1 );
  • // Execute the C # constructor A (int) of the base class A, and then execute

C # constructor B (int, int)

Here, the execution sequence of the C # constructor must be clearly analyzed. In addition, if the base class A does not provide the C # constructor public A () {x = 0 ;}, in the initialization table of all C # constructor members in the derived class, you must specify the C # constructor A (I) as follows:

  • ClassA
  • {Private IntX;
  •  PublicA (IntI) {x = I ;}
  • };
  •  
     
  • ClassB:
  • {Private IntY;
  •  PublicB (): A (I) {y = 0 ;}
  •  PublicB (IntI): A (I) {y = I ;}
  •  PublicB (IntI,IntJ): A (I) {y = j ;}
  • };

The Application of C # constructor is introduced here. I hope it will help you learn how to use C # constructor.

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.