• 沒有找到結果。

CSharp351Fall2021 Zheng-LiangLu C#Programming


Academic year: 2022

Share "CSharp351Fall2021 Zheng-LiangLu C#Programming"


加載中.... (立即查看全文)



C# Programming

Zheng-Liang Lu

Department of Computer Science & Information Engineering National Taiwan University

CSharp 351 Fall 2021


Class Information

• Instructor: Zheng-Liang Lu (Arthur)

• Email: arthurzllu@gmail.com

• The course website is


• All lecture slides are organized in English and will be modified if necessary.

• In-class examples may involve with high school math.


Teaching Philosophy

• I try to lower the barriers to entry.

Everything in my class is simple, essential, and general.1

• I could provide extra studying materials as many as possible.

• I answer your questions.

1“Simple is not easy. . . . Easy is a minimum amount of effort to produce a result. . . .Simple is very hard. Simple is the removal of everything except what matters. . . .” by Christopher S. Penn.

Zheng-Liang Lu C# Programming 2


Learning Tips

• Start with just onelanguage and master it.

• Ask lots of questions; Google first.

• Practice makes permanent (and hopefully, perfect).2

• It may take 10000 hours, more or less3; it is never too late.

• Grasp the fundamentals for long-term benefits; code from the bottom.

• Code by hand; it sharpens proficiency.


3Malcolm Gladwell (2008): Outliers.


“Knowledge is of no value unless you put it into practice.”

– Anton Chekhov (1860-1904)

“Many roads lead to the path, but basically there are only two: reason and practice.”

– Bodhidharma

Zheng-Liang Lu C# Programming 4


Grading Policy

• To acquire the class certificate, you need to finish all labs listed in the course page.4

4Seethe list of programming labs.


Roll Call

Zheng-Liang Lu C# Programming 6


1 class Lecture1 {


3 "Introduction"

4 5 }


7 // Keywords:

8 using, namespace, class, static, void, string


Zheng-Liang Lu C# Programming 8




Goal: Problem Solving

• Programming is to provide computational solutions to real-world problems.

• The resulting solution is called a program.

Zheng-Liang Lu C# Programming 10



• A program is a collection ofinstructionsto perform a specified task.

• They are almost everywhere, for example,

video games (e.g. Pok´emon Go, Travel Frog, . . . );

operating systems (e.g. Linux, . . . );

autonomous driving5/ auto-piloting (e.g. MRT, airplane, . . . );

search engines (e.g. Google, . . . );


computer malware7;

and more.8


6Seehttps://www.bostondynamics.com/and watch videos from its official YouTube channelhere.

7Seemalwareandcomputer virus.

8SeeInternet of Things (IoT).


How to Execute Programs


• Once the program is activated, both data and instructions are loaded from the disk into themain memory.

• We now call it a process, which is the smallest unit of resource allocation.9

• Then the instructions in the program are scheduledto be executed by theCPU.10

A CPU consists of 6 main components: arithmetic & logic units (ALUs), registers, control units, cache, buses, and clock.11

• The immediate result is stored back to the main memory and further written into the disk if necessary.

9Seehttps://en.wikipedia.org/wiki/Process (computing).

10Seehttps://en.wikipedia.org/wiki/Scheduling (computing).

11Seehttps://en.wikipedia.org/wiki/Central processing unit.

12SeeInside Your Computerby Bettina Bair andSystems Architectureby BBC.

Zheng-Liang Lu C# Programming 12


Memory Hierarchy


13See Figure 1-9 in Bryant, p. 14.


Programming Languages

• A programming language is an artificial language to communicatewith machines.14

• The elements of programming languages are syntaxand semantics, used to control the behavior of machines.

• Top 20 programming languages can be found in TIOBE.

• Every language originates from some reasons.

14Seehttps://en.wikipedia.org/wiki/Programming language.

Zheng-Liang Lu C# Programming 14


A Short History


• Gen 1: machine code.

• Gen 2: assembly code.

• Gen 3: high-level programming languages.

For example, C, C++, Java, C#, Python.

• Gen 4.

For example, SQL.

15Seegenerations of languageandcomputer programming history.


Zheng-Liang Lu C# Programming 16


What Can A Program Do?

• A programis an implementation of an algorithmexpressed in a specific programming language.


First Glance at Algorithms


• Simply put, an algorithm is a well-defined procedurethat solves a specific category of problems, like a recipe or a cookbook.

• In general, an algorithm may take adequate information as its input and produce the correct result as itsoutput.

16Also seeWhat’s an Algorithm?by David J. Malan.

Zheng-Liang Lu C# Programming 18


Example: Largest Number

• Let A be a nonempty list of any real numbers.

• Now try to propose an algorithm to find the largest number for any list of numbers.

Input: A.

Algorithm: your work here...

Output: the largest number in A.

• For example, assume that A = {1, 7, 9, −2, 4}.

• It is clear that the answer is 9.

• Can you teach the computer to repeat your idea?


Optimal Algorithm (Pseudo Code)

• For convenience, A(i ) denotes the i -th number of A and the symbol ← denotes a copy operator from right to left.

• Then the optimal algorithm could be as follows:

1 max<− A(1) // Initial guess, without loss of generality!

2 for i <− 2 ˜ n

3 if A(i)> max

4 max <− A(i)

5 end

6 end

7 return max

Zheng-Liang Lu C# Programming 20



