Introductory Programming
with C#
Judith Bishop
University of Pretoria, South Africa
Visiting TU-Berlin
Microsoft Courses 2003
1
Talk overview
 Introduction
– Who, why, when, how, with what?
 Syntax and semantics
– for you and your students
 A tour through some lessons
–
–
–
–
Early concepts
Our approach to GUIs
Debugging
Advanced concepts
 Assessment
– Quizzes, exercises, laboratories, exams
 References
Microsoft Courses 2003
2
Featuring …
 Syntax and semantics descriptions
 Early use of libraries
 Independent GUI specifications
 Debugging
Microsoft Courses 2003
New
3
Focus on C#
 Designed by Anders Hejlsberg, Scott Wiltamuth and
Peter Golde
 To b the main development medium for future
Microsoft products
 Origins in C++, Java, Delphi, Modula-2, Smalltalk
 Heljsberg was the chief architect behind Turbo
Pascal and Delphi
 Standardised by Ecma and ISO
 Free (to us)
Microsoft Courses 2003
4
Changing languages
 Major movements worldwide
– To Pascal in 1970s and 1980s
– To Java in 1990s
 Caused by advances in technology
– Data structures, oops, internet computing
– Foundation for later courses
– Desire to be "ahead of the pack"
 Inhibitors to change
–
–
–
–
Lack of teaching resources
Computing resources required by new technology
Investment in current language
Uncertainty over the measure of improvement
Microsoft Courses 2003
5
A first programming course
 Typically 40-50 lectures
 10-14 laboratories
 Take home assignments
 Project
 Questions:
–
–
–
–
–
–
where does it start?
where does it end?
what is the place of libraries?
what is the order of topics?
what should be included/left out?
what do I need to run the language?
Microsoft Courses 2003
6
Where does it start and end?
 Most institutions assume "no background"
– most students have more than that
– use of computers is almost universal now
– students can interact with GUIs
 Strong desire to have "objects first"
– but what is second?
– what do objects assume?
 With or without GUIs?
– huge tension between need to program realistically and the number
of concepts required to express GUIs
 Advanced topics can be left to other courses
– networking and databases - NetCentric Computing
– generics and overloading - Data Structures
Microsoft Courses 2003
7
What about libraries?
 Libraries cannot be ignored,
– More in the libraries than in the language!
– Without them, examples will be too constrained
 Students can be on a "need to know basis", BUT they
need to know the structure and organisation of
libraries
– This knowledge transcends languages
 Early use of libraries introduces many fundamental
concepts in a controlled manner, e.g.
–
–
–
–
variables vs properties
instance vs static
constructors
parameters
Microsoft Courses 2003
8
An order of topics
 Introduction to computers, languages and compiling 2
 Using types 5
 Defining types 5
 Data structures and control structures 5
Views System 4
Half way
 Input and output with files 4
Debugging 3
 Collections 5
 Extensibility and polymorphism 5
 Extra topics 2
Microsoft Courses 2003
9
What to include?
 If a feature is covered, cover it completely, albeit over
time in a spiral fashion
 Include
–
–
–
–
–
–
–
images as data types - adds to the fun
GUIs - for realism
formatting and unicode - promotes internationalisation
serialization - makes for serious programs
exception handling - makes for robust programs
foreach loop - so neat and powerful
collections - enhance object-orientation
Microsoft Courses 2003
10
What to exclude?
 What to exclude depends on
– length of course
– interface with other courses
 A suggestion
–
–
–
–
threads - in Operating Systems
networking - for Netcentric Computing
graphics and delegates - to introduce non-Views GUIs
operator overloading, other upcoming features (e.g. generics), - in
Data Structures
 Notes:
– Topics that were in a Java introductory course might not be in a C#
version (applets)
– NOTE: some institutions will start with Netcentric Computing interesting approach
Microsoft Courses 2003
11
What resources do I need?
 Microsoft Academic Alliance, plus
 Option 1 (Student):
– a PC
– C# compiler
– Any simple editor
 Option 2 (Lecturer)
– a PC with lots of memory
– Visual Studio
 Option 3 (Researcher)
– PC or Mac, Windows or Linux
– Rotor
– Any simple editor
Microsoft Courses 2003
12
C# Concisely
 First year programming
text book, Oct 2003
 Pearson, 2004
 Incorporates Views
 Reviewed by Microsoft
 Contents on the Views
