c#--the understanding of the constructor function

Source: Internet
Author: User

Plainly, the constructor is the data member that is used to initialize the class {because the C # language has a type-safe trait--cannot use a variable without initialization}

I think it's good to quote someone's summary here:

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 qualities:

(1). The constructor must have the same name as the class;

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

(3). There can be one or more constructors in a class, that is, constructors can be overloaded to provide different methods of initializing class objects;

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

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

(6). Static constructors, with static modifiers, used to initialize static variables, a class that only allows a static constructor to be loaded when the class is instantiated, when the modifier public, private, loses its function.

{

Static constructors do not have access modifiers or parameters.

class before the first instance is create D or any of the static members is referenced. "> The static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.

class before the first instance is creat Ed or any static members is referenced. "> The static constructor cannot be called directly. In a program, users cannot control when static constructors are executed.

  

loadlibrary method. " The static constructor is also useful when creating wrapper classes for unmanaged code, where the constructor can call the  loadlibrary  method.

      If the static constructor throws an exception, the runtime will not call the constructor again, and the type will remain uninitialized for the lifetime of the application domain in which the program is running}

(7) Public, protected, private modifier can be used;

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

Here code for example only lists the use of base and this, but also other people's examples, to use, I think it is very easy to understand

public class Constructorprogram
{
private string name;
private int age;

Public Constructorprogram (): This ("Bell")
{
Console.WriteLine ("No Info left.");
}
Public Constructorprogram (string name)
: This ("Simple Programmer", 20)
{
THIS.name = name;
Console.WriteLine ("Name=" + this.name);
}
Public Constructorprogram (string name, Int. age)
{
THIS.name = name;
This.age = age;
Console.WriteLine ("Name=" + this.name);
Console.WriteLine ("age=" + this.age);
}

In the above code, it can be seen that this red font here is used to call the Constructorprogram class's own constructor. Understand the meaning of this sentence, you should understand the results of the implementation.
public static void Main ()
{
Constructorprogram cp1= New Constructorprogram ("goal");
Constructorprogram cp2 = new Constructorprogram ();

}
}

Operation Result:

Name=simple Programmer

Age=20

Name=goal

Name=simple Programmer

Age=20

Name=bell

In the above code, it can be seen that this red font here is used to call the Constructorprogram class's own constructor. Understand the meaning of this sentence, you should understand the results of the implementation.

The following code follows:

public class ConstructorProgram1
{
private string name;
private int age;
Public ConstructorProgram1 ()
{
Console.WriteLine ("No Info left");
}
Public ConstructorProgram1 (string name)
{
THIS.name = name;
Console.WriteLine ("Name=" + this.name);
}
Public ConstructorProgram1 (string name, Int. age)
{
THIS.name = name;
This.age = age;
Console.WriteLine ("Name=" + this.name);
Console.WriteLine ("age=" + this.age);
}

}

public class Constructorprogram:constructorprogram1
{

Public Constructorprogram ()
{
Console.WriteLine ("Sdfsdf");
}
Public Constructorprogram (string name)
: Base ("Goalbell", 20)
{
Console.WriteLine ("Name=" + name);
}

public static void Main ()
{
Constructorprogram cp = new Constructorprogram ("Programmer");
}

}

The results of the operation are as follows:

Name=goalbell

Age=20

Name=programmer

As you can see from the above code, the base in the derived class calls the constructor of the parent class (the base class), but if it does not provide initialization (that is: base ("Goalbell", 20)
Do not) point to the constructor of the base class, it executes a constructor that has no arguments in the base class.

will get the following result:

Name=goalbell

Age=20

Name=programmer

No Info Left

This means that base is a reference to the parent class, and this is a reference to the class itself.

Look at the following code, (constructor relationships in base classes and derived classes)

Using System;
Namespace Zjw.csharp
{
public class ConstructorProgram1
{
private string name;

Public ConstructorProgram1 ()
{
Console.WriteLine ("No Info left");
}
Public ConstructorProgram1 (string name)
{
THIS.name = name;
Console.WriteLine ("Name=" + this.name);
}

}
public class Constructorprogram:constructorprogram1
{
private string name;
private int age;

Public Constructorprogram (): This ("Bell")
{
Console.WriteLine ("No Info left.");
}
Public Constructorprogram (string name)
: This ("Simple Programmer", 20)
{
THIS.name = name;
Console.WriteLine ("Name=" + this.name);
}
Public Constructorprogram (string name, Int. age)
{
THIS.name = name;
This.age = age;
Console.WriteLine ("Name=" + this.name);
Console.WriteLine ("age=" + this.age);
}


public static void Main ()
{
Constructorprogram cp1= New Constructorprogram ("goal");
Constructorprogram cp2 = new Constructorprogram ();

Console.ReadLine ();

}
}


}

Execution results are

No Info Left

Name=simple Programmer

Age=20

Name=goal

No Info Left

Name=simple Programmer

Age=20

Name=bell

It is possible to conclude that a derived class invokes the constructor of the parent class before invoking the constructor of the inheriting class, and if the constructor of the parent class is not indicated in the inheriting class, the constructor that does not have a parameter in the parent class is called by default, and then the constructor of the inheriting class is called.

turn from; https://www.cnblogs.com/siyecao/archive/2012/05/17/2506375.html

c#--the understanding of the constructor function

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.