C # learning notes -- constructor and destructor

Source: Internet
Author: User

I. Principle:
Constructor is generally used to initialize the same name as the class of the object;
The Destructor destroys the resources occupied by the object. "
When an object is created, the constructor is automatically executed. When an object dies, the Destructor is automatically executed.
A simple example:
Class CLS
{
Public Cls (){}
Public ~ CLS (){}

}

Ii. Naming rules
1. The name of the constructor is the same as that of the class.
2. No return value type (this is different from the function with return value type void)
 

Iii. Examples of several common Constructors
1. default constructor (provided by the system (CLR ).
Class testclass
{
Public testclass (): Base () {}// provided by CLR
}

2. instance Constructor (instance constructor is a member of the method that implements initialization of instances in the class ). For example:
Using system;
Class Point
{
Public Double X, Y;
Public point ()
{
This. x = 0;
This. Y = 0;
}
Public point (Double X, Double Y)
{
This. x = X;
This. Y = y;
}
...
}
Class Test
{
Static void main ()
{
Point A = new point ();
Point B = new point (3, 4); // use the constructor to initialize the object.
...
}
}

3. Static Constructor
A static constructor is a method member that initializes a class. It is generally used to initialize static data.
Static constructor cannot have parameters, modifiers, and call. When a class is loaded, the static constructor of the class is automatically called.

Iv. Use of destructor and garbage collector in C #
A destructor is a method member that destroys an instance of a class.
The Destructor cannot have parameters, any modifiers, and cannot be called.
Because the purpose of the Destructor is opposite to that of the constructor, the prefix '~ 'To show the difference.
Public class resourceholder
{
...
~ Resourceholder ()
{
// Here is the user who cleans up unmanaged ResourcesCodeSegment
}
}

For the call priority, the example is as follows:
Class Program
{
Static void main (string [] ARGs)
{
C = new C ();
GG. C ();
}
Public static class gg
{
Static Gg () // The static constructor does not have an access modifier. The default value is private constructor, which is also the usage of private constructor.
{
Console. writeline ("static class GG! ");
}
Public static void C ()
{
Console. writeline ("static method of static class GG ");
}
/*
~ GG () // static class cannot contain destructor
{
Console. writeline ("static class S destructor! ");
}


*/
}
Public Class
{
Static ()
{
Console. writeline ("static constructor of Class! ");
}
Public ()
{
Console. writeline ("instance constructor of Class! ");
}
~ A ()
{
Console. writeline ("destructor of Class! ");
}
}
Public Class B:
{
Static B ()
{
Console. writeline ("B: static constructor of! ");
}
Public B ()
{
Console. writeline ("B: A's instance constructor! ");
}
~ B ()
{
Console. writeline ("B: destructor of ");
}
}
Public Class C: B
{
Static C ()
{
Console. writeline ("C inherits the static constructor of B! ");
}
Public C ()
{
Console. writeline ("static constructor of static classes! ");
}
~ C ()
{
Console. writeline ("C: constructor of B! ");
}
}
}

The execution result is as follows:

C: static constructor of Class B!

B: static constructor of Class!

Static constructor of Class!

Class A instance constructor!

B: instance constructor of Class! S

C: Class B instance constructor!

Static class GG! //

Static Gg-like static method!

C: Class B destructor!

B: destructor of Class!

Class A destructor!

 

Summary:ProgramFor a static constructor during execution: first execute your own static constructor and then execute the constructor of the parent class,

When executing an instance constructor, first execute the instance constructor of the parent class, and then execute your own instance 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.