Teaching with C Sharp Rob Miles. Overview Introduction >My perspective C# and Java >Broad overview...

49
Teaching with C Sharp Rob Miles

Transcript of Teaching with C Sharp Rob Miles. Overview Introduction >My perspective C# and Java >Broad overview...

Page 1: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Teaching with C Sharp

Rob Miles

Page 2: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Overview

Introduction> My perspective

C# and Java> Broad overview of the languages

Significant C# Features> C# features from a teaching perspective

Conclusion> ..and possibly a sing song?

Page 3: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Introduction

The material is based on my experience teaching C# to two student cohorts

It is necessarily coloured by my teaching philosophy

It has been presented in targeted sections so that you can use those components which suit your purpose

There are number of sample projects for each section which form the basis of the lab work

Page 4: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Presentation Notes

A full set of notes is provided for this presentation

The notes are broken down in the same sequence as the items given here

Each section is followed by a number of practical examples along with some ideas for investigation

Page 5: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Bonus Materials

As this is the “collectors edition” of the course> Just like every other edition…

It is supplied with bonus materials in the form of a copy of “C# from Java”

This provides an FAQ for a number of conversion issues

Page 6: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Teaching Philosophy

At Hull we teach an “algorithms first” course This concentrates on program construction in

semester one and introduces objects in semester two

Everything is placed in the strongest possible “real world” context and I make heavy use of analogies

Page 7: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Objects

Objects are presented as a solution to structure and design

I try to make a logical transition from structures to components:

> Encapsulation> Business Objects> Test driven development> Component based design> Hierarchies for code reuse

Page 8: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Teaching and C#

I have found that C# matches with our teaching philosophy

It would also match with an objects first approach, at least as well as Java does

The points made later are applicable to both teaching approaches

Page 9: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

C# and Java

C# is heavily based on Java Conversion from C# to Java (or back) is not

difficult C# programs seem to run faster on the PC

platform C# is not being positioned as Microsoft

proprietary> But the language development is Microsoft driven, in the

same way that Java development is Sun driven

Page 10: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

C# Versions

There are now two versions of C# Version 2 adds a number of features:

> Partial classes> Generics

There are few breaking changes moving from C# Version 1 to Version 2

This presentation is Version 1 compliant

Page 11: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Development Tools

A command line compiler kit is available> Directly analogous to the JDK

Visual Studio 2005 is available under the Academic Alliance

Visual Studio 2005 Express Edition is available for free

> David will give more details of these later

Page 12: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

C# Language Features

We can consider some specific C# features:> User input/Output> Value Types and Structures> Exception Handling> Reference Parameters> Event Handling/Delegates> Program Linking and Deployment> Properties> Generics

They will be placed into a teaching context

Page 13: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

User input/output

The System.Console object provides a range of input/output actions

There is no need to create a stream object There is also no need to trap exceptions

Page 14: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

demodemo

Simple Input/Output> Text Read and Write and colour manipulation

Console Power> Moving dot game

Page 15: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Value Types

In Java a type is either primitive or reference: Primitive

> Managed by value> Primitive types do not support methods or properties

Reference> Managed by reference> Support methods and properties> Slower to manipulate

Page 16: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Java Primitive Problems

For a student learning to program the primitive-reference distinction is hard to grasp

It also brings a need to describe the use of wrapper classes

> Students do not like to have to learn the difference between

int and Integer This knowledge is not that relevant to other

languages

Page 17: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

C# value types

C# uses a different approach:> All types are objects> Some types are “value” types, i.e. manipulated by value not

reference> But they all support methods and properties> The “box” operation performs conversion from value to

reference as required

The C# student need not be aware of any distinction at the start of the course

Page 18: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

demodemo

Value type behaviour in .NET 1.1 and .NET 2.0> Using ildasm to look at the IL

Page 19: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Boxing in Action

static void Main(string[] args){ int a, b; a = 99; b = a; b = 100; Console.WriteLine("a is {0} b is {1}", a, b);

}

Page 20: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Boxing Assembler

Page 21: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Structures as value types

C# supports a struct type This type is directly analogous to the struct in

C A struct is manipulated as a value type This allows data to be grouped together

without a need to cover objects and references

It also allows for more complex exercises

Page 22: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

C# Structs

A struct in C# is managed by value. However, they do behave a lot like objects

> They can support methods and properties> They can have constructors> They can even implement an interface

But they do not support inheritance

Page 23: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

demodemo

Structures in C#> Simple structures

> Structure “housekeeping”

> Arrays of structures

Page 24: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Exception Handling

In Java you have to handle some exceptions (the checked ones)

