C# Programming
Location: Room 108, 德田館
Time: 1900 ~ 2200
``Just because people tell you it can't be done,
that doesn't necessarily mean that it can't be done.
It just means that they can't do it.''
-- Anders Hejlsberg
``It's not a bug - it's an undocumented feature.''
-- Anonymous
Instructor Information
- Name: 盧政良 (Zheng-Liang Lu)
- Email: arthurzllu at gmail.com
Software Installation & WiFi Access
- IDE: Visual Studio Community 2022 You need to sign up for a Microsoft account!
- Wi-Fi setting
- SSID: CSIE_guest or CSIE_guest-5G
- Account: guest_5WCMB
- Passwords: FAK3FWBQ
Make-up Class Method Students are advised to utilize the website NTU COOL for class compensation. Please note, the required login credentials are those associated with your individual training class account.
Recording Classroom Lectures Policy Recording of classroom lectures is prohibited unless advance written permission is obtained from the class instructor and any guest presenter(s).
Objectives
- Learn to write computer programs in C# on .NET 8
- Comprehend computational thinking
- Understand basic concepts of computer science
Syllabus
Algorithm-based programming
- Data types, variables, and operators
- Flow controls: branching & repetition
- Arrays & more data structures
- Methods
Object-oriented programming
- Class & object
- Encapsulation
- Class & interface inheritance
- Subtype polymorphism and method overriding
- SOLID principles
- Delegation
- Selected design patterns: singleton and strategy
Advanced topics
- Exception and its handling
- Generic programming
- LINQ
- Winform and Windows Presentation Foundation (WPF)
- Concurrent programming: multithreading
- ASP.NET
Date |
Summary |
2024.4.9
|
- Background knowledge: computer system (cpu-memory model), programming languages, algorithms, Alan Turing
- First C# program: console application
- Homework 0 (no due date)
- Installation of Visual Studio (see the slides or you may follow the instructions here)
- (FYR) Microsoft, C# language versioning, 2023
- (FYR) JetBrains, C# Developer Ecosystem, 2023
- (FYR) Visual Studio 2022 roadmap
- (FYR) Technology trees: https://roadmap.sh/
- (FYR) Google, Kids Coding, 2012
- (FYR) Computer games: one-page slide
- (FYR) DeepMind: AlphaGo, 2017
- (FYR) CS50, Large Language Models and The End of Programming - Tech Talk with Dr. Matt Welsh, Harvard University, 2023.10.30
|
2024.4.12
|
- Data types, variables, and operators
- Variable declaration (with memory address)
- Primitive types: int, double, char / string, bool
- Operators: assignment operator (=), arithmetic operators (+, −, *, /, %), relational operators (>, >=, <, <=, ==, !=), logical operators (!, &&, ||, ∧)
- Type conversion (casting)
|
2024.4.16 (2 hours)
|
- Data types, variables, and operators (cont'd)
- Operators: compound arithmetic operators (+=, ++)
- Misc: console input, parsers, string format
- Flow controls
- Selections (branching): if-else, switch-case-default-break, ?: operator
- Digression: pseudorandom number generator (PRNG)
- Repetitions: while loop
|
2024.4.19
|
- Flow controls (cont'd)
- Repetitions: do-while loop, for loop
- Jump statements
- Nested loops
- Case study: Monte Carlo
- (FYR) Analysis of algorithms: big O
- Homework 1
|
2024.4.23 (3.5 hours)
|
- Data structures
- Arrays: syntax & memory allocation
- Examples of processing arrays
- Foreach loops
- Cloning arrays (shallow copy?)
- Random permutation (shuffling)
- Some algorithms: sorting, searching (especially binary search)
- Multidimensional arrays and jagged arrays
- A glimpse on linked list
- Generics and more data structures: list, stack, dictionary
- Homework 2
|
2024.4.26 (3.5 hours)
|
- Methods
- Method definition & implementation
- Call stack & variable scope (again, with memory model)
- Pass by reference: ref, in, out
- Implicitly typed local variables: var
- Method overloading
|
2024.4.30
|
- Methods (cont'd)
- Variable-length arguments: param
- Optional arguments
- The Main method with command-line arguments
- Object-oriented programming
- Class & object
- Encapsulation: private fields + public methods = public properties; auto-implemented properties
- Constructors
- Homework 3
|
2024.5.3
|
- Object-oriented programming (cont'd)
- Garbage collection (GC)
- Unified modeling language (UML): class diagram
- Self-reference: the this operator
- HAS-A relationship: association, aggregation, composition
- Instance members vs. static members
- Briefing design patterns: Singleton Pattern
- First IS-A relationships: class inheritance
- Constructor chaining with the base operator
- Method overriding: virtual & override
- Homework 4
|
2024.5.7
|
- Object-oriented programming (cont'd)
- Subtype polymorphism: Liskov substitution principle, up / down casting, and the ``as'' operator
- Runtime type testing: the ``is'' operator
- Abstract method & class
- Sealed method & class
- Second IS-A relationships: interface inheritance
|
2024.5.10
|
- Object-oriented programming (cont'd)
- Delegation and anonymous function (lambda expressions)
- Case study: Strategy Pattern
- Enumeration, structure, tuple, and nested types
- Namespaces
- Access modifiers
- Exception handling
- Generic programming
- LINQ
- Install and manage packages in Visual Studio using the NuGet Package Manager
- Logger: NLog
- Windows Presentation Foundation (WPF)
- All previous homework submissions are due by 5.19 midnight.
- Homework 5
|
Promotion |
|
Sample code
Gradebook
Programming Labs
Late Homework Policy All programming labs should be submitted before the last date of class in order to deliver your final grades to the office so that the certificates can be issued soon. (See a recent complaint letter here.)
Homework Submission Send the source code (the file with .cs extension) to my email shown in the beginning of this page. Remember to indicate this class number, your full Chinese name, and homework number in your email title. For example, [CSharp 407] Homework 1 盧政良. Do not attach any class file because any executable file will be ruled out by Gmail.
Lab 1 Number-Guessing Game
Write a program for number-guessing game (you may refer to Guess the Number if you have never played this game). The program first generates a secret number ranging between 0 and 99, inclusive. Then the program asks the player to guess a number. If the input number is equal to the secret number, then the player wins. If not, then update the range depending on the input accordingly. (For example, assume that the secret number is 42. If the player types 50 for the first time, then the program shows (0, 49) on the screen.) When there is only one integer left, the player loses the game. Also, make sure that the player types a number in the feasible range; otherwise, ask the player to redo the input.

Lab 1-1 (Optional)
Add some strategies (AI?) to play this game and calculate each winning rate by using these strategies for 1e5 times. For example, the naive strategy is to guess a number in random, and its result is around 66%. Surprisingly, the winning rate for binary search is about 63%, not as expected to beat the naive one. Why?
Lab 1-2 (Optional)
Find the optimal strategy to beat the former two. The result is shown below. As you could see, the winning rate of my optimal strategy reaches 99%!

Lab 1-3 (Optional)
Modify the game loop which allows the player to guess at most 7 times (why?). Also report their performance like below:

The performance of binary search remains ~63% while the other two degrade severely!
Lab 2 Contagion Control
The virus, COVID-19 (aka Wuhan coronavirus), is primarily spread from close contact with infected people. You task is to design an algorithm to identify the chain of virus transmission from one person to the next. For simplicity, let N be the number of citizens, each denoted by an integer ID from 0 to N − 1. Each citizen is asked to keep a record of the ID of the person they comes in contact with. It is assumed that no two citizens keep the same ID of the citizen in close contact. To avoid typing numbers by hand, I suggust using the shuffling algorithm to generate a random sequence of 0, 1, 2, ..., N − 1 for testing. For example, consider N = 16 and assume that the citizen with ID 0 is initially infected. Your program is to identify and list all IDs along this infection chain. A possible output is shown below.

Let M be a 2D matrix whose number of ``rows'' and ``cols'' are integers specified by user input from the keyboard. Write a program for the following tasks: (1) generate integers from 0 to rows * cols − 1 and fill up a 2D array (more explicitly, created by calling new int[rows, cols]) with these integers in a random order (you could use the shuffle algorithm here), (2) sort M by performing row exchanges based on the numbers of the first columns in ascending order, and (3) output the resulting matrix. Note that you need to encapsulate some code snippets into functions / methods as needed.

Lab 3-1 (Optional)
Redo Lab 3 but using List<List<int>>
instead of native arrays for M.
Lab 4 Refactoring Number-Guessing Game by Using Design Patterns
Rewrite your program of Lab 1 in OOP style. First analyze the functionalities of Lab 1 and then define at least two classes for each functionality. For instance, you could create two objects: one object to manage the game procedure, the other for the player who generates an integer within feasible range during the game. To achieve this, you will need to define a protocol, similar to the relationship between the Console.WriteLine method and the ToString method of any C# object. Hence the game will have minimal dependency on the player (the game won't need to know which kind of player during the game procudure). A possible class design in UML is shown below.

Lab 5 Simple Calculator by Windows Presentation Foundation (WPF)
Use WPF to implement a simple calculator illustrated in the figure below.

References
C# Programming Language
Design Patterns
- Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Grady Booch, Design Patterns: Elements of Reusable Object-Oriented Software, 1994
- Robert C. Martin, Agile Software Development, Principles, Patterns, and Practices, 2005
- Alexander Shvets, Gerhard Frey, and Marina Pavlova, Design Patterns
- Eric Freeman and Elisabeth Robson, Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software, 2/e, 2020 (highly recommended!!!)
- Kenneth M. Anderson, Object-Oriented Analysis & Design, Department of Computer Science, University of Colorado, 2012fa (highly recommended!!!)
- Zach Tatlock, CSE 331: Software Design and Implementation, Computer Science & Engineering, University of Washington, 2017 winter (highly recommended!!!)
- Josh Bloch and Charlie Garrod, Principles of Software Construction: Objects, Design, and Concurrency, Carnegie Mellon School of Computer Science, 2016fa
- Max Goldman and Armando Solar-Lezama, 6.005: Software Construction, EECS, Massachusetts Institute of Technology, 2016fa
- 蔡昇達, 設計模式與遊戲開發的完美結合, 2/e, 2019
- Alexander Shvets, Dive Into Design Patterns, 2019
.Net Framework / Core
Windows Presentation Foundation (WPF)
ASP.Net