C# 学习笔记(CTS) - 02

来源:互联网 发布:网络黑侠 自己玩死 编辑:程序博客网 时间:2024/06/06 14:28

Primitive Types: are instrinsic to the language, are not compatible.

Classes: are created by users, members are either intrinsic types or other classes

Having a single base class from which all other classes are derived is referred to as having a single rooted hierarchy.

share minimium set of abilities

Four public methods that all inherit from System.Object

bool Equals: Compare two objects at run time to determine whether tery're same object; Class: Variables refer to same object; Value Types: are identical and have same value;

int GetHashCode: Retrieves the hash code sepcified for an object.

Type GetType: Used with Reflection method to retrieve the type information for a given object.

string ToString: Used by default to retrieve the name of object; can be overridden by derived classes

Protected Method:

void Finalize: Called by the runtime to allow prior to gabbage collection

object Memberwise Clone:

Biggest disadvantage of making everything object has always being poor performance

 

Value Types:

  • can't be null
  • enumeratiors, structures, primitives
  • When you declare a variable of one of these types, the compilor allocates the number of bytes associated with that type and you can work directly with that allocated memory.
  • When you pass a variable that's a vlue type, you are passing the variable's value and not a reference to it's to ist's underylying object

Reference Types:

  • a reference - when not null - is always guaranted to point to an object that's of the type specified and that was alreadybeen allocated on the heap
  • classes, arrays, interfaces, delegates

HEAP

myInt         [    42    ]

myString     [            ]  <------> ["Hello World!"]

Boxing: convert a value type to a reference type;

Unboxing: convert a reference type to a value type;

lds.     load a numerica constant value
.i4.s    refers to the fact that the value is being pushed onto the stack as a 32-bit(4byte) integer
lds.i4.s  Pushes specified 8-bit value as 32-bit.
stloc   Pop the value to variable
ldloc   Pushes Local variable at specified index
box    convert a value type to reference type

 

Boxing:

First--Memory is allocated on the heap, tha aomunt of memory allocated on the heap must be the size of value type plus the amount of memory needed to hold the object and it's internal structure;

Second--The value type's value is copied to the newly allocated heap memory.

Third--The addess of the newly allocated object is placed on the stack and now points to a reference type.

Unboxing:

Explicitly cast is necessary because so that the compiler can verify that cast is valid for specified type

ldind.i4.   Pushes(indirect) value of type int as int32

First--The runtime must dermine that the address on the stack points to a valid object and the object type can be converted to the value type specified in the MSIL unboxing instruction call.

Second--Once it has been determined the cast is valid, a pointer to the value within the object is returned

stsfld.   Stores a static field
ldsfld.   Pushes static field of an object
callvirt. Calls virtual method of obj.

The object-oriented concept of substitutability states that you should always be able to use a derived class in place of tis base class if the class hierarchy has been defined correctly.

*The third variable is set to the unboxing value

upcast:   Employee -> ContractEmployee
downcast:   ContractEmployee -> Employee

Explicitly downcasting:

The result is not a compiler error because the true nature of the upcasted object can't be determined until runtime.
The common language determine the object type at run time.

as     The advantage of using the keyword instead of a cast is that you don't have to worry about an exception being throw fi the cast is invalid. Instead, the resualt will be null.

 

CTS. benefits:

Language Interoperability

  • CTS define the set types that a .net compiler must support to interoperate with other language.
  • Common Language Specification(CLS) defines a single set of rules for every compiler, ensuring that each complier will output code that interacts with the common language runtime consisitantly.
  • One of the CLS requirements is that the compiler must support certain types defined in CTS.
原创粉丝点击