C# Intro Programming languages and programs: Source code and object code Editors and compilers C# fundamentals: Program structure Classes and Objects Variables and Types Methods Loops and Conditional statements FEN AK IT: Softwarekonstruktion
Software Development Virkeligheden ??? Modeller Teknologi: Hardware, Windows, netværk, servere, compilere www Programmer Løsninger Systemudvikling Programmering FEN AK IT: Softwarekonstruktion2 Vi fokuserer her
FEN AK IT: Softwarekonstruktion3 The Tasks of the Compiler Compiler Source ProgramObject Code Error messages Hello.cs class Hello { public static void Main( { System.Console.Write } Hello.exe
In C#: FEN AK IT: Softwarekonstruktion4 Source : a text file The compiler processes the source file An exe is output from the compiler
Run the program FEN AK IT: Softwarekonstruktion5 It is a normal application And the result!
Integrated Development Environment (IDE) This is troublesome! Normally one will use an IDE. An IDE integrates editor, file handling, compiler, execution and much more. We will use Visual Studio. FEN AK IT: Softwarekonstruktion6
FEN AK IT: Softwarekonstruktion7 New project Or in the File Menu
FEN AK IT: Softwarekonstruktion8 Choose C# Console Application Give Project a good name Pick a location For now Solution name and project name can be the same
FEN AK IT: Softwarekonstruktion9 Source goes here Projects and files are handled here Some standard libraries are included Keeps the output console open When “using”, we don’t need to specify the library (“System”)
Run the program FEN AK IT: Softwarekonstruktion10 Click the ”Run-button” …and the program runs! Press enter, and the program terminates and you are back in VS
Exercise Do it! (Exercise 1) FEN AK IT: Softwarekonstruktion11
The programming process The Aim – Well designed – Well written – Easy to maintain The Process – Write the software (implementation) – “Translate” the software (compilation) – Correct syntax errors – Test the software – Correct the logical (semantic) errors FEN AK IT: Softwarekonstruktion12
Syntax and semantics Syntax is about form. Semantics is about meaning. – The man drinks a cold beer. – The man drink a coldly beeer. – The beer drinks a cold man. FEN AK IT: Softwarekonstruktion13
C# All program logic must be embedded in (typically) a class. Every executable program must contain a Main- method. The Main-method is the starting point of the application. C# is case-sensitive No multiple inheritance (only between interfaces) All classes inherit object Garbage-collection C# supports operator and method overloading FEN AK IT: Softwarekonstruktion
The First C# Program FEN AK IT: Softwarekonstruktion15 We’ll need some libraries VS projects are embedded in a namespace All code must be embedded in classes Actual work is done here “Main”-method: the starting point of the application
FEN AK IT: Softwarekonstruktion16 Types and Variables Every piece of data (objects and values) is referred to by variables. Every variable must have a type (class (programmer- defined) or build-in). Build-in types are called primitive types and typically include: – Numbers: integers and decimals (int, double etc.) – Text: Characters and strings (char and string) – Logical values: Boolean (bool: true or false) Programmer-defined types are defined by classes.
FEN AK IT: Softwarekonstruktion17 Types and Variables Variables are declared Input/output is done in text format (string) Text must be converted to numbers before computing Output is formatted and printed Let’s try it in Visual Studio
FEN AK IT: Softwarekonstruktion18 Types and Variables Think of variables as cells in memory: – The variable name is the label of the cell. – The type of the variable is the size and shape of the cell determining what can be stored in the cell. – The value of the variable is the content of the cell – the actual data that the program works with. x y s This is a string
Exercise Do Exercise 2: SimpleCalculator. FEN AK IT: Softwarekonstruktion19
FEN AK IT: Softwarekonstruktion20 Types and Variables Every piece of data (objects and values) is referred to by variables. Every variable must have a type (class or build-in). public class BookMain { public static void Main() { Book b1= new Book("C#","Troelsen"); } b1 “C#“ “Troelsen” Type Object Variable The variable is a assigned a value = an object is created
A class public class Book { private string title; private string author; public Book(string t, string a) //Constructor { title= t; author= a; } public override string ToString(){ return (title+" "+author); } FEN AK IT: Softwarekonstruktion
Driver Program (Main) public class BookMain { public static void Main() { Book b1= new Book("C#","Troelsen"); Book b2= new Book("Java","Kölling"); System.Console.WriteLine(b1.ToString()); System.Console.WriteLine(b2); } At the moment, the driver acts as both frontend and database FEN AK IT: Softwarekonstruktion
Demo The Book example in VS. Let’s add a price attribute to the Book class and a method that increases the price with some given percentage. (We will also have to change the constructor and the ToString method accordingly.) FEN AK IT: Softwarekonstruktion23
Exercise Do Exercise 3: Person Class. FEN AK IT: Softwarekonstruktion24
FEN AK IT: Softwarekonstruktion25 C# - Namespaces and Using Namespaces are a tool for structuring programs and systems. Makes it possible to use the same names (identifiers) in different parts of an application. Namespaces may be nested. Visual Studio creates default a namespace with the same name as the project. using tells the compiler where to look for definitions that our program refers to.
FEN AK IT: Softwarekonstruktion26 C# - value- and reference-types Objects of value-type are stack allocated – objects of reference type are allocated on the heap. Value types die, when control goes out of the scope where they were declared – reference types are removed by the garbage collector (non-deterministic). Value types are copied with assignment – with reference types a reference (the address) to the object is copied.
FEN AK IT: Softwarekonstruktion27 C# - reference types - example creation, assignment and comparison: Customer c1, c2, c3; string s1, s2; c1 = new Customer("Flemming Sander", 36259); c2 = new Customer(”Bjarne Riis", 55298); c3 = null; // c3 refers to nothing c3 = c1; // c3 refers to the same object as c1 if (c1 == null)... // is c1 referring to something? if (c1 == c2)... // compare references if (c1.Equals(c2))... // compares object-values
FEN AK IT: Softwarekonstruktion28 C# - When are objects equal? Classes ought to override the Equals-method public class Customer {. public override bool Equals(object obj) { Customer other; if ((obj == null) || (!(obj is Customer))) return false; // surely not equal other = (Customer) obj; // explicit typecast return this.id == other.id; // equal if ids are... }
FEN AK IT: Softwarekonstruktion29 C# - Boxing and Unboxing C# converts automatically between simple values and objects – value => object = "boxing“ (the value is “wrapped in a box”) – object => value = "un boxing“ (the value is unwrapped again) int i, j; object obj; string s; i = 32; obj = i; // boxing (copy) i = 19; j = (int) obj; // unboxing! s = j.ToString(); // boxing! s = 99.ToString(); // boxing!
FEN AK IT: Softwarekonstruktion30 C# - arrays Arrays are reference types – Created from the Array-class in FCL – Created using the new-operator – 0-based indexing – Are initialised with default value (0 if numeric, null if reference) int[] a; a = new int[5]; a[0] = 17; a[1] = 32; int x = a[0] + a[1] + a[4]; int l = a.Length; Access element 1 Creation Number of elements
FEN AK IT: Softwarekonstruktion31 C# - selection and iteration x = obj.foo(); if (x > 0 && x < 10) count++; else if (x == -1)... else {... } while (x > 0) {... x--; } for (int k = 0; k < 10; k++) {... }
FEN AK IT: Softwarekonstruktion32 C# - foreach-loop foreach loop is used to sweep over collections such as arrays – Reduces the risk of indexing errors int[] data = { 1, 2, 3, 4, 5 }; int sum = 0; foreach (int x in data) { sum += x; } foreach type value collection
FEN AK IT: Softwarekonstruktion33 C# - Methods A class may have two kind of methods: – Instance methods – Static methods (class methods) – Instance methods need an object to be invoked – Static methods are called using the class name only
FEN AK IT: Softwarekonstruktion34 C# - Example The array-class in BCL (FCL) – The class is a member of namespace System (System.Array) namespace System { public class Array { public int GetLength(int dimension) {... } public static void Sort(Array a) {... }. } instance method static method
FEN AK IT: Softwarekonstruktion35 C# - calling the methods /* main.cs */ using System; public class App { public static void Main() { int[] data = { 11, 7, 38, 55, 3 }; Array.Sort(data); for (int i=0; i<data.GetLength(0); i++) Console.WriteLine(i + ": " + data[i]); } Class-method Instance-method
Exercise Do exercise 4: Array Manipulation FEN AK IT: Softwarekonstruktion36