1.Instance Constructors and Classes (Reference Types)

Constructors methods :

  1.allow an instance of a type to be initialized to a good state.

  2.are always called .ctor (for constructor) in a method definition metadata table.

  3.When creating an instance of a reference type, memory is allocated for the instance’s data fields, the object’s overhead fields (type object pointer and sync block index) are initialized, and then the type’s instance constructor is called to set the initial state of the object.

  3.When constructing a reference type object, the memory allocated for the object is always zeroed out before the type’s instance constructor is called. Any fields that the constructor doesn’t explicitly overwrite are guaranteed to have a value of 0 or null.

  4.instance constructors are never inherited. That is, a class has only the instance constructors that the class itself defines.

  Because instance constructors are never inherited, you cannot apply the following modifiers to an instance constructor: virtual, new, override, sealed, or abstract.

  5.If you define a class that does not explicitly define any constructors, the C# compiler defines a default (parameterless) constructor for you whose implementation simply calls the base class’s parameterless constructor.

  8.Methods(一)=8.Methods(一)

  If the class is abstract, the compiler-produced default constructor has protected accessibility;otherwise, the constructor is given public accessibility.

  If the base class doesn’t offer a parameterless constructor, the derived class must explicitly call a base class constructor or the compiler will issue an error.

  If the class is static (sealed and abstract), the compiler will not emit a default constructor at all into the class definition.

  6.define several instance constructors. Each constructor must have a different signature,and each can have different accessibility.

  7.For verifiable code, a class’s instance constructor must call its base class’s constructor before accessing any of the inherited fields of the base class.

  The C# compiler will generate a call to the default base class’s constructor automatically if the derived class’s constructor does not explicitly invoke one of the base class’s constructors.

  Ultimately, System.Object’s public,parameterless constructor gets called. This constructor does nothing—it simply returns. This is because System.Object defines no instance data fields, and therefore its constructor has nothing to do.

  8.In a few situations, an instance of a type can be created without an instance constructor being called.

    In particular, calling Object’s MemberwiseClone method allocates memory, initializes the object’s overhead fields, and then copies the source object’s bytes to the new object.

  Also, a constructor is usually not called when deserializing an object with the runtime serializer. The deserialization code allocates memory for the object without calling a constructor by using the System.Runtime.Serialization.FormatterServices type's GetUninitializedObject or GetSafeUninitializedObject methods.

  9.You should not call any virtual methods within a constructor that can affect the object being constructed.

  The reason is if the virtual method is overridden in the type being instantiated, the derived type’s implementation of the overridden method will execute,but all of the fields in the hierarchy have not been fully initialized.

  Calling a virtual method would therefore result in unpredictable behavior.

 

compiling:

  1.a simple syntax that allows the initialization of fields defined within a reference type when an instance of the type is constructed

  8.Methods(一)

  examine the Intermediate Language (IL) for SomeType’s constructor method (also called .ctor)

  8.Methods(一)

  SomeType’s constructor contains code to store a 5 into m_x and then calls the base class’s constructor.

  In other words, the C# compiler allows the convenient syntax that lets you initialize the instance fields inline and translates this to code in the constructor method to perform the initialization.

  2. you should be aware of code explosion.

  The compiler initializes any fields by using the convenient syntax before calling a base class’s constructor to maintain the impression that these fields always have a value as the source code appearance dictates. 

  8.Methods(一)

  When the compiler generates code for the three constructor methods.

    the beginning of each method includes the code to initialize m_x, m_s, and m_d.

  then, the compiler inserts a call to the base class’s constructor.

    then, the compiler appends to the method the code that appears in the constructor methods.

  For example, the code generated for the constructor that takes a String parameter includes the code to initialize m_x, m_s, and m_d, call the base class’s (Object’s) constructor, and then overwrite m_d with the value 10. Note that m_b is guaranteed to be initialized to 0 even though no code exists to explicitly initialize it.

  3.The potential problem occurs when a base class’s constructor invokes a virtual method that calls back into a method defined by the derived class.

  If this happens, the fields initialized by using the convenient syntax have been initialized before the virtual method is called.

  4.If you have several initialized instance fields and a lot of overloaded constructor methods, you should consider defining the fields without the initialization, creating a single constructor that performs the common initialization, and having each constructor explicitly call the common initialization constructor.

  This approach will reduce the size of the generated code.

  example:there are three constructors in the preceding class, the compiler generates the code to initialize m_x, m_s, and m_d three times—once per constructor.

   8.Methods(一)

 

