"Parsing constructors in C # classes"

Source: Internet
Author: User
Tags protected constructor


A. Overview of constructors in C # :

C # the middle class contains data members and function members. Function members provide some functionality for manipulating data in classes, including methods, properties, constructors, and finalizers, operators, and indexers.

1. definition of constructor design :

A constructor is a special method that allows an instance of a type to be initialized to a good state.

2. the function of the constructor design:

C # When you create an instance of a reference type in, you first allocate memory for the instance's data field, then initialize the object's additional fields (type Object pointers and synchronization block indexes), and finally call the type's instance constructor to set the initialization state of the object.

3. there is no common or protected constructor defined on the class, which prevents the class from using the New instantiated in external code, the effect of this approach:

(1). A class is used only as a container for some static members or properties, so it is never instantiated.

(2). you want the class to instantiate only by calling a static member function (object instance Chemical plant method)

4. use of the base class constructor:

before the compiler calls the constructor of the base class, any fields that use the simplified syntax are initialized to maintain the impression that the source code left "These fields always have a value", and if the constructor of the base class calls a virtual method, it callbacks a method defined in the derived class. There may be problems. In this case, the field initialized with the simplified syntax is initialized before the virtual method call.

two. Classification of constructors in C # classes:

1. Instance constructors:

(1). An overview of instance constructors:

Declares a method that contains a similar name, and the method has no return type. "If no constructors are provided, the compiler creates a default constructor in the background"

(2). The function of the instance constructor:

A. initializes all member fields to the standard default values.

B. when constructing an object of a reference type, the memory allocated for the object is always first zeroed before the instance constructor of the type is called, and the constructor does not show all fields that are overridden to guarantee that there is a 0 or NULL values. Instance constructors can never be inherited.

C. for the code to be "verifiable," the class's instance constructor must call the base class's constructor before accessing any fields that are integrated from the base class. If the constructor of a derived class does not appear to call a base class constructor,theC # compiler automatically generates a call to the default base class constructor.

2. Static Constructors:

(1). Static Constructors Overview:

Static constructors (type constructors, type initializers): Static constructors (type constructors) cannot contain parameters, no modifiers, only constructors with no arguments, which can only be executed once, and only one static constructor for a class, when the preceding constructor is an instance constructor, Whenever an object of a class is created, it is executed.

(2). How static constructors work:

. NET The runtime does not make sure when static constructors are executed, so you should not put code that requires execution at a particular time in a static constructor. It is also not possible to predict in what order the static constructors of the same class are executed.

(3). calls to static constructors:

the static constructor does not have an access modifier, and other C # code never calls it, but when the class loads, it is always called by the . NET Runtime. A static constructor can only access static members of a class and cannot access instance members of a class.

3. Constructor usage scope:

(1). An instance constructor with no parameters can be defined in the same class as a static constructor, and an instance constructor and a static constructor can also be defined in the same class. "Executes the static constructor when the class is loaded, and executes the instance constructor when the instance is created"

    (2). C # The compiler does not allow a value type (struct) to define a parameterless constructor, so the compiler never generates code that automatically calls it, no parameterless constructor, Fields of value types (structs) are always initialized to 0 or null c# clr Allow. c# Run a value type (struct) that defines a constructor with parameters "

three. How to override constructors in C # classes:

1. In rare cases, you can create an instance of a type without invoking the instance constructor. Using the memberwiseclone method of object, the function of this method is to allocate memory, initialize additional fields of the object (type object pointer and Synchronous block index), The byte data of the source object is then copied to the new object.

2. When deserializing an object with the run-time serializer, there is usually no need to call the constructor and deserialize the formatterservice type using the The Getuninitialzedobject or getsafeuninitializedobject method allocates memory for an object, and a constructor is not called during the period.

Four. Use cases for constructors in C # classes:

1. Use of static constructors:

class, you need to initialize these static fields and properties from an external source before you use the class for the first time.

2. Invocation of the constructor:

in the C # class, the constructor can call other constructors. the C # constructor initializer can contain a call to another constructor of the same class, or it can contain a call to the constructor of the direct base class. There cannot be more than one call in the initializer. "Using The this keyword to implement the initializer, the constructor initializer that invokes the parameter that best matches the constructor is performed before the function body of the constructor function."

Five. performance of type constructors in C # classes:

1. invocation of the type constructor:

JIT The compiler must decide whether to generate code to invoke it, and CLR you must ensure that the call to it is thread-safe.

2. JIT The compiler generates a call to the type constructor, where the call adds the situation:

(1). The JIT compiler can generate this call just before the first instance of the type is created, or just before a non-inheriting field or member of the class is accessed. is the exact semantics.

(2). The JIT compiler can make a call at any time before the first access to a static field or a static / instance method, or before invoking an instance constructor. That is, the pre-initialization semantics of the field.

3.. JIT -to-call location selection:

  clr is free to choose the time to invoke the type constructor, and clr

4.C # compiler If you see a class (beforefieldinit) containing a static field that is initialized inline, a class is generated in the type definition table to add a Beforefiledinit The record entry for the metadata token. the C # compiler does not add a Beforefiledinit metadata token if it sees a class that contains a type constructor that is displayed . "A static field can only be initialized before it is accessed, and it doesn't matter what time it is." An explicit type constructor may contain code that has side effects, so you need to run the exact time "



This article is from the "7693836" blog, please be sure to keep this source http://7703836.blog.51cto.com/7693836/1709190

"Parsing constructors in C # classes"

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.