通用中間語言/基本語法
外觀
< 通用中間語言
CIL 的語法類似於基於 C 的語言和傳統組合語言。
語句用於在 CIL 中執行操作。語句由一個指令及其後的任何所需引數組成。
ldc.i4 2 // give one argument (2)
ldc.i4.5 // argument is part of the instruction (5)
add // no arguments given
call void [mscorlib]System.Console::WriteLine(int32) // give a method signature as an argument
塊用於對元素進行分組並建立主體(例如方法主體)。它們透過放置一個起始花括號和一個結束花括號來建立,元素位於兩者之間。
.method public void MyMethod()
{ // Block used to form the method body
ldstr "Hello, World!"
call void [mscorlib]System.Console::WriteLine(int32)
{ // Blocks can be nested
ldstr "Goodbye, World!"
call void [mscorlib]System.Console::WriteLine(int32)
}
}
註釋用於在原始碼中建立內聯文件。編譯器會忽略註釋,它們不會影響生成的程式。CIL 有兩種型別的註釋
- 單行註釋
//用於建立這些註釋,之後的任何內容都將是註釋的一部分,直到行末。
// ldstr "ignored" this line is ignored by the compiler
ldstr "Not ignored" // this instruction is seen, but this message isn't
- 多行註釋
- 這些註釋可以跨越多行。這些註釋以
/*開頭,以*/結束;編譯器將忽略它們之間的任何文字。
/*
This is a multi-line comment
that's two lines long.
*/