161x Filetype PDF File size 0.99 MB Source: www.kau.edu.sa
C# Development Rob Miles 2008-2009 Department of Computer Science University of Hull Contents Introduction....................................................................................................................... 11 Welcome ............................................................................................................. 11 Reading the notes................................................................................................ 11 Getting a copy of the notes ................................................................................. 11 Computers 12 An Introduction to Computers .......................................................................................... 12 Hardware and Software ...................................................................................... 12 Data and Information ........................................................................................................ 13 Data Processing .................................................................................................. 13 Programmer’s Point:At the bottom there is always hardware ............................. 14 Programming Languages 15 What is Programming? ..................................................................................................... 15 From Problem to Program .................................................................................. 15 Programmer’s Point:The specification must always be there ............................. 16 A Simple Problem ............................................................................................................. 16 Specifying the Problem ....................................................................................... 16 Programmer’s Point:metadata is important ......................................................... 17 Programmer’s Point:Good programmers are good communicators .................... 19 Programming Languages .................................................................................................. 19 Programmer’s Point:The language is not that important..................................... 20 C# 20 A look at C# ...................................................................................................................... 20 Dangerous C ....................................................................................................... 20 Programmer’s Point:Computers are always stupid ............................................. 21 Safe C# ............................................................................................................... 21 C# and Objects ................................................................................................... 21 Making C# Run .................................................................................................. 21 Creating C# Programs ........................................................................................ 22 The Human Computer ........................................................................................ 22 Programmer’s Point:Great programmers debug less .......................................... 22 What Comprises a C# Program? ....................................................................................... 22 Controlling the Compiler .................................................................................... 23 Storing the Data .................................................................................................. 23 Describing the Solution ...................................................................................... 23 Identifiers and Keywords .................................................................................... 23 A First C# Program 24 The Program Example ...................................................................................................... 24 using System; ............................................................................................. 24 class GlazerCalc ...................................................................................... 25 static.............................................................................................................. 25 void .................................................................................................................. 25 Main .................................................................................................................. 25 () ....................................................................................................................... 25 i { .......................................................................................................................... 26 double.............................................................................................................. 26 width, height, woodLength, glassArea ...................................... 26 Programmer’s Point:Know where your data comes from ................................... 26 ; ......................................................................................................................... 26 string widthString, heightString; ............................................. 27 widthString = ............................................................................................. 27 Console. ......................................................................................................... 27 ReadLine ......................................................................................................... 27 () ....................................................................................................................... 27 ; ......................................................................................................................... 28 width = ........................................................................................................... 28 double. ........................................................................................................... 28 Parse ................................................................................................................ 28 (widthString); .......................................................................................... 28 heightString = Console.ReadLine(); height = double.Parse(heightString);............................................................ 29 woodLength = 2*(width + height)*3.25 ; ................................. 29 glassArea = 2 * ( width * height ) ; ...................................... 29 Console.WriteLine ................................................................................... 29 ( ......................................................................................................................... 29 "The length of the wood is " ......................................................... 29 + ......................................................................................................................... 29 woodLength .................................................................................................... 30 + " feet" ...................................................................................................... 30 ) ......................................................................................................................... 30 ; ......................................................................................................................... 31 } ......................................................................................................................... 31 } ......................................................................................................................... 31 Programmer’s Point:Program layout is very important ...................................... 31 Punctuation ......................................................................................................... 31 Manipulating Data 32 Variables and Data ............................................................................................................ 32 Types of Variables ............................................................................................................ 32 Storing Numbers ................................................................................................. 32 Storing integer values ......................................................................................... 33 Programmer’s Point:Check your own maths....................................................... 33 integer literal values ............................................................................................ 34 Storing real values .............................................................................................. 34 real literal values ................................................................................................. 34 Programmer’s Point:Simple variables are probably best .................................... 35 Storing Text ........................................................................................................ 35 char variables ...................................................................................................... 35 char literal values ................................................................................................ 35 string variables .................................................................................................... 36 string literal values .............................................................................................. 37 bool variables ..................................................................................................... 37 bool literal values ............................................................................................... 37 Programmer’s Point:Think about the type of your variables .............................. 37 Identifiers .......................................................................................................................... 38 Programmer’s Point:Think about the names of your variables ........................... 38 Giving Values to Variables ............................................................................................... 39 Expressions ......................................................................................................... 39 Changing the Type of Data ............................................................................................... 40 Widening and Narrowing ................................................................................... 40 Casting ................................................................................................................ 41 ii Types of Data in Expressions............................................................................................ 42 Programmer’s Point:Casts can add clarity .......................................................... 43 Programs and Patterns ...................................................................................................... 43 Writing a Program 44 Software as a story ............................................................................................................ 44 Comments ........................................................................................................... 45 Programmer’s Point:Don't add too much detail .................................................. 45 Program Flow ................................................................................................................... 45 Conditional Execution - if .................................................................................. 46 Conditions and Relational Operators .................................................................. 46 Combining Logical Operators ............................................................................ 48 Programmer’s Point:Break down your conditions .............................................. 48 Lumping Code Together ..................................................................................... 48 Metadata, Magic Numbers and const ................................................................. 49 Loops ................................................................................................................................ 50 Programmer’s Point:Don't be clever/stupid ........................................................ 53 Breaking Out of Loops ....................................................................................... 53 Programmer’s Point:Be careful with your breaks ............................................... 53 Going back to the top of a loop .......................................................................... 54 More Complicated Decisions ............................................................................. 54 Programmer’s Point:Get used to flipping conditions .......................................... 54 Complete Glazing Program ................................................................................ 54 Operator Shorthand ........................................................................................................... 55 Statements and Values ........................................................................................ 56 Programmer’s Point:Always strive for simplicity ............................................... 57 Neater Printing .................................................................................................................. 57 Using Placeholders in Print Strings .................................................................... 57 Methods 59 Methods So Far ................................................................................................................. 59 Method and Laziness .......................................................................................... 59 Parameters .......................................................................................................... 60 Return values ...................................................................................................... 60 A Useful Method ................................................................................................ 60 Programmer’s Point:Design with methods ......................................................... 61 Method Limitations ............................................................................................ 61 Programmer’s Point:Document your side-effects ............................................... 63 Programmer’s Point:Languages can help programmers...................................... 63 Method Libraries ................................................................................................ 64 Programmer’s Point:Always consider the failure behaviours ............................. 64 Variables and Scope ......................................................................................................... 64 Scope and blocks ................................................................................................ 65 Nested Blocks ..................................................................................................... 65 For loop local variables ...................................................................................... 66 Programmer’s Point:Plan your variable use ........................................................ 66 Arrays 66 Why We Need Arrays ....................................................................................................... 66 Array Elements ................................................................................................... 67 Array Element Numbering .................................................................................. 68 Large Arrays ....................................................................................................... 68 Managing Array Sizes ........................................................................................ 68 Creating a Two Dimensional Array .................................................................... 69 More than Two Dimensions ............................................................................... 69 Programmer’s Point:Keep your dimensions low ................................................ 69 iii
no reviews yet
Please Login to review.