[讀書筆記 ]Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 第十一章

  • 1420
  • 0

閱讀Stephens' C#教材第十一章筆記

Chapter 11 Using Variables and Performing Calculations.

 
本章介紹變數的觀念及C#使用的15種資料型態、溢出字元(escape sequences)以及各種運算子
 
變數就是一段保存某資料的記憶體,可提供給程式進行運算之用。
因為記憶體資源有限,為了善用資源,C#的變數使用前需要先確定其資料型態,依照不同資料型態配置不同記憶體資源。
 

Data Type

Meaning

Range

byte

Byte(8-bit記憶空間)

0 to 255

sbyte

Signed byte(8-bit記憶空間)

-128 to 127

short

Small signed integer(16-bit記憶空間)

-32,768 to 32,767

ushort

Unsigned short(16-bit記憶空間)

0 to 65,535

int

Integer(32-bit記憶空間)

-2,147,483,648 to 2,147,483,647

uint

Unsigned integer(32-bit記憶空間)

0 to 4,294,967,295

long

Long integer(64-bit記憶空間)

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

ulong

Unsigned long(64-bit記憶空間)

0 to 18,446,744,073,709,551,615

float

Floating point(32-bit記憶空間)

大約在 -3.4e38 to 3.4e38 之間

有效位數7位

double

Big floating point(64-bit記憶空間)

大約在 -1.8e308 to 1.8e308之間

有效位數15/16位

decimal

Higher precision and smaller range then floating-point types(128-bit記憶空間)

大約在 -7.9e28 to 7.9e28之間

有效位數28/29位

char

Character(16-bit記憶空間)

0 to 65,535

A single Unicode character.

string

Text(視字串長度不定)

1 to 231 character

bool

Boolean

Can be Ture or Flase

object

An objent

Can point to almost anything.

 

Seuence

Meaning

\a

Bell(發出一聲)

\b

Backspace(倒退一格)

\f

Formfeed(跳至下一頁起點)

\n

Newline(插入新行)

\r

Carriage return(跳至同一行起點)

\t

Horizontal tab(水平方向的Tab鍵效果)

\v

Vertical tab(垂直方向的Tab鍵效果)

\’

Single quotation mark(顯示單引號)

\”

Double quotation mark(顯示雙引號)

\\

Backslash(顯示反斜線)

\?

Question mark(顯示問號)

\ooo

ASCII character in octal(八進位數字的ASCII編碼符號)

\xhh

ASCII character in hexadecimal(16進位數字的ASCII編碼符號)

\xhhhh

Unicode character in hexadecimal(16進位數字的Unicode編碼符號)

 
C# Operators運算子書上介紹有Arithmetic Operators(算術運算子),Logical Operators(邏輯運算子),String Operators(字串運算子), Comparison Operators(複合運算子), Assignment Operators(指定運算子), Bitwise Operators(位元運算子),詳細資料可查看網路資料介紹
 
Precedence介紹各運算子混合使用時彼此有優先順序,例如先乘除後加減的法則,可參考微軟網頁介紹 http://msdn.microsoft.com/en-us/library/6a71f45d.aspx
 
介紹const關鍵字引出常數的功能與使用
 
CelsiusToFahrenheit程式示範將透過變數操作將攝氏溫度轉華氏溫度
 
        // Convert from Celsius to Fahrenheit.
        private void cToFButton_Click(object sender, EventArgs e)
        {
            float celcius = float.Parse(celsiusTextBox.Text);
            float fahrenheit = celcius * 9 / 5 + 32;
            fahrenheitTextBox.Text = fahrenheit.ToString();
        }

        // Convert from Fahrenheit to Celsius.
        private void fToCButton_Click(object sender, EventArgs e)
        {
            float fahrenheit = float.Parse(fahrenheitTextBox.Text);
            float celcius = (fahrenheit - 32) * 5 / 9;
            celsiusTextBox.Text = celcius.ToString();
        }

 

CurrencyConvertert程式示範各國幣值的換算

 
DistanceConvertert程式示範各種距離單位的換算
 
TRY IT中示範如何排出一個下圖的選單,可以依據數量變化計算含稅後的價格