website
http://csharp.cs.uvic.ca
Microsoft Courses 2003
13
Volunteers on a C# course in Africa
Microsoft Courses 2003
Do it in C# Naturally!
14
From the ECMA C# Specification
8.7.4 Properties
A property is a member that provides access to a characteristic of an object or a class. Examples of
properties include the length of a string, the size of a font, the caption of a window, the name of a
customer,and so on. Properties are a natural extension of fields. Both are named members with
associated types, and the syntax for accessing fields and properties is the same. However, unlike
fields, properties do not denote storage locations. Instead, properties have accessors that specify the
statements to be executed when their values are read or written.
Properties are defined with property declarations. The first part of a property declaration looks quite
similar to a field declaration. The second part includes a get accessor and/or a set accessor. In the
example below, the Button class defines a Caption property.
public class Button {
private string caption;
public string Caption {
get {
return caption;
}
set {
caption = value;
Repaint();
}
}}
Microsoft Courses 2003
15
Syntax forms in C#C
Fixed words
and symbols
Items to
fill in
public string Course {
get {return course;}
}
Microsoft Courses 2003
16
Also for libraries
Microsoft Courses 2003
17
Visual Studio Help
Microsoft Courses 2003
18
Concepts for simple oops
Microsoft Courses 2003
19
Example sequence from early lessons
 Example 2.5 (page 45) - Meeting times
– Creates objects of type DateTime and accesses their properties
and methods
 Example 2.6 (page 47) - Dates in different formats
– Further example of DateTime methods, customising output
 Example 2.7 (page 49) - Time with reading
– Introduces input using Console and the Parse methods of a type
 Example 3.4 (page 83) - Table of meeting times
– Using a loop to create different times
 Examples 3.2 and 3.3 (page 75) - The shuttle bus
– Defining a type from scratch and using it in a program
Microsoft Courses 2003
20
GUIs
 Current approaches do not emphasise independent
principles
OPTIONS
 Create GUIs by hand
– error prone
– takes too much time
 Use a GUI builder
– dumps code in the program
– hides principles
Microsoft Courses 2003
21
Where GUIs are going
The reality of a
Views
single cross-language,
cross-platform
GUI interface programming model
is in sight, based on an
XML description language
supported by
fast native runtimes.
[Russel Jones, DevX, Nov 2002]
Microsoft Courses 2003
22
… and more recently
Supporting many GUIs
isn't just a simple process
of including one set of libraries or another;
it's often a frustrating and error-prone exercise
in writing GUI-specific code.
[Russel Jones, DevX, Aug 2003]
Microsoft Courses 2003
23
Rotor CLI Implementation
VS.NET
C#
JScript
System.WinForms
System.WinForms
System.Web
(ASP.NET)
System.Drawing
System.Data
(ADO.NET)
System.Xml
System
SDK Tools
Common Language Runtime
Platform Abstraction
Microsoft Courses 2003
24
Views
 Views is a Vendor Independent Extensible
Windowing System
 Developed by Nigel Horspool and Judith Bishop with
help from students in 2002-2003
 Provides an XML-based specification notation for
defining GUIs, and an execution engine for handling
event listening and dispatching back to the program
 It was supported under the Microsoft Rotor RFP
Program
 It is distributed from the C# Concisely book website
Microsoft Courses 2003
25
Microsoft Courses 2003
26
Example in WinForms
show.Click += new
EventHandler(ActionPerformed);
hide.Click += new
EventHandler(ActionPerformed);
}
public void ActionPerformed(Object src,
EventArgs args) {
if (src == show) {
pic.Show();
} else if (src == hide) {
pic.Hide();
}
}i

Embedded in 115 lines of generated code - “do not touch”

Unexplained classes and unused objects here
Microsoft Courses 2003
27
GUI building today
widget
rendering
in the OS
widget
calls in a
language
GUI Builder
Windows
Application
Add Listeners
Handlers
Visual Studio
C#
Microsoft Courses 2003
28
A GUI using XML
widget
rendering
in the OS
GUI
XML
Spec
Application
Handlers
Control
Engine
Add Listeners
Microsoft Courses 2003
29
XML
Example in Views
Views.Form f =
new Views.Form(@"<Form>
<vertical>
<horizontal>
<Button Name=Show/>
<Button Name=Hide/>
</horizontal>
<PictureBox Name=pic
Image='C:Jacarandas.jpg'
Height=175/>
</vertical>
</Form>" );
string c;
for (;;) {
c = f.GetControl();
PictureBox pb = f["pic"];
switch (c) {
case ”Show" : pb.Show();
break;
}
case ”Hide" : pb.Hide();
break;
}
}
}

Microsoft Courses 2003
C#
No pixel positioning

No generated code

