TDDD49 Programmering C# Och .NET Lecture 1 - 2014
Total Page:16
File Type:pdf, Size:1020Kb
TDDD49 Programmering C# och .NET Lecture 1 - 2014 Johannes Schmidt, Department of Computer and Information Science (IDA), Linköping University In this lecture: ● Organisation ● Introduction to C# and .NET ● Basic syntax and constructs, OOP Staff Examiner and Course leader: Johannes Schmidt Assistants: Asmae Bni, Emil Olofsson, Jesper Tingvall, Naga Venkata Tallapragda Other teachers: Erik Berglund Course Secretary: Helene Meisinger Director of studies: Jalal Maleki Questions Who wants a break (15 minutes)? Who knows C, C++ or Java? Who does not? Purpose ● tillämpa och reflektera över C#s språkkonstruktioner och deras semantik, t.ex. klasser, delegater, event, nätverk för att bygga objektorienterade program. ● tillämpa och reflektera över olika centrala delar av .NET Framework t.ex. ASP.NET, ADO.NET, Windows Presentation Foundation (WPF) för att bygga applikationer ● tillämpa och reflektera över utvecklingsmiljö som Visual Studio. Organisation 4 Lectures 4 Labassignments (connected) 13 assisted lab sessions scheduled 107 hours of work => you work a lot on your own Examination elements: LAB1 4 poäng - Laborationer (U,G) Course page http://www.ida.liu.se/~TDDD49/ Lab Organisation Work in pairs or individually. Location: PC1, PC2, PC3 Register in Webreg: https://www.ida.liu.se/webreg/TDDD49-2014/LAB1 https://www.ida.liu.se/webreg/725G66-2014/LAB1 Registration Deadline: Nov. 12th Deadline for Lab demonstrations: Dec. 19th th Nov 20 : only PC1 and PC2 Lab assignments Subject: Board games! (Monopoly, Mastermind, Chess, Poker, …) Tic-tac-toe: too small Lab1: Develop the GUI Lab2: Game Engine (rules, logik) Lab3: Artificiel intelligence Lab4: Synchronized storage (use LINQ). (See course page for more details http://www.ida.liu.se/~TDDD49/) Do the labs stepwise or all in one. Important: use one of the IDEs Visual Studio, Xamarin, MonoDevelop. Submission: via box (invitations send out soon). Lab responsible: Erik Berglund. Lectures - Outline Lecture 1: Intro to C# and .NET, basic syntax and constructs, OOP Lecture 2-4: more OOP, GUIs, Generics, Events, Delegates, LINQ C# - origins Programming languages preceding (and influencing) C#: C 1972 C++ 1983 Java 1995 C# 2000 // syntax very similar to C, C++, Java. Ada 1980 // looks different. Compilation vs Interpretation C and C++ are compiled directly to machine code (unmanaged code). Java and C# are compiled to an intermediate code (managed code). For excecution a special Runtime is needed. Compiler C/C++ code Machine code Compiler Runtime Java/C# code Intermediate code Machine code Interpretation: Java vs C# Compiler Java code Java Byte Code Java Runtime Machine code Environment Common Compiler Common Language C# code Intermediate Runtime Machine code Language CIL CLR or VES (Virtual Execution System) Note: Specification and Runtime for CIL are called CLI = Common Language Infrastructure What is .NET? Predominant implementation of the Common Language Infrastructure From: Microsoft (MS) Target Platforms: MS Windows Other implementations exist*), e.g.: MS Silverlight (Mac and Windows) MS Compact Framework (PDAs and phones) MS XNA (Xbox, Windows Vista) Mono Project (Windows, Linux and Unix) DotGNU (.NET and DotGNU.Portable.NET) Rotor (Windows, Mac OS X and FreeBSD) *) Source: Essential C# 5.0 by Mark Michaelis What provides a CLI implementation such as .NET? ● Garbage collection ● Type safty (based on Common Type System CTS) ● Code Access Security *) ● Base Class Library (BCL) and platformspecific libraries ● Language interoperability (C#, Visual Basic, F#, …) ● Platform portability (theoretically...) *) for instance Framework Class Library FCL (.NET) .NET Hello World! C Java #include <stdio.h> class HelloWorld { int main(void) public static void main(String[] args) { { System.out.println("Hello world!"); printf("Hello world!"); } } } C++ C# #include <iostream.h> using System; int main(void) class HelloWorld { { std::cout << "Hello world!"; static void Main() } { Console.WriteLine("Hello world!"); } Ada } with Ada.Text_IO; use Ada.Text_IO; procedure Hello is begin Put_Line ("Hello world!"); end Hello; Predefined types Type Size Range BCL Name Signed Literal Suffix sbyte 8 bits -128 to 127 System.SByte Yes byte 8 bits 0 to 255 System.Byte No short 16 bits -32,768 to 32,767 System.Int16 Yes ushort 16 bits 0 to 65,535 System.UInt16 No int 32 bits -2,147,483,648 to System.Int32 Yes 2,147,483,647 uint 32 bits 0 to 4,294,967,295 System.UInt32 No U or u long 64 bits -9.2e18 to 9.2e18 System.Int64 Yes L or l ulong 64 bits 18.4e18 System.UInt64 No UL or ul float 32 bits +-1.5e-45 to +-3.4e38 System.Single Yes F or f double 64 bits +-5.0e-324 to +-1.7e308 System.Double Yes D or d decimal 128 bits +-1.0e-28 to +-7.9e28 System.Decimal Yes M or m char 16 bits 65536 characters System.Char bool 8 bits true or false System.Boolean All these types are value types Value types vs Reference types Value types: Reference types: A variable contains the data A variable contains a reference to directly. the data. Examples: Examples: all primitive types from last slide strings, arrays, objects and structs decimal x = 8; string a = "abc"; decimal y = 9; string b = "abracadabra"; x = y; // 16 bytes of data are copied a = b; // only a reference is copied // (typically 4-8 bytes) Declarations int myInteger; myInteger = 1; int yourTnteger = 0; char x; x = '\u0020'; // blank char y = 'c'; float height1; height1 = 1.443F; float height2 = float.Parse("1.222"); string s; s = "def"; string t = "abc"; int[] values; values = new int[] { 3, 2, 7, 18 }; int[] more = { 4, 5, 6 }; int[] evenmore = new int[100]; int count = more.Length; // will be 3 Conditions if (more.Length == 14) { if (<boolean>) { evenmore[0] += 22; ... } } if (more.Length < 10) { evenmore[0] += 22; } else { evenmore[2] = -17; } if (more.Length < 10) { evenmore[0] += 22; } else if(more.Length > 102) { evenmore[1] = 8; } else { System.Console.WriteLine("why?"); } Loops do { a = int.Parse(System.Console.ReadLine()); } while (a != 0); while (a < 10) { System.Console.WriteLine("Here is your integer: {0}\n", a); a++; } for (a = 0; a < 10; a++) { System.Console.WriteLine("Counting...\n"); } foreach(int x in values){ if(x > currentMax){ currentMax = x; } } Operators Operator Meaning && Logical AND || Logical OR ! Logical NOT +, -, *, / Arithmetic operators % Rest of integer division (Remainder) <, <=, ==, !=, >=, > Comparing operators ++x, x++, --i, i-- Pre- and post-incrementing / decrementing Operator Meaning |, &, ^ Binary OR, AND, XOR << Binary leftshift >> Binary rightshift Methods (functions) public int computeMax(int[] values) { int currentMax = values[0]; foreach(int x in values){ if(x > currentMax){ currentMax = x; } } return currentMax; } Objects Everything in C# is in a class. Nothing exists without a class. A class is a template for an object. An object has fields (data) and methods (functions). Recall HelloWorld: using System; class HelloWorld { static void Main() { Console.WriteLine("Hello world!"); } } This class declares no fields, only one method (Main). Objects class Employee { private string FirstName; 2 fields private string LastName; public string GetName() 1 method { return string.Concat(FirstName, " ", LastName); } } Literature and useful links Books on C#: Essential C# 5.0 by Mark Michaelis (with Eric Lippert) (language focused) Head first C# by Andrew Stellman and Jennifer Greene (Visual Studio and GUI) Links: http://msdn.microsoft.com/ (Reference, no Tutorial) http://msdn.microsoft.com/en-us/library/w0x726c2%28v=vs.110%29.aspx (.NET) http://msdn.microsoft.com/en-us/vstudio/hh341490.aspx (Visual C# references) http://msdn.microsoft.com/en-us/vstudio//bb798022.aspx (Visual C# Videos) http://code.msdn.microsoft.com/ (Code samples) http://msdn.microsoft.com/en-us/library/ms754130(v=vs.100).aspx (WPF) The web..