【重拾基础——读《Beginning C# 2012 programming》总结】1 - 变量与表达式

1. 简单类型的识记:

* Integer Type(整型类型):

sbyte/byte: 8位, -28~27/0~28-1(-128~127/0~255) System.SByte/System.Byte

short/ushort: 16位,  -216~215/0~216-1(-32768~32767/0~65535) System.Int16/System.UInt16

int/uint: 32位, -232~231/0~232-1(-2147483648~2147483647/0~4294967295) System.Int32/System.UInt32

long/ulong: 64位, -264~263/0~264-1(-9.2*1018~9.2*1018/0~1.84*1019) System.Int64/System.UInt64

注:UInt16,UInt32,UInt64都不是CLS的规范

* Floating-point Types(浮点类型):

float(System.Single):±3.4E+38,224

double(System.Double): ±1.7E+308,253(容量最大的一个类型,其次就是float类型)

decimal(System.Decimal): ±7.9E+28,296(decimal 类型是 128 位的数据类型,适合用于财务计算和货币计算。高精度需求)

* Text and Boolean Types(文本与布尔类型):

char(System.Char): single Unicode character, stored as a integer between 0 and 65535(char类型的变量可以强制转为整型值)

bool(System.Boolean): True or false (不能将两个bool类型的变量相加)

string(System.String): A sequence of characters with infinite length.(string类型的变量是没限制总长度的)

* Literal Values

float:3.45f    double:44.232d   uint/ulong: 2323u   long/ulong: 2342L   decimal:1.5M

(Literal Value的后缀是把数值作为变量使用的一个手段,数值没有类型,而变量有类型,Literal values使用数值也拥有类型)

* Escape Sequences(转义符):

比较特殊的转义符:  \f: ♀  \v: ♂

* implicit Conversion:

8/5 = 1, 8f/5f = 1.6

8/5.0 = 1.6

注:在没写明数值类型时,没有小数点数值的相除,将直接取结果的整数位,忽略小数。

* String可以使用@来避免使用转义符:

@"C:\Temp" VS "C:\\Temp"

* String是引用类型,而非值类型,所以String变量可以赋null

2.表达式:

Unary(一元运算符): +,-,++,--

Binary(二元运算符): +,-,%,*,/,=,+=,-=,*=,/=,%=

Ternay(三元运算符): :?

 

总结:带宽常用单位(M)b是8位字节byte/sbyte, 半整短16位是short/ushort,常见32位处理器常用Int/uInt, 牛叉64位处理器使用Long/uLong

        容量最大E308是double(15位精度), 常用float作小数E38(7位精度),精确计算用decimal(28位精度)

        容量最大数double,精度最大28就decimal,可怜float处中间

        char可作数字加减乘除,bool不可,string没限长。

        Literal数值,使用L,U,F,M,d来标识类型。

    乘除法要小心,标识类型很重要,否精度就丢失。

        @让你轻松写路径,不担心转义符。

        

posted @ 2014-01-23 01:00  逝涛  阅读(111)  评论(0)    收藏  举报