C # Builder Summary

Source: Internet
Author: User

In C #, constructors are divided into instance constructors and type constructors.

first, the instance constructor

When constructing an object of a reference type, the memory allocated for the object is always 0 before the instance constructor is called, and the overridden field is guaranteed to have a value of 0 or null without being displayed by the constructor

In a class, if we do not have a definition of any constructors displayed, then the C # compiler will define a default parameterless constructor, which simply calls the parameterless constructor of a base class, such as the following SomeType class

 Public class SomeType    {        publicbase() {}    }

But when we do not provide a parameterless constructor in our base class, then our derived class must display a constructor that calls a base class, or the compiler will give an error. If the class's modifier is static, the compiler does not generate a default constructor in the class's definition at all.

In one type we can define multiple instance constructors, but each constructor must have a different signature.

Now let's talk about the execution of the instance constructor, see the following code:

Internal Sealed class SomeType    {        privateint5;    }

When we construct the SomeType class, its m_x field is initialized to 5 because the constructor of SomeType stores 5 in the field m_x, calls the constructor of the base class, and finally calls its own constructor. If you don't understand it, take a look at the following code:

 internal  sealed  class   SomeType { private  int  m_x = 5   private  double  m_d = 3.1415  ; 
Private byte M_b; public SomeType (string s) {m_d = 10 ; } }

When we instantiate a type and call the SomeType (string s) constructor, the compiler-generated code first initializes the values of m_x,M_b , and M_d, and then calls the constructor of the base class. The base class here is object, and finally the code, so the last three fields have a value of m_x = 5, M_d =10,m_b= 0;

Having said the instance constructor of the reference type, let's talk about the constructor of the value type, which the individual thinks is not so important for a value type constructor, just remember the time:

1. The CLR does allow you to define a constructor for a value type, and you can call the constructor to initialize a field of a value type, but you must show the call to execute.

2. The C # compiler does not allow value types to define parameterless constructors, but the CLR allows, that is, we can use a different language to define value types with no parameter constructors.

Second, type constructor

In addition to instance constructors, the CLR also supports type constructors, also called static constructors, class constructors, or type initializers.

1. The type constructor is not defined by default, and if you want to define it, you can define only one

2. Type constructors never have parameters

3, must be marked as static, which is also the difference between it and the instance Builder

4, C # automatically mark it as private, with the display tag, otherwise it will error, the reason is private, is to prevent developers to call it, because its call is the CLR responsible.

5. Code in a type constructor can only access static fields of a type, and its general purpose is to initialize those fields.

C # Builder Summary

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.