C# lexical structure

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.

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.

using System;

/*
  This is comments.cs 
  Author: Jan Bodnar 
  ZetCode 2013
*/

public class Comments
{
    // Program starts here
    static void Main()
    {
        Console.WriteLine("This is comments.cs");
    }
}

Comments are ignored by the C# compiler.

/*
  This is comments.cs 
/*  Author: Jan Bodnar */
  ZetCode 2013
*/

Comments cannot be nested. The above code does not compile.

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.

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.

using System;

public class Identifiers
{
    static void Main()
    {
        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.

$ dmcs identifiers.cs
$ ./identifiers.exe 
Robert
Julia

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.

using System;

public class Literals
{
    static void Main()
    {        
        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 {0}", name);

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

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

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.

$ ./literals.exe 
His name is James
He is single
His job is 
He weighs 68.5 kilograms
He was born in 1987

This is the output of the program.

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.

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.

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# is rich in keywords. Many of them will be explained in this tutorial. The keywords include if, else, for, while, base, false, float, catch, this, and many others.

using System;


public class CSharpApp
{
    static void Main()
    {
        int i;

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

In the above example, we use several keywords. The using, public, static, void, int, and for are C# keywords.

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 part of the C# tutorial, we covered the basic lexis for the C# language.