660 likes | 915 Views
Session 2. C# February 6th - 2007. This week’s info. Exercises will start next week due to the time of day. Use class breaks, get a hold of me after class or email. Project will be given next week. All students who are not enrolled can do so this week. C#.
E N D
Session 2 C# • February 6th - 2007
This week’s info • Exercises will start next week due to the time of day. • Use class breaks, get a hold of me after class or email. • Project will be given next week. • All students who are not enrolled can do so this week.
C# • C# is a language created by Anders Hejlsberg to provide a vehicle for writing enterprise applications in .NET. • C# resembles Java a lot, but provides innovations in type safety, versioning, events and garbage collection. • C# is an ECMA standard (as opposed to Java). (http://www.ecma-international.org/ and http://en.wikipedia.org/wiki/Ecma) • The smarts of .NET is not in a particular language, but in the libraries. • Remember: The role of the compiler is to make life easy, not difficult. • There is a new standard (C# version 2.0) that is in the 2005 (Whidbey) packages. • Version 3.0 is coming soon.
General structure • C# programs consist of one or more files. • Each file can contain code from one or more namespaces. • A namespace can contain types such as classes, structs, interfaces, enumerations, and delegates, in addition to other namespaces. • There is no order enforced by the system (like there is in e.g. C++). // A skeleton of a C# program using System; namespace MyNamespace1 { class MyClass1 { } struct MyStruct { } interface IMyInterface { } delegate int MyDelegate(); enum MyEnum { } namespace MyNamespace2 { } class MyClass2 { public static void Main(string[] args) { } } }
Classes. • A Class is a definition of an object. • A Class encapsulates a state and behaviour. • Powerful abstraction concept. Models realworld concepts. • Objects are instances of Classes. • Classes in .NET are quite similar to classes in other OO languages. • Classes are Types.
Class state • A object defines its state at a given time through the values of the object's member variables. • The state is modified through the operations (behaviour) provided by the object. • Model some real life objects...
UML • As a profession, we need a language to express designs and ideas. • Compare with any other profession: Mathematical:
UML • There are several methodogies available, but the one most commonly used is Unified Modelling Language, or UML. • UML was invented by the OMG (Object Management Group). • http://en.wikipedia.org/wiki/Unified_Modeling_Language • Programming language agnostic. • We will be discussing: • Class diagrams. • Sequence diagrams. • Activity diagrams.
Class Diagrams • Class diagrams are the most commonly used UML diagrams. • A class diagram defines classes and the relationships that hold between them. • Class diagrams show operations and fields for a given class. • We will be using class diagrams to introduce some class concepts used in OO in general and in .NET in particular.
UML Class diagrams: A class The Class name Properties Methods
Mother Father Child1 Child2 Child3 Inheritance: Human example
Inheritance: Human example • Uncontrollable results • More properties or less properties • Can be for the better or for worse • Properties can clash
Inheritance: Transitive • Inheritance is a transitive relation. • Let C be derived from B and B be derived from A. B inherits from A and C inherits from B (and A). • Derived classes can add elements, and it can override elements, but not remove the elements from the base class. • ALL types are derived from a common root class, the System.Object class. • Note that ALL types are objects in C#, not only reference types.
UML class diagrams: Inheritance Specialization
Abstract Classes • Abstract classes cannot be instantiated directly. They contain abstract methods that must be implemented by derived classes.
Classes: Visibility of class members • Class operations work on the internal state. It is always convenient to hide away the details of implementation to the class' clients. • To this end, members may be internal, private, protected or public (and sealed in c#). • Prefix the member by • - for private members • # for protected members • + for public members. • Use lowercase for member names. • Members may be static or not static (instance). • Static member names are underlined.
UML class diagrams: Associations & Aggregations • Use unfilled diamond if the target leads a life of its own. • Use filled diamond if the class pointed to does not contains a life of its own.
UML Class diagrams: Comments • You should annotate your diagram by using comments. • Comments should enhance legibility and aid comprehension. • Use dashed line if you want to tie the comment to somewhere specific.
UML Object diagrams • Class diagrams deal with the structure of data. • Object diagrams describe instances of data (snapshots). • Left: The classes and relationships involving orders and orderlines. • Right: A order with two order lines created on the 1. january 2003.
Exercise: Buildings • Construct a class “building” with basic properties and methods. • Construct at 2 branches “skyscraper” and “mobile home” with additional properties and methods. This class inherits from “building” • Construct 2 classes “door” and “window” (with some properties), that have a one-to-many relation to the “mobile home”
Using part using System; using System.Collections; using System.Collections.Generic; using System.Text; using Session2_lib;//custom lib.
Namespace part • Namespaces are a way to define the classes into one hierarchical structure. • Has nothing to do with the execution • Strictly for organizing and branding code • You can have as may namespaces as you like. • Namespaces are meant to make coding simpler! • You can NOT use the name “System” as a custom namespace! • Namespace Customer • Namespace Customer.Address
Class part • Classes define a type, of object. • Classes are the core of C# and object-oriented programming. • Classes should encapsulate your methods in groups of functionality. • Classes are declared within a namespace public class Employee { public void DoWork() { } }
Partial class public partial class Employee { public void DoWork() { } } public partial class Employee { public void GoToLunch() { } }
Method(s) part • A method is a code block containing a series of statements • In C#, every executed instruction is done so in the context of a method. • Specifying the access level, the return value, the name of the method, and any method parameters. • Method parameters are surrounded by parentheses, and separated by commas. • Empty parentheses indicate that the method requires no parameters. • Methods are declared within a class
Method public class Motorcycle { public void StartEngine() { } public void AddGas(int gallons) { } public int Drive(int miles, int speed) { return 0; } }
.NET building blocks • Programming is like building houses • The programmer is the architect (or a specific person in the project is given that role) • C#’s name for building blocks is “assemblies” • Microsoft has developed a huge amount of “blocks” for you to use (and ease your programming)
Techniques and skills Good programming …
Techniques and skills BAD programming …
.NET Assemblies • The output of the compilation process is an assembly or an executable file. • Any number of source texts go into building an assembly. • An assembly can contain any number of classes from any number of namespaces. • Assemblies typically refer to other assemblies. • At runtime, all the assemblies are stored together and deployed.
Namespaces • Namespaces provide a naming schemes for grouping related types. • Helps developers focus and browse and reference types. • Convenience as programs and libraries get larger. • Used to resolve naming conflicts. • Types are included in a namespace when it appears within a namespace block. • Namespaces may be defined in any number of source files. • Types may be referred to by using their full (qualified) name or implicitly by having using clauses (using unqualified names).
Assemblies. • Assemblies are the building blocks of .NET applications. • Assemblies are: • Reusable • Versionable • Secure • Self describing. • Assemblies consist of two parts: • Types and resources used to provide a logical unit of functionality. • Metadata that describes how these elements relate and what they depend on. • The metadata that describes the assembly is called the assembly manifest.
Namespaces and Assemblies • There is no relationship between namespaces and assemblies. • An assembly can contain types from any number of namespaces. • Classes from the same namespace may be located in different assemblies. • However, members may be marked internal causing access from classes in the same assembly.
To sum it all up – Methods vs classes Class StartEngine() Drive() Break() StopEngine()
To sum it all up – Classes vs assemblies Assembly CarMotionControl LightsControl() DoorManagement() StartEngine() Drive() Break()
To sum it all up – Assemblies vs Project .Exe or Assembly Project Car Assembly Road Assembly Gasoline Assembly CarMotion Lights Doors
Compiling C# programs • There are several choices, depending on the environment you have chosen to work in. • Command line: csc HelloWorld.cs. • Use your favourite IDE. • The IDEs are normally just wrappers around the compiler. • The compiler reads ALL the files and generates ONE assembly from them. There are no ”object files” and no ”linking phase”.
Using VS.NET • Source texts are arranged in projects. • A solution may contain one or more projects. • There are several useful project types. • Lots of code is generated for the user. • Help is always near...
Short Demo of VS.NET • Locations of things • Create project • Add project to a solution • Make a reference
C# Compilation • Demo. Using the command line compiler. • Using the VS environment • Using Reflector (http://www.aisto.com/roeder/dotnet/) to inspect the generated assembly.
.NET Types. • Everything is derived from System.Object (aka object). • There are two types of type: Reference types (classes) and Value types (structs, ints, etc). • Instances of reference types live on the heap and are subject to garbage collection. • Instances of value types (deriving from System.ValueType), on the other hand, live on the heap and are more lightwieght objects. • There are limitations to value types (no inheritance)
.NET Data types. • The .NET environment offers the boolean type and 3 numeric types (integral types, floating point types and decimal type). • bool values may assume the values true and false. • Integral types (stored in 2s complement):
.NET Floating point types • Floating point Literals: • Float: 3.14159f • Double: 3.14159265358979323d • Decimal: 7.55m