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.
分享到:
相关推荐
Data Types and Sizes Constants Declarations Arithmetic Operators Relational and Logical Operators Type Conversions Increment and Decrement Operators Bitwise Operators Assignment Operators and ...
Data Types and Sizes Constants Declarations Arithmetic Operators Relational and Logical Operators Type Conversions Increment and Decrement Operators Bitwise Operators Assignment Operators and ...
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 ...
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 book explains the concept of pointers, including declaration, initialization, and dereferencing. It also covers advanced topics like pointer arithmetic and dynamic memory allocation using `malloc...
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 ...
Constructors and destructors are special types of member functions. Constructors are automatically called when an object is created, initializing its state. There are different types of constructors, ...
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 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 ...
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 ...
In this experiment, we will delve into the world of arrays and explore their definitions, types, and applications. 一维数组 A one-dimensional array is a collection of elements of the same data type...
- **Giving Values to Variables**: Methods for assigning values to variables, including initialization during declaration and using assignment statements. - **Expressions**: How expressions involving ...
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 ...
- **EJB Types**: There are three types of EJBs: session beans, entity beans, and message-driven beans. Session beans represent temporary business logic, entity beans represent persistent data, and ...
- **Declaration and Initialization**: How to declare and initialize different types of collections. - **Manipulating Collections**: Adding, removing, and iterating over elements in collections. - **...
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 ...
- **Copyable and Movable Types:** Design classes to be both copyable and movable when appropriate. - **Structs vs. Classes:** Use structs for POD (Plain Old Data) types and classes for more complex ...
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 ...
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 ...
- **Simple Types:** Explains primitive data types such as integers, floats, and characters. - **Initialization:** Describes different methods of initializing variables. - **The Traditional Conversions...