Comparison between instance constructor and Type constructor

Source: Internet
Author: User
(Technical level: 200)

The instance constructor is the instance constructor, And the Type constructor is the static constructor.

(1) In the instance constructor, We can initialize the instance fields and static fields of the class. Public Class Test
{
Private Int M;
Private Static Int N;
Public Test ()
{
M=1;//Equivalent to this. M = 1;
N=2;//The access between instance fields and static fields is no different.
}
}

(2) In the Type constructor, we can only initialize static fields for the purpose.Public Class Test
{
Private Static Int N = 1 ;
Static Test ()
{
N=2;
//Is the final value of N 1 or 2? The sequence is first initialized to 1, and then assigned to 2 by the Type constructor.
}
}

(3) The conditions triggered by the instance constructor and the Type constructor are different.
The instance constructor is called when an instance of a class is constructed. The Type constructor is called when the class is accessed for the first time.
So it can be said that if an instance constructor of a class is called, its Type constructor will certainly be called (if any), and vice versa. Public Class Test
{
Public Static Int M;
Public Test ()
{
Console. writeline ("Instance Constructor");
}
Static Test ()
{
Console. writeline ("Type constructor");
}
}
Public Class App
{
Static Void Main ()
{
Test. m = 1 ;
// At this time, the test class is accessed for the first time, so the Type constructor is called and the output result is: "Type constructor"
Test test = New Test ();
// At this time, the test class is not accessed for the first time, but an instance of the class is created, so the output result is: "instance constructor"
// If you remove the first line of code above, you should know the output result, or practice it yourself. Well, it's nice to use "# develop!
}
}

Appendix: difficulty level definition (from msdn)
100: technical level of introductory and general materials. Assume that you have little or no idea about the topic concept, functions, features, and advantages.
200: technical level of intermediate level data. Assume that you have 100 levels of knowledge and have specific training and learning for this topic.
300: technical level of advanced materials. Assume that you have 200 levels of knowledge, a deep understanding of the characteristics of the real application environment, and a very skilled coding skills. Provides a detailed technical overview of a subset of product/technical features, including architecture, performance, porting, deployment, and development.
400: technical level of expert-level data. It is assumed that you have deep technical knowledge and experience, and have a detailed and comprehensive understanding of the subject. Provides interaction between experts and covers specific topics.
Hope everyone'sArticleIt also comes with difficulty levels for Reference

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.