Separation of concerns
30
Example 2
Views.Form v = new Form (@"<form Text= Lucky>
<vertical>
<TextBox name =Number Text = '13'/>
<Button name = Start/>
<ListBox name = Day Width = 270/>
</vertical>
</form>");
int luckyNumber =
int.Parse(v.GetText("Number"));
Random r = new Random (luckyNumber);
for( ; ; ) {
string s = v.GetControl( );
if (s==null) break;
DateTime luckyDate =
new DateTime(DateTime.Now.Year, r.Next(3,12);, r.Next(1,30););
v.PutText("Day", "Your lucky day will be " +
luckyDate.DayOfWeek + " " + luckyDate.ToString("M"));
}
Microsoft Courses 2003
31
Other Views examples
 Calculator
– Compare with text version
– Separation of concerns
– Internationalization
 PhotoAlbum
– Fun with pictures
Microsoft Courses 2003
32
Debugging
 Principles - types of errors:
– syntactic
– semantic
– runtime
 Exception handling
 Robust code
– simple logic
– validity checks - also with Assert
– tracing statements
 Debugger programs
– Text based, or
– GUI, with or without Visual Studio
Microsoft Courses 2003
33
Oops in C#
 Structs and classes
 Well defined collection library
–
–
–
–
Array class
Sorted lists
BitArray
Queue, Stack, Hashtable
 Polymorphism and extensibility
– Interfaces and inheritance
Microsoft Courses 2003
34
Example - Access control
 Page 318
 Students, staff, posgrads and tutors have different
rules for access to a building. The rules are
implemented at the start of each year.
 Polymorphic collection over IAccess
 Classic simple data update example
 Can be much extended e.g. for
– serialisation
– images
Microsoft Courses 2003
35
Assessment
 Quizzes
– available online on the website
 Exercises
– at the end of each chapter - answers will be provided to lecturers
 Practicals
– worksheets are being devised based on the book
 Exam questions
– samples will also be provided
Watch for the CD
Microsoft Courses 2003
36
References
 Peter Drayton, Ben Albahari, Ted Neward, C# in a Nutshell,
O’Reilly, 2002
 Troelsen, Andrew “C# and the .NET platform” A! press 2001
 Damien Watkins, Mark Hammond and Brad Abrams,
Programming in the .NET environment, Microsoft .NET
Development Series, Addison Wesley, 2002
 Not many text books yet, but many trade books
 Visual Studio help files
 DevHood tutorials -- see http://www.devhood.com
 http://www.cs.up.ac.za/rotor -- for the Views project
Microsoft Courses 2003
37
Motivation for a different approach
Forward looking
– Move to platform independent GUI systems
– Integration of XML into languages (cf XEN)
Technical
– Rotor does not have a GUI capability
– Interesting challenges in Reflection, RegEx etc
Educational
– Dissatisfaction with method-oriented or drag and drop GUIs
– Separation of concerns
Microsoft Courses 2003
38
The Views Notation
form:
controlGroup:
controlList:
textItemList:
control:
<form> controlGroup </form>
<vertical> controlList </vertical>
| <horizontal> controlList </horizontal>
{ control }
{ <item> text </item> }
controlGroup
| <Button/> | <CheckBox/>
| <CheckedListBox> textItemList </CheckedListBox>
| <DomainUpDown> textItemList </DomainUpDown>
| <GroupBox> radioButtonList </GroupBox>
| <Label/> | <ListBox/>
| <OpenFileDialog/> | <SaveFileDialog/>
| <PictureBox/> | <TextBox/>
| <ProgressBar/> | <TrackBar/>
radioButtonList: { <RadioButton/> }
Microsoft Courses 2003
39
Handler methods
Essentially five kinds of methods:
Form(string spec,params)
The constructor.
construct
void CloseGUI( )
close
Terminates the execution thread
getControl
string GetControl( )
Waits for the user to perform an action
get
string GetText(string name)
put
Returns the value of the Text attribute
PLUS … direct access
int GetValue(string name)
Returns the Value attribute from TrackBar, ProgressBar and CheckBox
int GetValue(string name, int index)
Returns the status of CheckBox at position index
void PutText(string name, string s)
Displays the string in a TextBox or ListBox control.
void PutValue(string name, int v)
Sets an integer value associated with a ProgressBar or CheckBox
Microsoft Courses 2003
40
Handler methods
Essentially five kinds of methods:
Form(string spec,params)
The constructor.
construct
void CloseGUI( )
close
Terminates the execution thread
getControl
string GetControl( )
Waits for the user to perform an action
get
string GetText(string name)
put
Returns the value of the Text attribute
PLUS … direct access
int GetValue(string name)
Returns the Value attribute from TrackBar, ProgressBar and CheckBox
int GetValue(string name, int index)
Returns the status of CheckBox at position index
void PutText(string name, string s)
Displays the string in a TextBox or ListBox control.
void PutValue(string name, int v)
Sets an integer value associated with a ProgressBar or CheckBox
Microsoft Courses 2003
41
Object orientation
 Extension, polymorphism
D1(P)
D2(Q)
D3(R)
 Delegation
F() -- calls M
I
M
F() -- calls M
F(D1) -- calls P
via M
A
D
M
A
A
C
B
M
M
M
Interfaces
Microsoft Courses 2003
B
M
C
M
B
P
Q
C
R
M
Inheritance
Delegates
42
GUI building today
widget
rendering
in the OS
widget
calls in a
language
GUI Builder
Windows
Application
Add Listeners
Handlers
Visual Studio
C#
Microsoft Courses 2003
43
Descargar

C# and .NET for Java Programmers