Jump to content

Common Intermediate Language/Basic syntax

From Wikibooks, open books for an open world

The syntax of CIL is similar to C-based languages and traditional assembly languages.

Statements

[edit | edit source]

Statements are used to perform operations in CIL. Statements consist of an instruction with and any needed arguments after.

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

Blocks

[edit | edit source]

Blocks are used to group elements and create bodies (e.g. method body). They are created by placing both an opening and closing curly-brace, with elements in between.

.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)
    }
}

Comments

[edit | edit source]

Comments are used to create inline documentation within the source code. Comments are ignored by the compiler and do not affect the resulting program. CIL has two types of comments:

Single-line comments
// is used to create these comments, and anything after will be part of the comment until the end of the line.
// ldstr "ignored" this line is ignored by the compiler
ldstr "Not ignored" // this instruction is seen, but this message isn't
Multi-line comments
These comments can be multiple lines long. These comments are started with /* and ended with */; Any text between will be ignored by the compiler.
/*
This is a multi-line comment
that's two lines long.
*/