C
#
Programming
Rob Miles
Edition 2.0 July 2010
Department of Computer Science University of Hull
i
Contents
Introduction 5
Welcome ............................................................................................................................. 5
Reading the notes ................................................................................................................ 5
Getting a copy of the notes ................................................................................................. 5
1
Computers and Programs 6
1.1
Computers ............................................................................................................ 6
1.2
Programs and Programming ................................................................................. 8
1.3
Programming Languages .................................................................................... 13
1.4
C# ....................................................................................................................... 14
2
Simple Data Processing 18
2.1
A First C# Program ............................................................................................ 18
2.2
Manipulating Data .............................................................................................. 26
2.3
Writing a Program .............................................................................................. 37
3
Creating Programs 52
3.1
Methods .............................................................................................................. 52
3.2
Variables and Scope ........................................................................................... 58
3.3
Arrays ................................................................................................................. 61
3.4
Exceptions and Errors......................................................................................... 65
3.5
The Switch Construction .................................................................................... 68
3.6
Using Files .......................................................................................................... 71
4
Creating Solutions 76
4.1
Our Case Study: Friendly Bank .......................................................................... 76
4.2
Enumerated Types .............................................................................................. 76
4.3
Structures ............................................................................................................ 79
4.4
Objects, Structures and References .................................................................... 82
4.5
Designing With Objects ..................................................................................... 89
4.6
Static Items ......................................................................................................... 94
4.7
The Construction of Objects ............................................................................... 98
4.8
From Object to Component .............................................................................. 104
4.9
Inheritance ........................................................................................................ 109
4.10
Object Etiquette ................................................................................................ 119
4.11
The power of strings and chars ......................................................................... 124
4.12
Properties .......................................................................................................... 126
4.13
Building a Bank ................................................................................................ 129
5
Advanced Programming 134
5.1
Generics and Collections .................................................................................. 134
5.2
Storing Business Objects .................................................................................. 138
5.3
Business Objects and Editing ........................................................................... 148
5.4
A Graphical User Interface ............................................................................... 153
5.5
Using Delegates ................................................................................................ 162
5.6
Threads and Threading ..................................................................................... 163
ii
5.7
Structured Error Handling ................................................................................ 171
5.8
Program Organisation ....................................................................................... 174
5.9
Debugging ........................................................................................................ 180
5.10
The End? .......................................................................................................... 183
6
Glossary of Terms 184
Abstract ........................................................................................................................... 184
Accessor .......................................................................................................................... 184
Base ................................................................................................................................ 184
Call ................................................................................................................................. 184
Class ............................................................................................................................... 184
Code Reuse ..................................................................................................................... 185
Cohesion ......................................................................................................................... 185
Collection ........................................................................................................................ 185
Compiler ......................................................................................................................... 185
Component ...................................................................................................................... 185
Constructor ..................................................................................................................... 185
Coupling ......................................................................................................................... 186
Creative Laziness ............................................................................................................ 186
Delegate .......................................................................................................................... 186
Dependency .................................................................................................................... 186
Event ............................................................................................................................... 186
Exception ........................................................................................................................ 186
Functional Design Specification ..................................................................................... 187
Globally Unique Identifier (GUID) ................................................................................ 187
Hierarchy ........................................................................................................................ 187
Immutable ....................................................................................................................... 187
Inheritance ...................................................................................................................... 188
Interface .......................................................................................................................... 188
Library ............................................................................................................................ 188
Machine code .................................................................................................................. 188
Member ........................................................................................................................... 188
Metadata ......................................................................................................................... 188
Method ............................................................................................................................ 188
Mutator ........................................................................................................................... 189
Namespace ...................................................................................................................... 189
Overload ......................................................................................................................... 189
Override .......................................................................................................................... 189
Portable ........................................................................................................................... 189
Private ............................................................................................................................. 189
Property .......................................................................................................................... 190
Protected ......................................................................................................................... 190
Public .............................................................................................................................. 190
Reference ........................................................................................................................ 190
Signature ......................................................................................................................... 190
Source file ....................................................................................................................... 190
Static ............................................................................................................................... 190
Stream ............................................................................................................................. 191
Structure .......................................................................................................................... 191
Subscript ......................................................................................................................... 191
Syntax Highlighting ........................................................................................................ 191
Test harness .................................................................................................................... 191
This ................................................................................................................................. 191
Typesafe .......................................................................................................................... 192
Unit test........................................................................................................................... 192
Value type ....................................................................................................................... 192
© Rob Miles 2010 Department of Computer Science, The University of Hull.
Reward Your Curiosity
Everything you want to read.
Anytime. Anywhere. Any device.
No Commitment. Cancel anytime.