- Recap
1. What do computer programs basically do?
Suffice it to say that a computer program consists of a series of operations that manipulate data.
2. What do computer programs basically need?
Computer programs need a way to store data as well as methods for manipulating it. "Variables" and "Expressions" are just suitable for such demand.
3. What does the whole syntax of C# consist of?
3.1 Statement
Statement is the basic unit of C# code ended by a semicolon. For readability, it is usual to add carriage returns after each one, though unnecessary.
3.2 Block
Block is the basic unit of C# code structure which contains 0 to multiple statements and is delimited by a pair of curly brackets. Blocks can also be nested so that each of them has its own level of indentation. For readability, the more nested blocks become, the further they indent.
{
<code line 1>;
{
<code line 2>;
<code line 3>;
}
<code line 4>;
}
3.3 Comment
Comment is, strictly speaking, not C# code at all. But it can cohabit with it and provides description to the code. There are 3 different styles of formats of comments available in C#.
3.3.1 Single Line
The comment starts with "//" in a single line. All text after that is the comment until the line break.
3.3.2 Multiple Lines
The comment starts with "/*" and ends with "*/". All text in between is the comment but allows no "*/" content inside.
3.3.3 XML
The comment starts with "///" and stays in a single line just like the single line one. However, it provides the capability to create XML documentation for the C# code.
3.4 Case
C# code is case-sensitive.
3.5 Outlining
To achieve code outlining functionality that helps to group C# code, "#region" and "#endregion" can encapsulate lines of code into a region. Thus, the codes within can be expanded or collapsed alternatively in the code editor.
4. How to store data in C#?
4.1 Variable
In order to store data in a C# program, variables are used.
4.1.1 Variables are strongly typed
Because different data types may require different manipulations, a type systems exists in C# to classify variables.
4.1.2 Declaration comes first
Before using variables, it is required to assign them names and types. Syntax is:
<type> <name>;
4.1.3 Value assignment follows
After declaration, values can be assigned to variables in many ways. Syntax is:
<name> = <value>;
4.2 Simple Types of Variables
Some simple and predefined types are provided by CTS for primitive data storage. They have both common and C#-specific names. They occupy memory spaces diversely.
Common Name | C#-Specific Name | Nominal Size in Bits | Values |
---|---|---|---|
System.SByte | sbyte | 8 | Integer between –128 and 127 |
System.Byte | byte | 8 | Integer between 0 and 255 |
System.Int16 | short | 16 | Integer between –32,768 and 32,767 |
System.UInt16 | ushort | 16 | Integer between 0 and 65,535 |
System.Int32 | int | 32 | Integer between –2,147,483,648 and 2,147,483,647 |
System.UInt32 | uint | 32 | Integer between 0 and 4,294,967,295 |
System.Int64 | long | 64 | Integer between –9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 |
System.UInt64 | ulong | 64 | Integer between 0 and 18,446,744,073,709,551,615 |
System.Single | float | 32 | Floating-point values between 1.5 × 10^–45 and 3.4 ×10^38 in terms of +/−m × 2^e |
System.Double | double | 64 | Floating-point values between 5.0 × 10^–324 and 1.7 ×10^308 in terms of +/−m × 2^e |
System.Decimal | decimal | 128 | Floating-point values between 1.0 ×10^–28 and 7.9 ×10^28 in terms of +/−m × 10^e |
System.Char | char | 16 | Single Unicode character, stored as an integer between 0 and 65,535. |
System.Boolean | bool | 16 | Boolean value, true or false. |
System.String | string | dynamic | A sequence of characters |
4.3 Variable Naming
Although you may not choose arbitrary character sequence for naming a variable, C# still leaves a flexible naming system.
4.3.1 Basic Rules
The first character of a variable name must be either a letter, an underscore character (_), or the at symbol (@); Subsequent characters may be letters, underscore characters, or numbers. Besides, certain keywords cannot be used.
4.3.2 Naming Convention
It is accepted to name variables in C# for their purposes. And there are two types of conventions used.
4.3.2.1 Pascal Case
Every english word of the variable name should be in lowercase except for its initial letter. Variables with advanced functionalities take this convention.
4.3.2.2 Camel Case
The first word starts with a lowercase letter and for the rest of them keep the same as Pascal Case. Simple variables take this convention.
4.4 Literal Values
Different data types have different literal values to represent concrete data.
4.4.1 Non-string types
Some of them use suffixes to differentiate.
4.4.2 Strings
Escape sequences are used to represent special characters in a string. Any string starting with @ is called verbatim in which every character except " is not escaped.
5. How to manipulate data in C#?
By combining operators with variables and literal values (together referred to as operands), we can create expressions that generate a result each, in order to achieve basic computation. The operators can be classified based on their purposes as follows.
5.1 Mathematical Operators
5.1.1 Unary (Upon single operand)
+, -, ++(prefix or suffix), --(prefix or suffix)
5.1.2 Binary (Upon two operands)
+, -, *, /, %
5.2 Assignment Operators
5.2.1 Binary (Upon two operands)
=, +=, -=, *=, /=, %=
6. How to organize data in C#?
Namespace is the .NET way of providing code containers, and is also a means of categorizing items in the .NET Framework.
6.1 Define namespace
Every item in the .NET Framework must reside in a specific namespace. The syntax of namespace definition is as follows.
namespace LevelOne
{
// Items of LevelOne are defined inside
namespace LevelTwo
{
// Items of LevelTwo are defined inside
// More namespaces can be nested
}
}
6.2 Access namespace
Names in the namespaces need being accessed from each other. However, if the target name is outside the source name's namespace, then the target's namespace needs to be qualified. These rules apply:
6.2.1 Period characters (.) are used between namespaces that are adjacent.
6.2.2 If the target namespace is a child namespace of the source one, then the relative notation is used.
6.2.3 If the target namespace is not a child namespace of the source one, then the absolute notation (from the global namespace) is used.
6.2.4 "using" statement is to simplify access the names of a specific namespace.
- Quiz
1. Will #region affect the execution of the code in any way? If so, how?
2. Compare the commonalities and differences between the following data types, and state when they should be used respectively.
2.1 char & byte
2.2 double & decimal
3. Why does C# introduce namespaces?
4. Why does C# bother adding suffixes to some literal values?
5. State the connections and differences between expression and statement.