ZetCode

C# lexical structure

last modified July 5, 2023

Computer languages, like human languages, have a lexical structure. A source code of a C# program consists of tokens. Tokens are atomic code elements. In C#, we have comments, variables, literals, white spaces, operators, delimiters, and keywords.

C# programs are composed of characters from the Unicode character set.

C# comments

Comments are used by humans to clarify the source code. There are three types of comments in C#. Single-line comments, multi-line comments and XML comments. XML comments can be extracted to HTML files.

Multi-line comments are enclosed by /* */ characters. Single line comments start with two forward slashes.

Program.cs
/*
    This is Program.cs
    Author: Jan Bodnar
    ZetCode 2022
*/

// A C# statement
Console.WriteLine("This is Comments program");

Comments are ignored by C# compiler.

/*
  This is Program.cs
/*  Author: Jan Bodnar */
  ZetCode 2022
*/

Comments cannot be nested; the above code does not compile.

C# white space

White space in C# is used to separate tokens in the source file. It is also used to improve readability of the source code.

int i = 0;

White spaces are required in some places. For example between the int keyword and the variable name. In other places, white spaces are forbidden. They cannot be present in variable identifiers or language keywords.

int a=1;
int b = 2;
int c  =  3;

The amount of space put between tokens is irrelevant for the C# compiler.

C# variables

A variable is an identifier which holds a value. In programming we say that we assign a value to a variable. Technically speaking, a variable is a reference to a computer memory where the value is stored. Variable names can have alphanumerical characters and underscores. An identifier may begin with a character or an underscore. It may not begin with a number.

Variable names are case sensitive. This means that Name, name, and NAME refer to three different variables. Variable names also cannot match language keywords. (In fact, we can use keywords as identifiers if we precede them with the @ character. But it is not a good programming practice.)

string name23;
int _col;
Date birth_date;

These are valid C# identifiers.

string 23name;
int %col;
Date birth date;

These are invalid C# identifiers.

Program.cs
string name = "Robert";
string Name = "Julia";

Console.WriteLine(name);
Console.WriteLine(Name);

Identifiers are case sensitive. Name and name are two different identifiers. In Visual Basic, a cousin of the C# language, this would not be possible. In this language, variable names are not case sensitive.

$ dotnet run
Robert
Julia

C# literals

A literal is a textual representation of a particular value of a type. Literal types include boolean, integer, floating point, string, character, and date. Technically, a literal will be assigned a value at compile time, while a variable will be assigned at runtime.

int age = 29;
string nationality = "Hungarian";

Here we assign two literals to variables. Number 29 and string "Hungarian" are literals.

Program.cs
bool sng = true;
string name = "James";
string job = null;
double weight = 68.5;
DateTime born = DateTime.Parse("November 12, 1987");

Console.WriteLine($"His name is {name}");

if (sng)
{
    Console.WriteLine("He is single");
} else
{
    Console.WriteLine("He is in a relationship");
}

Console.WriteLine($"His job is {job}");
Console.WriteLine($"He weighs {weight} kilograms");
Console.WriteLine($"He was born in {born:yyyy}");

In the above example, we have some literals. The bool literal may have value true or false. "James" is a string literal. The null represents the default value of any data type. Number 23 is an Integer literal. Number 68.5 is a floating point literal. Finally, November 12, 1987 is a date literal.

$ dotnet run
His name is James
He is single
His job is
He weighs 68.5 kilograms
He was born in 1987

C# operators

An operator is a symbol used to perform an action on some value. Operators are used in expressions to describe operations involving one or more operands.

+    -    *    /    %    ^    &    |    !    ~
=    +=   -=   *=   /=   %=    ^=    ++    --
==   !=    <   >    &=  >>=   <<=   >=   <=
||   &&    >>    <<    ?:

This is a partial list of C# operators. We will talk about operators later in the tutorial.

C# separators

A separator is a sequence of one or more characters used to specify the boundary between separate, independent regions in plain text or other data stream.

[ ]   ( )   { }   ,   :   ;
string language = "C#";

The double characters are used to mark the beginning and the end of a string. The semicolon (;) character is used to end each C# statement.

Console.WriteLine("Today is {0}", DateTime.Today.ToString("M/d"));

Parentheses (round brackets) are used to mark the method signature. The signature consists of method parameters. Curly brackets are used to denote the evaluated value.

int[] array = new int[5] {1, 2, 3, 4, 5};

Square brackets [] are used to denote an array type. They are also used to access or modify array elements. Curly brackets {} are also used to initiate arrays. Curly brackets are also used in variable interpolation or to enclose the body of a method or a class.

int a, b, c;

The comma character can be used to use multiple declarations on the same line of code.

C# keywords

A keyword is a reserved word in the C# language. Keywords are used to perform a specific task in the computer program. For example, define variables, do repetitive tasks, or perform logical operations.

C# has plenty of keywords. The keywords include if, else, for, while, base, false, float, catch, this, and many others. The keywords are gradually introduced in the tutorial.

Program.cs
for (int i = 0; i <= 5; i++)
{
    Console.WriteLine(i);
}

The example uses two keywords: int and for.

C# conventions

Conventions are best practices followed by programmers when writing source code. Each language can have its own set of conventions. Conventions are not strict rules; they are merely recommendations for writing good quality code. We mention a few conventions that are recognized by many C# programmers. (And often by other programmers too).

In this article we covered the basic lexical structure of the C# language.

Source

C# documentation

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all C# tutorials.