> These are thrown by lots of library methods, including input/output

In C# all exceptions are unchecked> If you don’t catch them the run time system will pick them up> This makes things like simple input/output much simpler

Page 25: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

demodemo

Exceptions in C#> Simple exception handling

> Creating your own exceptions

Page 26: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Reference Parameters

Unlike Java, where parameters are always passed by value, in C# reference parameters are allowed

This makes programming more efficient It also allows discussion of the meaning of

references without the need to consider objects

Page 27: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

demodemo

Reference Parameters> ref parameters

> in parameters

Page 28: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Event handling and delegates

C# provides a delegate type which can be used to create a typesafe reference to a method in an instance of a class

There is no need to create an instance of a class to receive events

Delegates are used to manage system events and the user interface

Page 29: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

demodemo

Event handling with delegates> Simple form creation and event management

> Using the Visual Studio 2005 Forms Designer

Page 30: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Delegates and Design

Delegates bring function pointers back into programming

However, they can be deployed in a typesafe manner in a properly managed environment

However, he biggest advantage over Java is that they make interacting with Forms components much easier

Page 31: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Program Linking and Deployment

In Java a program is built at run time> The classpath is used to locate the required components as

the program runs> There is little attention paid at build time to linking issues

In C# the .exe file is an assembly which may make use of .dll library files

> There is a distinct difference between an executable file and a library one

> An exe file will refer to a specific version of a dll when it is built

Page 32: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

C# Programs

Only one class in a C# program may contain a Main method to provide the entry point

During the build process the particular libraries to be used must be specifically identified and located

> If you are using Visual Studio the metadata from the assembly can be used in interactive help

Page 33: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

C# Linking and Teaching

I place great emphasis on Business Objects and Test Driven Development

The C# linking process fits well with this approach

The students are initially introduced to the process by means of compiler directives

Later they move on to use Visual Studio 2005

Page 34: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

demodemo

Using Business Objects> Single workspace containing a number of Visual Studio Projects

Page 35: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

The naming of components

Java provides a package mechanism by which a system can be broken down into components

> this is linked to the physical position of the file in a directory hierarchy

> only one public class can placed in each file, and the filename must match the class name

C# does not do this> A class is placed in a “namespace” which is not physically

mapped to storage

Page 36: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

C# Namespace

Namespaces are hierarchical A single source file can contain components

from a number of namespaces A namespace can span multiple source files Visual Studio 2005 provides a view of all the

components in a namespace

Page 37: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Partial Classes

These are available in C# Version 2.0 A class can be made to span a number of

source files This allows the developer to partition the

behaviours of a class into a number of separate source files

> Hide the scary bits?

Visual Studio 2005 does this with forms

Page 38: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

demodemo

Using Business Objects and Namespaces> Advanced multi-project workspace with business and control objects

Page 39: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Properties

Properties are “syntactic sugar” They provide a convenient wrapper for get

and set behaviour in objects There is no need to use them

> But all of the system library classes do

They are best explained in the context of “making life easier”

Page 40: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

demodemo

Simple Properties> A book title class which uses properties to manage the name of the book

Page 41: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Properties and Assembler

Page 42: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Generics

Generics are new in Version 2.0 They let you create classes which work on

classes You don’t have to give the types of the

classes when you write the code Instead you use placeholders which are “filled

in” at runtime in a typesafe manner

Page 43: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Generics and teaching

I would not teach about generics in the first part of a programming course

The fact that they are now present in C# means that we can use this language in later years

Page 44: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Generic Collections

The major advantage that generics brings for learner programmers is a better way to deploy collection classes

They remove the need to cast items read from the collection

> Although I must admit I think that covering casting at this point is a good idea

Page 45: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

demodemo

Using the List collection class> A typesafe list collection is created using the new System.Generics.List

collection class

Page 46: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Using Generics

Generics do make it possible to talk about data structures without dwelling on the type of the data being manipulated

This can be rolled into discussions about generalisation which can take place in the latter parts of a first year course

Page 47: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

demodemo

Creating and using a generic linked list class> A linked list generic which is deployed for both integers and string types

Page 48: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Conclusion

Students like C#> We have seen improved performance in both coursework

and examinations

It is a more productive programming environment than Java

> All of the language changes are soundly pragmatic> The toolset is very effective and now handles important

issues like test driven development

C# will do very nicely. For now……

Page 49: Teaching with C Sharp Rob Miles. Overview  Introduction >My perspective  C# and Java >Broad overview of the languages  Significant C# Features >C#

Questions

I can take any quick questions now For more detailed ones please catch me in

the break or during the labs