2.Instance Constructors and Structures (Value Types)

Value type (struct) constructors:

  work quite differently from reference type (class) constructors.

  1.The common language runtime (CLR) always allows the creation of value type instances, and there is no way to prevent a value type from being instantiated.

  For this reason, value types don’t actually parameterless constructors for value types.

  2.guaranteed to be 0/null.

  a security breach is possible.

   

  you can completely ignore everything in this note.

  3.does.

constructor.

  field initializers in structs.

  4.fields.

  any field being read

  8.Methods(一)

  Field 'SomeValType.m_y' must be fully assigned before control leaves the constructor.

  To fix the problem, assign a value (usually 0) to y in the constructor. or do like this below

  8.Methods(一)

  fields.

  In a reference type’s constructor, this is considered read-only, so you cannot assign to it at all.

 

compliling:

  1.To construct a Rectangle, the new operator must be used, and a constructor must be specified.

  8.Methods(一)

this case, the default constructor automatically generated by the C# compiler is called.

to 0/null.

  A value type’s instance constructor is executed only when explicitly called.

  2.will execute is if you write code to explicitly call one of them.

  8.Methods(一)

  Point’s constructor, the m_x and m_y fields in both Point fields would be 0.

  the C# compiler doesn’t automatically emit this code.

  code to call a value type’s constructor.

  3.confusion a developer might have about when that constructor gets called.

  be defined, the compiler can never generate code to call it automatically.

constructor, a value type’s fields are always initialized to 0/null.

  8.Methods(一)   8.Methods(一)

  CS0568: Structs cannot contain explicit parameterless constructors.

 

3.Type Constructors

 type constructors:

  class constructors, or type initializers

  1.(although C# doesn’t allow this), reference types, and value types.

  2.of a type. By default, types don’t have a type constructor defined within them.

  If a type has a type constructor, it can have no more than one.

  In addition, type constructors never have parameters

  8.Methods(一)

  except that you must mark them as static.

  3.C# makes them private for you automatically.

CS0515: 'SomeValType.SomeValType()': access modifiers are not allowed on static

constructors.

calling them; the CLR is always capable of calling a type constructor.

  4.never actually do this

static type constructor

  8.Methods(一)

  5.The calling of a type constructor is a tricky thing.

the JIT compiler checks if the type’s type constructor has already been executed for this AppDomain.

native code that the JIT compiler is emitting.

the JIT compiler does not emit the call because it knows that the type is already initialized.

  6.only once per AppDomain.

block.

  method.

constructor has already executed and will ensure that the constructor is not called again.  

  7.objects required by the type.

  8.code that reference each other

  references ClassB, and ClassB has a type constructor containing code that references ClassA

  the CLR still guarantees that each type constructor’s code executes only once;

type constructor.

  You should certainly try to avoid writing code that sets up this scenario.

that requires type constructors to be called in a specific order.

  9.be unusable.

to be thrown.

  10.to initialize those fields.

  11.fields, it does allow you to use it for static fields.

  8.Methods(一)

SomeType type above from a class to a struct, the code will compile and work as expected

 

 compiling:

  1.the compiler automatically generates a type constructor

  8.Methods(一)    ->       8.Methods(一)    ->     8.Methods(一)

  method definition metadata table.

  2.Type constructors shouldn’t call a base type’s type constructor. Such a call isn’t necessary

none of a type’s static fields are shared or inherited from its base type.

  and all of its base type’s type constructors to be called.

  implemented by the types must also have their type constructors called.

  type.

  method for all base types.

  the CLR won’t call it again.

  3.the explicit code contained in your type constructor method.

 

  8.Methods(一)

  initializes s_x to 5 and then initializes s_x to 10.

  4.when a type is unloaded?

   yes,with the System.AppDomain type’s DomainUnload event.

to the type, which will automatically get called when the type is unloaded?

  No.

  .

the CLR doesn’t support static Finalize methods.All is not lost.

 

 

相关文章: