一周學會C#(前言續)

來源:互聯網
上載者:User
一周學會C#(前言續)

C#才鳥(QQ:249178521)

4.標點符號

{ 和 } 組成語句塊

分號表示一個語句的結束

using System;

public sealed class Hiker

{

public static void Main()

{

int result;

result = 9 * 6;

int thirteen;

thirteen = 13;

Console.Write(result / thirteen);

Console.Write(result % thirteen);

}

}

一個C#的“類/結構/枚舉”的定義不需要一個終止的分號。

public sealed class Hiker

{

...

} // 沒有;是正確的

然而你可以使用一個終止的分號,但對程式沒有任何影響:

public sealed class Hiker

{

...

}; //有;是可以的但不推薦

在Java中,一個函數的定義中可以有一個結尾分號,但在C#中是不允許的。

public sealed class Hiker

{

public void Hitch() { ... }; //;是不正確的

} // 沒有;是正確的

5.聲明

聲明是在一個塊中引入變數

u 每個變數有一個標識符和一個類型

u 每個變數的類型不能被改變

using System;

public sealed class Hiker

{

public static void Main()

{

int result;

result = 9 * 6;

int thirteen;

thirteen = 13;

Console.Write(result / thirteen);

Console.Write(result % thirteen);

}

}

這樣聲明一個變數是非法的:這個變數可能不會被用到。例如:

if (...)

int x = 42; //編譯時間出錯

else

...

6.運算式

運算式是用來計算的!

w 每個運算式產生一個值

w 每個運算式必須只有單邊作用

w 每個變數只有被賦值後才能使用

using System;

public sealed class Hiker

{

public static void Main()

{

int result;

result = 9 * 6;

int thirteen;

thirteen = 13;

Console.Write(result / thirteen);

Console.Write(result % thirteen);

}

}

C#不允許任何一個運算式讀取變數的值,除非編譯器知道這個變數已經被初始化或已經被賦值。例如,下面的語句會導致編譯器錯誤:

int m;

if (...) {

m = 42;

}

Console.WriteLine(m);// 編譯器錯誤,因為m有可能不會被賦值

7.取值

類型 取值 解釋

bool true false 布爾型

float 3.14 實型

double 3.1415 雙精確度型

char 'X' 字元型

int 9 整型

string "Hello" 字串

object null 對象


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.