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

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Introduction to ASP.NET.
C Language.
Intermediate Code Generation
Language Fundamentals in brief C# - Introduction.
Computer and Programming
Written by: Dr. JJ Shepherd
C#: Data Types Based on slides by Joe Hummel. 2 UCN Technology: Computer Science Content: “.NET is designed around the CTS, or Common Type System.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
3. Data Types. 2 Microsoft Objectives “.NET is designed around the CTS, or Common Type System. The CTS is what allows assemblies, written in different.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Guide To UNIX Using Linux Third Edition
4. Statements and Methods. 2 Microsoft Objectives “With regards to programming statements and methods, C# offers what you would come to expect from a.
From C++ to C#. Web programming The course is on web programming using ASP.Net and C# The course is on web programming using ASP.Net and C# ASP.Net is.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
OOP Languages: Java vs C++
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
C#: Statements and Methods Based on slides by Joe Hummel.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
FEN 2012 UCN Technology: Computer Science1 C# - Introduction Language Fundamentals in Brief.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Spring 2007NOEA: Computer Science Programme 1 C# - Introduction Language Fundamentals: Data Types string Objects and Classes Methods Iteration and Selection.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Java Classes. Consider this simplistic class public class ProjInfo {ProjInfo() {System.out.println("This program computes factorial of number"); System.out.println("passed.
Applications Development
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 4 Introduction to Classes, Objects, Methods and strings
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Section 3 - Arrays and Methods. Arrays Array: collection of group of data variables of same type, sharing the same name for convenience - Easy to search.
Object Oriented Software Development 4. C# data types, objects and references.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Session 1 C# Basics.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
11 Introduction to Object Oriented Programming (Continued) Cats.
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
1 Introduction to Object Oriented Programming Chapter 10.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 1: Introduction to Computers and Programming.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
The need for Programming Languages
Creating and Using Objects, Exceptions, Strings
Classes and Objects.
Java for Android is specific
Objectives Identify the built-in data types in C++
CS360 Windows Programming
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Advanced Programming Lecture 02: Introduction to C# Apps
Java Programming Language
Review for Midterm 3.
Presentation transcript:

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