`
xiechangming
  • 浏览: 25948 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Types and Initialization

阅读更多

Types are allowed to provided a distinguished method that is called when the type is first initialized. This type initializer is simply a static method with a well-known name(.cctor). A type can have at most one type initializer, and it must take no parameters and return no value. It is called automatically by CLR. The following shows a type initializer in C#:

namespace EssentialNet

{

    public class Customer

    {

        public static string name;

 

        static Customer()

        {

            name = "xiech2";

        }

    }

}

If a single C# type have both an explicit type initializer method and static field declarations with initializer expressions, .cctor method will begin with field initializers(in order of declaration), followed by the body of the explicit typer initializer method. Consider the following C# type definiation:

namespace EssentialNet

{

    public class Customer

    {

        public static string name;

 

        public static string id = "C1";

 

        public static string code = "XIECH2";

 

        static Customer()

        {

            name = "xiech2";

        }

    }

}

The fields will be initialized in the following order:id,code,name.

 

There is another distinguished method that CLR will call automatically each time an instance of the type is allocated. It is called a constructor and must have the distinguished name .ctor. The C# compiler will inject any non-static field initialization expressions in to the generated .ctor before the explicit method body. Consider the following C# type definition:

namespace EssentialNet

{

    public class Customer

    {

        public static string name;

 

        public static string id = "C1";

 

        public static string code = "XIECH2";

 

        public long t1 = DateTime.Now.Ticks;

 

        public long t2 = DateTime.Now.Ticks;

 

        public long t3;

 

         static Customer()

        {

            name = "xiech2";

        }

 

        public Customer()

        {

            t3 = DateTime.Now.Ticks;

        }

 

       

    }

}

The fields will be initialized in the following order:t1,t2,t3.

 

0
1
分享到:
评论

相关推荐

    The C programming Language(chm格式完整版)

    Data Types and Sizes Constants Declarations Arithmetic Operators Relational and Logical Operators Type Conversions Increment and Decrement Operators Bitwise Operators Assignment Operators and ...

    C程序设计语言第2版

    Data Types and Sizes Constants Declarations Arithmetic Operators Relational and Logical Operators Type Conversions Increment and Decrement Operators Bitwise Operators Assignment Operators and ...

    GObject Reference Manual

    Parameters and Values - Standard Parameter and Value Types Varargs Value Collection - Converting varargs to generic values GParamSpec - Metadata for parameter specifications Signals - A means for ...

    PCI Express Technology 3.0

    Packet Types and Fields Transaction Ordering Traffic Classes, Virtual Channels and Arbitration (QoS) Flow Control ACK/NAK Protocol Logical PHY (8b/10b, 128b/130b, Scrambling) Electrical PHY Link ...

    The_C_Programming_Language-英文版

    2.2 Data Types and Sizes 35 2.3 Constants 36 2.4 Declarations 38 2.5 Arithmetic Operators 39 2.6 Relational and Logical Operators 39 2.7 Type Conversions 40 2.8 Increment and Decrement Operators 43 ...

    Exploring C++ 11

    Part 2: Custom Types – Assignment and Initialization Part 2: Custom Types – Writing Classes Part 2: Custom Types – More About Member Functions Part 2: Custom Types – Access Levels Part 2: Custom ...

    Lesson-1-master.zip

    3. Define and implement types of debugging allowed with EDK II 4. Identify the responsibilities of the Security phase 5. Define PEI and its purpose, functions, attributes, and components 6. Describe ...

    Metal.编程向导英文版.and.Reference.via.Swift.pdf.zip

    Metal Primitive Types 113 Responding to MTKViewDelegate Methods 115 Retrieving a Drawable 115 Creating a Command Buffer 116 Creating a Command Encoder 117 Fixed-Function State on the Command ...

    HPL: Vol. IV: Functional and Logic Programming Languages

    2.23. Your .emacs Initialization File Part II—Scheme Chapter 3—Scheme 3.1. Who Uses Scheme? 3.2. Scheme as a Dialect of Lisp 3.2.1. Expressions 3.2.2. Uniform Syntax 3.2.3. Automatic ...

    Effective C# (Covers C# 4.0) Mar 2010

    Spot the advantages of the dynamic and Expression types over reflection (see Items 42 and 43) Assess why query expressions are better than loops (see Item 8) Understand how generic covariance and ...

    understanding linux network internals

    Topics include: Key problems with networking Network interface card (NIe device drivers System initialization Layer 2 (link-layer) tasks and implementation Layer 3 (IPv4) tasks and implementation ...

    CSharp 3.0 With the .NET Framework 3.5 Unleashed(english)

    A Quick Introduction to Reference Types and Value Types 108 The Unified Type System 109 Reference Type and Value Type Memory Allocation 114 Reference Type and Value Type Assignment 117 More ...

    Microsoft Library MSDN4DOS.zip

    Chapter 5 Defining and Using Complex Data Types Chapter 6 Using Floating-Point and Binary Coded Decimal Numbers Chapter 7 Controlling Program Flow Chapter 8 Sharing Data and Procedures among Modules ...

    Professional C# 3rd Edition

    Value Types and Reference Types 39 CTS Types 40 Predefined Value Types 41 Predefined Reference Types 44 Flow Control 47 Conditional Statements 47 Loops 51 Jump Statements 54 Enumerations 55 Arrays 57 ...

    Beyond.BIOS.Developing.with.the.UEFI.3rd.epub

    The reader will find: * An overview of Uefi and underlying Platform Initialization (Pi) specifications * How to create Uefi applications and drivers * Workflow to design the firmware solution for a ...

    Oracle Security

    Setting Up Initialization Parameters for Security Chapter 9 Developing a Simple Security Application The Application Overview Preparing the Role-Object Matrix Views Roles Grants Application ...

    The Indispensable PC Hardware Book - rar - part1. (1/7)

    Addressing types and instruction coding. Programming at processor level: mnemonics and the assembler. Addressing types. Instruction coding. Loading instructions and prefetching. Real mode, high ...

    depends22_x64

    invalid modules, import/export mismatches, circular dependency errors, mismatched machine types of modules, and module initialization failures. Dependency Walker runs on Windows 95, 98, Me, NT, 2000...

    Wideband TDD - WCDMA for unpaired spectrum - Chitrapu P. (2004)

    9.1.2 Comparison of TDD and WLAN System and Service Attributes 231 9.1.3 Performance of 802.11b WLAN Systems 233 9.1.4 Comparison of UMTS TDD and 802.11b WLAN System Performance 235 9.1.5 Deployment ...

Global site tag (gtag.js) - Google Analytics