c#捷径教程笔记 第三章c#语法概述
c# 类型和变量
*值类型可以存活于栈或托管堆中。用struct可以定义用户自定义类型。
*值类型与引用类型
值类型:在c#中使用struct关键字定义。值类型的实例是唯一在栈中生存的实例。如果实例是引用
类型的成员或是已装箱,它们就生活于堆中,这些内容稍后讨论。当作为参数传入方法或被赋值给
其它变量时,默认情况下是以值复制方式进行的,就此而言他们很像c++中的结构。尽管c#的内建值
类型与java基本类型很相似,但却不是java的完全对应。
引用类型:在c#中使用class关键字定义。他们之所以被称为引用类型,是因为所使用的这些变量实
质上是引用到一个堆上的对象。事实上,在CLR中引用类型变量类似与引用到堆变量的值类型。在这
一点上,c3和java是一致的。c++程序员能够把引用类型视为不必解引用去访问对象指针。一些c++
程序员乐于把引用类型视作智能指针。
*隐式类型化局部变量
采用var生命隐式类型化局部变量
一个隐含类型的变量的生命必须包括初始化。而且只能出现在局部变量声明中,不能将类成员声明
为隐式类型化局部变量。