530 likes | 559 Views
Teaching with C Sharp. Rob Miles. 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?. Introduction.
E N D
Teaching with C Sharp Rob Miles
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?
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
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
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
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
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
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
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
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
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
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
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
demo • Simple Input/Output • Text Read and Write and colour manipulation • Console Power • Moving dot game
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
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
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
demo • Value type behaviour in .NET 1.1 and .NET 2.0 • Using ildasm to look at the IL
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); }
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
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
demo • Structures in C# • Simple structures • Structure “housekeeping” • Arrays of structures
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
demo • Exceptions in C# • Simple exception handling • Creating your own exceptions
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
demo • Reference Parameters • ref parameters • in parameters
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
demo • Event handling with delegates • Simple form creation and event management • Using the Visual Studio 2005 Forms Designer
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
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
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
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
demo • Using Business Objects • Single workspace containing a number of Visual Studio Projects
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
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
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
demo • Using Business Objects and Namespaces • Advanced multi-project workspace with business and control objects
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”
demo • Simple Properties • A book title class which uses properties to manage the name of the book
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
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
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
demo • Using the List collection class • A typesafe list collection is created using the new System.Generics.List collection class
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
demo • Creating and using a generic linked list class • A linked list generic which is deployed for both integers and string types
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……
Questions • I can take any quick questions now • For more detailed ones please catch me in the break or during the labs