首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> C# >> 關於C#

C# 基礎語法速查:基本型別、控制結構、前置處理器指令

     編輯:關於C#

基本型別

關鍵字.NET 型別大小/範圍備註
boolSystem.Booleantrue/false不可轉換為數值
byteSystem.Byte0~255無號
sbyteSystem.SByte-128~127有號
shortSystem.Int16-32,768~32,767
ushortSystem.UInt160~65,535
intSystem.Int32±21 億預設整數
uintSystem.UInt320~42 億
longSystem.Int64±9 兆
ulongSystem.UInt640~18 兆
floatSystem.Single7 位有效數字單精度
doubleSystem.Double15-16 位雙精度
decimalSystem.Decimal28-29 位高精度貨幣計算,128-bit
charSystem.CharUTF-16 字元
stringSystem.String不可變字串參考型別

字串轉數值


int i = int.Parse("1000");           // 失敗拋例外
bool ok = int.TryParse("1000", out int j); // 失敗回 false,不拋例外
double d = double.Parse("3.14");

控制結構

  • if/else、switch、while、do-while、for、foreach:語法同 C/C++/Java。
  • foreach 適用於所有實作 IEnumerable 的集合。

前置處理器指令

條件編譯


#define DEBUG
#if DEBUG
    Console.WriteLine("Debug 模式");
#else
    Console.WriteLine("Release 模式");
#endif
  • #define 必須置於檔案最頂端。
  • Visual Studio 專案屬性 → Build → Define DEBUG/TRACE 可全域控制。
  • 命令列:csc /define:DEBUG Program.cs

診斷指令


#if DEBUG && CLIENT
#error 客戶端程式碼不得開啟 DEBUG
#elif DEBUG
#warning 目前為偵錯模式
#endif
  • #warning:編譯警告,繼續建置。
  • #error:編譯錯誤,中止建置。

程式碼區域


#region 資料存取層
// 相關方法...
#endregion
  • 僅供 Visual Studio 編輯器摺疊/展開,不影響編譯。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved