Chapter 3 - Variables and Expressions

  • 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 NameC#-Specific NameNominal Size in BitsValues
System.SBytesbyte8Integer between –128 and 127
System.Bytebyte8Integer between 0 and 255
System.Int16short16Integer between –32,768 and 32,767
System.UInt16ushort16Integer between 0 and 65,535
System.Int32int32Integer between –2,147,483,648 and 2,147,483,647
System.UInt32uint32Integer between 0 and 4,294,967,295
System.Int64long64Integer between –9,223,372,036,854,775,808 and 9,223,372,036,854,775,807
System.UInt64ulong64Integer between 0 and 18,446,744,073,709,551,615
System.Singlefloat32Floating-point values between 1.5 × 10^–45 and 3.4 ×10^38 in terms of +/−m × 2^e
System.Doubledouble64Floating-point values between 5.0 × 10^–324 and 1.7 ×10^308 in terms of +/−m × 2^e
System.Decimaldecimal128Floating-point values between 1.0 ×10^–28 and 7.9 ×10^28 in terms of +/−m × 10^e
System.Charchar16Single Unicode character, stored as an integer between 0 and 65,535.
System.Booleanbool16Boolean value, true or false.
System.StringstringdynamicA 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
城市应急指挥系统是智慧城市建设的重要组成部分,旨在提高城市对突发事件的预防和处置能力。系统背景源于自然灾害和事故灾难频发,如汶川地震和日本大地震等,这些事件造成了巨大的人员伤亡和财产损失。随着城市化进程的加快,应急信息化建设面临信息资源分散、管理标准不统一等问题,需要通过统筹管理和技术创新来解决。 系统的设计思路是通过先进的技术手段,如物联网、射频识别、卫星定位等,构建一个具有强大信息感知和通信能力的网络和平台。这将促进不同部门和层次之间的信息共享、交流和整合,提高城市资源的利用效率,满足城市对各种信息的获取和使用需求。在“十二五”期间,应急信息化工作将依托这些技术,实现动态监控、风险管理、预警以及统一指挥调度。 应急指挥系统的建设目标是实现快速有效的应对各种突发事件,保障人民生命财产安全,减少社会危害和经济损失。系统将包括预测预警、模拟演练、辅助决策、态势分析等功能,以及应急值守、预案管理、GIS应用等基本应用。此外,还包括支撑平台的建设,如接警中心、视频会议、统一通信等基础设施。 系统的实施将涉及到应急网络建设、应急指挥、视频监控、卫星通信等多个方面。通过高度集成的系统,建立统一的信息接收和处理平台,实现多渠道接入和融合指挥调度。此外,还包括应急指挥中心基础平台建设、固定和移动应急指挥通信系统建设,以及应急队伍建设,确保能够迅速响应并有效处置各类突发事件。 项目的意义在于,它不仅是提升灾害监测预报水平和预警能力的重要科技支撑,也是实现预防和减轻重大灾害和事故损失的关键。通过实施城市应急指挥系统,可以加强社会管理和公共服务,构建和谐社会,为打造平安城市提供坚实的基础。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值