C# 学习笔记(CTS) - 02
2006-04-24 12:41
471 查看
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.
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.
相关文章推荐
- (原创)c#学习笔记02--编写c#程序01--开发环境
- (原创)c#学习笔记02--编写c#程序02--控制台应用程序
- (原创)c#学习笔记10--定义类成员03--接口的实现02--用非公共的可访问性添加属性存取器
- C#.NET验证码智能识别学习笔记---02-C#.Net中Using关键字的用法
- (原创)c#学习笔记10--定义类成员06--示例应用程序02--编写类库
- (原创)c#学习笔记04--流程控制02--goto语句
- (原创)c#学习笔记04--流程控制03--分支02--if语句
- (原创)c#学习笔记03--变量和表达式02--C#控制台应用程序的基本结构
- [C#学习笔记02]理解值和引用
- (原创)c#学习笔记08--面向对象编程简介02--OOP技术02--继承
- (原创)c#学习笔记08--面向对象编程简介02--OOP技术03--多态性
- (原创)c#学习笔记08--面向对象编程简介02--OOP技术04--对象之间的关系
- (原创)c#学习笔记09--定义类02--System.Object
- (原创)c#学习笔记06--函数02--变量的作用域01--其他结构中变量的作用域
- (原创)c#学习笔记06--函数02--变量的作用域02--参数和返回值与全局数据
- (原创)c#学习笔记10--定义类成员01--成员定义02--建立方法
- (原创)c#学习笔记08--面向对象编程简介02--OOP技术01--接口
- (原创)c#学习笔记05--变量的更多内容02--复杂的类型变量02--结构
- (原创)c#学习笔记10--定义类成员02--类成员的其他议题01--隐藏基类方法
- (原创)c#学习笔记10--定义类成员02--类成员的其他议题03--嵌套的类型定义