csharp_lexical_structure.md - brainchildservices/curriculum GitHub Wiki
SLIDE-1
-
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.
-
SLIDE-2
-
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
- 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.
SLIDE-2(DOWNWARDS)
USE A CODE EDITOR AND SEE COMMETED SECTION
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.
SLIDE-3
- 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.
- White spaces are required in some places. For example between the int keyword and the variable name.
- In other places, white spaces are forbidden.
- The amount of space put between tokens is irrelevant for the C# compiler.
SLIDE-4
- 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.(Advantage of C# language)
SLIDE-4(DOWNWARDS)
string name23;
int _col;
Date birth_date;
These are valid C# identifiers.
string 23name;
int %col;
Date birth date;
These are invalid C# identifiers.
SLIDE-5
- 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.
SLIDE-6
- 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.
SLIDE-7
- 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.
- 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.
- 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.
- The comma character can be used to use multiple declarations on the same line of code.
-
SLIDE-8
-
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.
- The keywords include if, else, for, while, base, false, float, catch, this, and many others.
- The using, public, static, void, int, and for are C# keywords.
-
REF Link: