1 / 49

Teaching with C Sharp

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.

cheryr
Download Presentation

Teaching with C Sharp

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Teaching with C Sharp Rob Miles

  2. 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?

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. demo • Simple Input/Output • Text Read and Write and colour manipulation • Console Power • Moving dot game

  15. 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

  16. 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

  17. 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

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

  19. 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); }

  20. Boxing Assembler

  21. 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

  22. 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

  23. demo • Structures in C# • Simple structures • Structure “housekeeping” • Arrays of structures

  24. 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

  25. demo • Exceptions in C# • Simple exception handling • Creating your own exceptions

  26. 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

  27. demo • Reference Parameters • ref parameters • in parameters

  28. 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

  29. demo • Event handling with delegates • Simple form creation and event management • Using the Visual Studio 2005 Forms Designer

  30. 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

  31. 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

  32. 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

  33. 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

  34. demo • Using Business Objects • Single workspace containing a number of Visual Studio Projects

  35. 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

  36. 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

  37. 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

  38. demo • Using Business Objects and Namespaces • Advanced multi-project workspace with business and control objects

  39. 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”

  40. demo • Simple Properties • A book title class which uses properties to manage the name of the book

  41. Properties and Assembler

  42. 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

  43. 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

  44. 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

  45. demo • Using the List collection class • A typesafe list collection is created using the new System.Generics.List collection class

  46. 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

  47. demo • Creating and using a generic linked list class • A linked list generic which is deployed for both integers and string types

  48. 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……

  49. Questions • I can take any quick questions now • For more detailed ones please catch me in the break or during the labs

More Related