|
一周學(xué)會(huì)C#(前言)
C#才鳥(QQ:249178521)
大家好!C#作為微軟在21世紀(jì)推出的新語言,它有著其他語言無法比擬的優(yōu)勢(shì)。但如何在短時(shí)間內(nèi)迅速掌握它,卻是一個(gè)比較難的問題。但如果你看完這個(gè)教程后,你一定會(huì)理解并掌握C#。
這個(gè)教程共分六個(gè)部分,今天先介紹C#中比較基本的概念。
1.總體框架
Hiker.cs 類名不一定等于文件名
using System; //每一個(gè)程序必須在開頭使用這一語句
public sealed class HitchHiker
{
public static void Main()//程序從Main開始執(zhí)行
{
int result;
result = 9 * 6;
int thirteen;
thirteen = 13;
Console.Write(result / thirteen); //輸出函數(shù)
Console.Write(result % thirteen);
}
}
//上面各語句的具體用法以后會(huì)介紹
/* 這個(gè)程序用來
* 演示C#的總體框架
*/
注意:上面的程序中,符號(hào)//表示注釋,在//后面的同一行上的內(nèi)容是注釋;
/*和*/ 這間的內(nèi)容都是注釋
你可以在windows的命令行提示符下鍵入:csc Hiker.cs
進(jìn)行編譯產(chǎn)生可執(zhí)行文件Hiker.exe
然后在windows的命令行提示符下鍵入:Hiker,你就可以看到在屏幕上顯視42
(注:你必須裝有.net framework)
single-line comment program
execution
starts
at Main 和Java不一樣,C#源文件名不一定要和C#源文件中包含的類名相同。
C#對(duì)大小寫敏感,所以Main的首字母為大寫的M(這一點(diǎn)大家要注意,尤其是熟悉C語言的朋友)。
你可以定義一個(gè)返回值為int的Main函數(shù),當(dāng)返回值為0時(shí)表示成功:
public static int Main() { ... return 0; }
你也可以定義Main函數(shù)的返回值為void:
public static void Main() { ... }
你還可以定義Main函數(shù)接收一個(gè)string數(shù)組:
public static void Main(string[] args)
{
foreach (string args in args) {
System.Console.WriteLine(arg);
}
}
程序中的Main函數(shù)必須為static。
2.標(biāo)識(shí)符
標(biāo)識(shí)符起名的規(guī)則:
ü 局部變量、局部常量、非公有實(shí)例域、函數(shù)參數(shù)使用camelCase規(guī)則;其他類型的標(biāo)識(shí)符使用PascalCase規(guī)則。
privateStyle camelCase規(guī)則(第一個(gè)單詞的首字母小寫,其余單詞的首字母大寫)
PublicStyle PascalCase規(guī)則(所有單詞的首字母大寫)
ü 盡量不要使用縮寫。
Message,而不要使用msg。
ü 不要使用匈牙利命名法。
public sealed class GrammarHelper
{ ...
public QualifiedSymbol Optional(AnySymbol symbol)
{ ... }
private AnyMultiplicity optional =
new OptionalMultiplicity();
}
3.關(guān)鍵字
C#中76個(gè)關(guān)鍵字:
abstract as base bool break
byte case catch char checked
class const continue decimal default
delegate do double else enum
event explicit extern false finally
fixed float for foreach goto
if implicit in int interface
internal is lock long namespace
new null object operator out
override params private protected public
readonly ref return sbyte sealed
short sizeof stackalloc static string
struct switch this throw true
try typeof uint ulong unchecked
unsafe ushort using virtual void
while
5個(gè)在某些情況下是關(guān)鍵字:
get set value add remove
C#中有76個(gè)在任何情況下都有固定意思的關(guān)鍵字。另外還有5個(gè)在特定情況下才有固定意思的標(biāo)識(shí)符。例如,value能用來作為變量名,但有一種情況例外,那就是它用作屬性/索引器的set語句的時(shí)候是一關(guān)鍵字。
但你可以在關(guān)鍵字前加@來使它可以用作變量名:
int @int = 42;
不過在一般情況下不要使用這種變量名。
你也可以使用@來產(chǎn)生跨越幾行的字符串,這對(duì)于產(chǎn)生正則表達(dá)式非常有用。例如:
string pattern = @"
( # start the group
abra(cad)? # match abra and optional cad
)+"; # one or more occurrences
如果你要在字符串中包含雙引號(hào),那你可以這樣:
string quote = @"""quote""&q
|
溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!