摘要
在編程的世界中,"Hello World"程序是最基本的示例,用于演示一個(gè)新的編程語言或者平臺。在本文中,我們將學(xué)習(xí)如何用C#編寫一個(gè)Hello World程序。
正文
C#創(chuàng)建程序順序
新建項(xiàng)目→編寫代碼→調(diào)試或運(yùn)行,系統(tǒng)會自動在你創(chuàng)建的項(xiàng)目上加一個(gè)解決方案層。

在.Net6時(shí),沒有Main入口方法了,Program自動增加了一行Console.WriteLine("Hello, World!");
Console這個(gè)類
Console.Write? 表示向控制臺直接寫入字符串,不進(jìn)行換行,可繼續(xù)接著前面的字符寫入。
Console.WriteLine? 表示向控制臺寫入字符串后換行。
Console.Read 表示從控制臺讀取字符串,不換行。
Console.ReadLine 表示從控制臺讀取字符串后進(jìn)行換行。
Console.ReadKey 獲取用戶按下的下一個(gè)字符或功能鍵,按下的鍵顯示在控制臺窗口中。
Console.Beep 通過控制臺揚(yáng)聲器播放提示音。
Console.Clear 清除控制臺緩沖區(qū)和相應(yīng)的控制臺窗口的顯示信息。
Console.BackgroundColor = ConsoleColor.Blue; //設(shè)置背景色
Console.ForegroundColor = ConsoleColor.White; //設(shè)置前景色,即字體顏色
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Hello, World!");
關(guān)鍵字
關(guān)鍵字是 C# 編譯器預(yù)定義的保留字。這些關(guān)鍵字不能用作標(biāo)識符,但是,如果您想使用這些關(guān)鍵字作為標(biāo)識符,可以在關(guān)鍵字前面加上 @ 字符作為前綴。
保留關(guān)鍵字
abstractasbaseboolbreakbytecase
catchcharcheckedclassconstcontinuedecimal
defaultdelegatedodoubleelseenumevent
explicitexternfalsefinallyfixedfloatfor
foreachgotoifimplicitinin (generic? modifier)int
interfaceinternalislocklongnamespacenew
nullobjectoperatoroutout? (generic? modifier)overrideparams
privateprotectedpublicreadonlyrefreturnsbyte
sealedshortsizeofstackallocstaticstringstruct
switchthisthrowtruetrytypeofuint
ulonguncheckedunsafeushortusingvirtualvoid
volatilewhile
上下文關(guān)鍵字
addaliasascendingdescendingdynamicfromget
globalgroupintojoinletorderbypartial? (type)
partial? (method)removeselectset
標(biāo)識符
標(biāo)識符是用于標(biāo)識類,變量,函數(shù)或任何其他用戶定義項(xiàng)的名稱。C#中命名類的基本規(guī)則如下:
名稱必須以字母開頭,后跟字母,數(shù)字(0-9)或下劃線。標(biāo)識符中的第一個(gè)字符不能是數(shù)字。
它不能包含任何嵌入式空格或符號,例如?-+!@#%^&*()[] {}。; :“'/和\。但是,可以使用下劃線(_)。
它不應(yīng)該是C#關(guān)鍵字。
Main方法的要求
Main方法必需定義為static
Main方法首字母必須大寫
返回值可以是void或int其它
命令行參數(shù)可選
namespace test1 //命名空間
{
? ? class A //類名
? ? {
? ? ? ? static int Main(string[] arg) //方法入口
? ? ? ? {
? ? ? ? ? ? Console.WriteLine("您好,C#");//語法
? ? ? ? ? ? return 0;
? ? ? ? }
? ? }
}
傳入一個(gè)參數(shù)

namespace test1 //命名空間
{
? ? class A //類名
? ? {
? ? ? ? static int Main(string[] arg) //方法入口
? ? ? ? {
? ? ? ? ? ? Console.WriteLine(arg[0]);//輸出參數(shù),這里是一個(gè)數(shù)組
? ? ? ? ? ? Console.WriteLine("您好,C#");//語法
? ? ? ? ? ? return 0;
? ? ? ? }
? ? }
}
注意:一個(gè)程序只能有一個(gè)Main入口方法。
注釋
// 單行注釋
/**/ 塊注釋
///說明注釋,注釋以后可以自動生成說明文檔檔
#region 折疊注釋,可以將代碼折疊? #endregion
只是#region 所在行后面的文字是注釋文字,而其它的#region和#endregion之內(nèi)的行代碼是有效的,僅僅起折疊作用
/*
* 創(chuàng)建者:張三
* 創(chuàng)建日期:2022-01-01
*/
namespace test1 //命名空間
{
? ? #region"類"
? ? class A //類名
? ? {
? ? ? ? /// <summary>
? ? ? ? /// 這個(gè)是方法入口
? ? ? ? /// </summary>
? ? ? ? /// <param name="arg">傳入?yún)?shù)</param>
? ? ? ? /// <returns></returns>
? ? ? ? static int Main(string[] arg) //方法入口
? ? ? ? {
? ? ? ? ? ? Console.WriteLine(arg[0]);//輸出參數(shù),這里是一個(gè)數(shù)組
? ? ? ? ? ? Console.WriteLine("您好,C#");//語法
? ? ? ? ? ? return 0;
? ? ? ? }
? ? }
? ? #endregion
}
快捷鍵:
注釋快捷鍵:Ctrl + K + C
取消注釋快捷鍵:Ctrl + K + U
一段完整的程序
/*
* 創(chuàng)建者:張三
* 創(chuàng)建日期:2022-01-01
*/
namespace test1 //命名空間
{
? ? #region"類"
? ? class A //類名
? ? {
? ? ? ? /// <summary>
? ? ? ? /// 程序入口
? ? ? ? /// </summary>
? ? ? ? /// <param name="arg">傳入?yún)?shù)</param>
? ? ? ? static void Main(string[] arg) //方法入口
? ? ? ? {
? ? ? ? ? ? Console.WriteLine("----------------------------");
? ? ? ? ? ? Console.WriteLine("|? ? ? ? ? ? PLC? ? ? ? ? |");
? ? ? ? ? ? Console.WriteLine(" -------------------------- ");
? ? ? ? ? ? Console.WriteLine("|? ? ? ? ? ? ? ? ? ? ? ? ? |");
? ? ? ? ? ? Console.WriteLine("|? ? ? ? ? ? ? ? ? ? ? ? ? |");
? ? ? ? ? ? Console.WriteLine("----------------------------");
? ? ? ? }
? ? }
? ? #endregion
}
命名規(guī)范
字母大小寫約定
Pascal風(fēng)格:將標(biāo)識符的首字母和后面連接的每個(gè)單詞的首字母都大寫。
如:Name,GetName
Camel風(fēng)格:標(biāo)識符的首字母小寫,而每個(gè)后面連接的單詞的首字母都大寫
userId,getName
項(xiàng)目名:公司名.產(chǎn)品名 Idiosoft.Mes
命名空間:公司名或產(chǎn)品名
接口:大寫"I"開頭,像IRun
類名:一定要休現(xiàn)功能與操作的意義,像用戶類,User,操作類,Operation
方法名:體現(xiàn)出這個(gè)方法的意思GetName,當(dāng)然現(xiàn)在有一種更簡單的直接寫Name這樣來做。
私有的成員變量:前綴寫成"_"
其它變量:小寫字母
ORM實(shí)體類:用小寫字母,這個(gè)在設(shè)計(jì)數(shù)據(jù)庫表時(shí)也可以用這個(gè)規(guī)則,如果是兩個(gè)單詞用"_"隔離,像name,created_dated