Presentation is loading. Please wait.

Presentation is loading. Please wait.

C# Intro Programming languages and programs: Source code and object code Editors and compilers C# fundamentals: Program structure Classes and Objects Variables.

Similar presentations


Presentation on theme: "C# Intro Programming languages and programs: Source code and object code Editors and compilers C# fundamentals: Program structure Classes and Objects Variables."— Presentation transcript:

1 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 2013-01-171AK IT: Softwarekonstruktion

2 Software Development Virkeligheden ??? Modeller Teknologi: Hardware, Windows, netværk, servere, compilere www Programmer ----- ------- ---- ------ ----- ------- ---- ------ ----- ------- ---- ------ Løsninger Systemudvikling Programmering FEN 2013-01-17AK IT: Softwarekonstruktion2 Vi fokuserer her

3 FEN 2013-01-17AK 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 00110101 11010111 10011010 10011101

4 In C#: FEN 2013-01-17AK IT: Softwarekonstruktion4 Source : a text file The compiler processes the source file An exe is output from the compiler

5 Run the program FEN 2013-01-17AK IT: Softwarekonstruktion5 It is a normal application And the result!

6 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 2013-01-17AK IT: Softwarekonstruktion6

7 FEN 2013-01-17AK IT: Softwarekonstruktion7 New project Or in the File Menu

8 FEN 2013-01-17AK 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

9 FEN 2013-01-17AK 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”)

10 Run the program FEN 2013-01-17AK IT: Softwarekonstruktion10 Click the ”Run-button” …and the program runs! Press enter, and the program terminates and you are back in VS

11 Exercise Do it! (Exercise 1) FEN 2013-01-17AK IT: Softwarekonstruktion11

12 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 2013-01-17AK IT: Softwarekonstruktion12

13 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 2013-01-17AK IT: Softwarekonstruktion13

14 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 2013-01-1714AK IT: Softwarekonstruktion

15 The First C# Program FEN 2013-01-17AK 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

16 FEN 2013-01-17AK 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.

17 FEN 2013-01-17AK 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

18 FEN 2013-01-17AK 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 0 5.213 This is a string

19 Exercise Do Exercise 2: SimpleCalculator. FEN 2013-01-17AK IT: Softwarekonstruktion19

20 FEN 2013-01-17AK 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

21 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 2013-01-1721AK IT: Softwarekonstruktion

22 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 2013-01-1722AK IT: Softwarekonstruktion

23 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 2013-01-17AK IT: Softwarekonstruktion23

24 Exercise Do Exercise 3: Person Class. FEN 2013-01-17AK IT: Softwarekonstruktion24

25 FEN 2013-01-17AK 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.

26 FEN 2013-01-17AK 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.

27 FEN 2013-01-17AK 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

28 FEN 2013-01-17AK 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... }

29 FEN 2013-01-17AK 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!

30 FEN 2013-01-17AK 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

31 FEN 2013-01-17AK 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++) {... }

32 FEN 2013-01-17AK 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

33 FEN 2013-01-17AK 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

34 FEN 2013-01-17AK 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

35 FEN 2013-01-17AK 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

36 Exercise Do exercise 4: Array Manipulation FEN 2013-01-17AK IT: Softwarekonstruktion36


Download ppt "C# Intro Programming languages and programs: Source code and object code Editors and compilers C# fundamentals: Program structure Classes and Objects Variables."

Similar presentations


Ads by Google