C# lexical structure

xingyun86 2021-5-30 1338

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
using System;

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

namespace Comments
{
    // Program starts here
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This is Comments program");
        }
    }
}

Comments are ignored by C# compiler.

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

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

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 Namename, 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
using System;

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

This is program output.

C# literals

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
using System;

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

This is the output of the program.

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

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# is rich in keywords. Many of them will be explained in this tutorial. The keywords include ifelseforwhilebasefalsefloatcatchthis, and many others.

Program.cs
using System;

namespace Keywords
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;

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

In the above example, we use several keywords. The usingpublicstaticvoidint, and for are C# keywords.

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

  • Classes, Interfaces and Enums begin with an uppercase letter.
  • Interface names start with an I letter.
  • Comments are placed on a separate line, not at the end of a line of code.
  • Method names begin with a uppercase letter.
  • Only one statement or declaration is placed per line.
  • Identifiers are easily readable and meaningful.
  • Identifiers are written in PascalCase, e.g. each subsequent word begins with a capital letter.
  • The public keyword precedes the static keyword when both are used.
  • The parameter name of the Main method is called args.
  • Constants are written in uppercase.
  • Curly braces in code blocks start on a new line.

In this part of the C# tutorial, we covered the basic lexis for the C# language.


×
打赏作者
最新回复 (0)
只看楼主
全部楼主
返回