C# Syntax

Summary: in this tutorial, you’ll learn about the basic C# syntax, including whitespace, statements, identifiers, keywords, literals, and comments.

Introduction to the C# syntax

C# syntax is similar to C/C++. This tutorial focuses on whitespace, statements, identifiers, keywords, literals, and comments.

Whitespace

Whitespace refers to the characters that do not have visible output, including:

  • Carriage return
  • Space
  • New Line
  • Tab

C# compiler ignores whitespace. But you use whitespace to make the code readable.

For example, the C# compiler will treat the following code snippets the same despite their differences in the presentation:

// with whitespace
bool isDark = false;

if (isDark)
{
    website.EnableDarkMode();
}Code language: C# (cs)
bool isDark = false; 
if (isDark){ website.EnableDarkMode();}Code language: C# (cs)

Statements

A statement is a source code instruction that declares a type or instructs the program to do something. A simple statement is terminated by a semicolon (;).

For example, the following code has two simple statements:

int age = 9;
Console.WriteLine("Welcome to C#");Code language: C# (cs)

The first statement defines an integer variable and initializes its values to 9. The second statement prints out a message to the console window.

Blocks

A block is a sequence of zero or more statements. A block starts with an opening curly brace ({) and ends with a closing curly brace (}).

For example, you can group the two statements above into a block like this:

{
    int age = 9;
    Console.WriteLine("Welcome to C#");
}Code language: C# (cs)

Unlike a statment, a block does not require a semicolon (;).

Identifiers

Identifiers are names that you choose for variables, functions, classes, methods, and so on. The identifier names follow these rules:

  • The alphabetic (a through z, A through Z) and underscore (_) characters can appear at any position.
  • Digits cannot appear in the first position but everywhere else.

C# identifiers are case-sensitive. For example, counter and Counter identifiers are different.

Keywords

Keywords are names that have special meanings to the compiler. All keywords are reserved identifiers. Therefore, you cannot use them as identifiers.

The following table shows the C# keywords:

abstracteventnamespacestatic
asexplicitnewstring
baseexternnullstruct
boolfalseobjectswitch
breakfinallyoperatorthis
bytefixedoutthrow
casefloatoverridetrue
catchforparamstry
charforeachprivatetypeof
checkedgotoprotecteduint
classifpubliculong
constimplicitreadonlyunchecked
continueinrefunsafe
decimalintreturnushort
defaultinterfacesbyteusing
delegateinternalsealedvirtual
doisshortvoid
doublelocksizeofvolatile
elselongstackallocwhile
enum   

Besides these keywords, C# has contextual keywords that provide specific meanings in the code. However, they are not reserved identifiers. And you’ll learn about them later tutorial.

If you must use an identifier with the same name as a reserved keyword, you can prefix it with the @ symbol. For example:

@classCode language: C# (cs)

Note that the @ symbol is not a part of the identifier. So @myVariable identifier is the same as myVariable.

In practice, you use the @ symbol when interfacing with libraries of other .NET languages that have different keywords.

Literals

Literals are primitive values in the program. For example, an integer has the following literal:

10Code language: C# (cs)

To form a string, you place the text inside the double quotes (") like this:

"Hello"Code language: C# (cs)

Comments

You use comments to explain the code or document it. The C# compiler ignores comments when compiling the program.

C# support three types of comments:

  • Single-line comments
  • Delimited comments
  • Documentation comments

Single-line comments

A single-line comment begins with a double forward-slash (//) and continues to the end of the line. For example:

int age = 18; // your ageCode language: C# (cs)

In this example, here’s the single-line comment:

// your ageCode language: C# (cs)

Delimited comments

A delimited comment starts with /* and ends with */. A delimited comment can span any number of lines. For example:

/*
    A delimited comment can span multiple lines
    and is ingored by the C# compiler
*/Code language: C# (cs)

Documentation comments

The documentation comments contain XML text used to make the program documentation.


/// <summary>
/// The main program
/// </summary>
class Program
{
    // ...
}Code language: C# (cs)

The documentation starts with three contiguous forward slashes (///).

Summary

  • C# compiler ignores whitespace such as carriage return, space, newline, and tab.
  • A simple statement is terminated with a semicolon (;). A block starts and ends with a pair of matching curly braces ({}).
  • C# identifers are case-sensitive.
  • C# supports single-line (//...), delimited comments (/*...*/), and documenation comments (///).
Was this tutorial helpful ?