• In Line 1, why not max ← 0 ?

• You may extend this algorithm for following questions:

smallest number;

location of the largest (also smallest) number;

... (more and more)

• We are eager for efficient algorithms.17

Will “sorting the list and then the largest number could be the last number” be more efficient than the previous one?

17We will show you thecomplexity analysisin the end of the chapter of loops.


“Computers are good at following instructions, but not at reading your mind.”

– Donald Knuth (1938-)

“There are two ways of constructing a software design:

One way is to make it sosimplethatthere are obviously no deficiencies, and the other way is to make it socomplicated thatthere are no obvious deficiencies. The first method is far more difficult.”

– Tony Hoare (1934-)

Zheng-Liang Lu C# Programming 22


Alan Turing

• Provided a formalization of the concepts of algorithmand computation with theTuring machine18, which can be considered a model of a general-purpose computer.

• Proposed the famous question: “Can machines think? ”19

Well-known as the Turing test.

• Turing Award is recognized as the highest distinction in computer science and the“Nobel Prize of computing”.20

18Turing (1936). Trythisgoogle doodle.

19Turing (1950). You could find the paperhere. Also seeTuring Test.

20Seehttps://en.wikipedia.org/wiki/Turing Award#Recipients.


• You may watch the film The Imitation Game (2014).

• Britain’s £50 note will honor computing pioneer Alan Turing.21

21SeeAlan Turing to be the Face of New£50 Noteby Bank of England.

Zheng-Liang Lu C# Programming 24


About C#

• C# 1.0 was designed byAnders Hejlsberg from Microsoft in 2000 and nowadays we have C# 10 released in 2021.22

• We will focus on C# 8.0, which is the most popular version.23

• Note that C# has many similar syntax and design rules to C++ and Java.24

• C# are widely used in Windows client applications (say Windows Presentation Form25) and web applications (say ASP.NET26).

22SeeThe history of C#.


24SeeComparison of C# and JavaandJava vs C#for comparisons.

25SeeWhat is Windows Presentation Foundation (WPF)?.



Microsoft Tech Stack


27Also see5 Reasons Why Microsoft Stack Is Still A Viable Choice.

Zheng-Liang Lu C# Programming 26


Common Language Runtime (CLR)


• The .NET Framework provides a run-time environment called the common language runtime, which runs the code and provides services that make the development process easier.

• Clearly, CLR is avirtual machine which executes C#


• It is similar to the relationship between Java Virtual Machine (JVM) and Java programs.28

28SeeCLR vs JVM.



Software Installation

• For Windows users, download and launch the installer for Visual Studio Community 2019; you need to install.Net Desktop Development for this course.

• For MacOS users, tryVisual Studio for Mac and install (at least).Net Core.

• You could also try other IDEs, say Visual Studio Codewith proper packages .

Zheng-Liang Lu C# Programming 28


First Program: Hello, World


1 using System;


3 namespace HelloWorldApp

4 {

5 class Program

6 {

7 static void Main(string[] args)

8 {

9 // This is my first program.

10 Console.WriteLine("Hello World!");

11 }

12 }

13 }

30Seehttps://en.wikipedia.org/wiki/%22Hello, World!%22 program.


Table of Special Characters

Symbol Name Description

{ } Opening/closing braces Denote a block to enclose statements.

( ) Opening/closing parentheses Mostly used with methods.

[ ] Opening/closing brackets Denote an array.

// Double slashes Precede a comment line.

” ” Opening/closing quotation marks Enclose a string.

; Semicolon Mark the end of a statement.

Zheng-Liang Lu C# Programming 30



• A bug is an error, flaw, failure, or fault in a computer program or system, producing an incorrect or unexpected result, or misbehaving in unintended ways.

Compile-time error: most of them are syntax errors.

Runtime error: occurs when the C# program runs, e.g. 1/0.

Logic error: introduced by implementing the functional requirement incorrectly.

• Note that logic (semantic) errors are the obscurest because they are hard to be found.


“If debugging is the process of removing software bugs, then programming must be the process of putting them in.”

– Edsger W. Dijkstra (1930–2002)

“Why do we fall sir? So that we can learn to pick ourselves up.”

– Alfred Pennyworth, Batman Begins (2005)

Zheng-Liang Lu C# Programming 32


Coding Style

• Good coding style makes a program easy to read and helps programmers prevent from errors.

For example,C# Coding Conventionsby Microsoft.

• In particular, we use indentationto enhance the structural relationships by visual.

• Be consistent through the whole program (even project)!



2 For example, we will visit more reference types, like interface and delegate.. Zheng-Liang Lu C#

• Performance: vectorized code often runs much faster than the corresponding code containing loops.. Zheng-Liang

Sometimes called integer linear programming (ILP), in which the objective function and the constraints (other than the integer constraints) are linear.. Note that integer programming

• The first module, written in assembly language, contains the external procedure. • The second module contains the C/C++ code that starts and ends

Building on the strengths of students and considering their future learning needs, plan for a Junior Secondary English Language curriculum to gear students towards the learning

It provides details about promoting Language across the Curriculum at the secondary level, the learning and teaching of language arts and the four language skills, namely

(provides student activities, parent education and support, assessment and counselling services) Youth Assessment and Development Centre, The Hong Kong Federation of Youth Groups

The Hong Kong school curriculum, made up of eight key learning areas (under which specific subjects are categorised), provides a coherent learning framework to enhance