C# Fundamentals. built in data types. built in data types. C# is a strongly typed language

Similar documents
Introduction to C# Applications

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Chapter 2: Using Data

Object Oriented Programming with Visual Basic.Net

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

CS2141 Software Development using C/C++ C++ Basics

C# Types. Industrial Programming. Value Types. Signed and Unsigned. Lecture 3: C# Fundamentals

Industrial Programming

Visual C# Instructor s Manual Table of Contents

Computers and Programming Section 450. Lab #1 C# Basic. Student ID Name Signature

DigiPen Institute of Technology

Understand Computer Storage and Data Types

Introduction To C#.NET

.Net Technologies. Components of.net Framework

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Full file at

Tokens, Expressions and Control Structures

Topic 6: A Quick Intro To C

Exercise: Using Numbers

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Introduce C# as Object Oriented programming language. Explain, tokens,

3. Java - Language Constructs I

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 1 Getting Started

BASIC ELEMENTS OF A COMPUTER PROGRAM

Question And Answer.

C#.Net. Course Contents. Course contents VT BizTalk. No exam, but laborations

IT 374 C# and Applications/ IT695 C# Data Structures

10/9/2012. Computers are machines that process data. assignment in C# Primitive Data Types. Creating and Running Your First C# Program

CS242 COMPUTER PROGRAMMING

UNIT- 3 Introduction to C++

Methods: A Deeper Look

DAD Lab. 1 Introduc7on to C#

EMBEDDED SYSTEMS PROGRAMMING Language Basics

Java Programming. Atul Prakash

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class.

Computer Components. Software{ User Programs. Operating System. Hardware

Objectives. In this chapter, you will:

Syntax and Variables

A Comparison of Visual Basic.NET and C#

C++ Programming: From Problem Analysis to Program Design, Third Edition

COMP 202 Java in one week

Introduction to C# Applications Pearson Education, Inc. All rights reserved.

The Basics. chapter 2. Why You Should Read This Chapter

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Expressions and Casting

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Integer Data Types. Data Type. Data Types. int, short int, long int

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0

Computer Science II Lecture 1 Introduction and Background

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Advanced Computer Programming

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Chapter-8 DATA TYPES. Introduction. Variable:

Lesson 02 Working with Data Types. MIT 31043: VISUAL PROGRAMMING By: S. Sabraz Nawaz Senior Lecturer in MIT

Pointers, Dynamic Data, and Reference Types

Visual Studio.NET.NET Framework. Web Services Web Forms Windows Forms. Data and XML classes. Framework Base Classes. Common Language Runtime

Chapter 6. Simple C# Programs

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

6.096 Introduction to C++ January (IAP) 2009

3. Java - Language Constructs I

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Chapter 2 Basic Elements of C++

Variables and Constants

Lesson 02 Working with Data Types MIT 31043, Visual Programming By: S. Sabraz Nawaz

Array. Prepared By - Rifat Shahriyar

This tutorial has been prepared for the beginners to help them understand basics of c# Programming.

C#: framework overview and in-the-small features

A Fast Review of C Essentials Part I

Creating a C++ Program

Duhok Polytechnic University Amedi Technical Institute/ IT Dept. Halkawt Rajab Hussain

Agenda & Reading. VB.NET Programming. Data Types. COMPSCI 280 S1 Applications Programming. Programming Fundamentals

Zheng-Liang Lu Java Programming 45 / 79

CS 11 java track: lecture 1

Intermediate Programming, Spring 2017*

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

Getting started with Java


C# INTRODUCTION. Object. Car. data. red. moves. action C# PROGRAMMERS GUIDE LESSON 1. CsGuideL1.doc. Date Started: May 8,2001

Appendix G: Writing Managed C++ Code for the.net Framework

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

UNIT-2 Introduction to C++

EEE-425 Programming Languages (2013) 1

CSI33 Data Structures

Variables. Data Types.

CSCI 1061U Programming Workshop 2. C++ Basics

CSc Introduction to Computing

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Short Notes of CS201

C++\CLI. Jim Fawcett CSE687-OnLine Object Oriented Design Summer 2017

Programming in C and C++

Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments

Transcription:

C# Fundamentals slide 2 C# is a strongly typed language the C# compiler is fairly good at finding and warning about incorrect use of types and uninitialised and unused variables do not ignore warnings, write code simply re-write it if the compiler complains - it is most likely correct! built in data types slide 3 built in data types slide 4 Type No. of bytes Description byte 1 unsigned values 0-255 char 2 unicode characters bool 1 true orfalse sbyte 1 signed values -128..127 short 2 signed -32768..32767 ushort 2 unsigned 0..65535 int 4 signed 2 31.. 2 31 1 uint 4 unsigned 0..2 32 1 long 8 signed long integers 2 63..2 63 1 ulong 8 unsigned long integer 0..2 64 1 Type No. of bytesdescription float 4 ±1. 5 10 45.. ±3. 4 10 38 with about 7 double 8 ±5. 0 10 324.. ±1. 8 10 308 with 15 sig decimal 12 fixed precision up to 28 digits

Data types slide 5 Data types slide 6 in general if you are starting with a blank canvas when designing your programs, work out the smallest datatype which will contain the largest data values you need in general, do not store numbers in strings use constants if possible do not store numbers inchars sometimes library considerations and interfacing to older code might force your hand Data types slide 7 Implicit conversion example slide 8 objects of a fundamental data types can be converted into another either implicitly happen automatically (the compiler does this for you) explicitly where you tell the compiler the rule for conversion consider the following snippet of code: short x = 6; int y = x; // the compiler will convert the short to an i

Explicit conversion example slide 9 Definite assignment slide 10 however try this the other way around: short x; int y = 400; x=y;//will not compile you need to explicitly convert using thecast operator short x; int y = 400; x=(short) y; // will compile C# requires definite assignment variables must be initialised before being used using System; public class TestAssignment public static void Main (string[] args) int i; System.Console.WriteLine ("the value of i is 0", i when you compile this program the compiler will generate an error message Simplest hello world slide 11 C# conditional statements slide 12 using System; public class HelloWorld public static void Main (string[] args) Console.WriteLine ("Hello World"); very similar to C if constructs if (expression1) statement1 if (expression2) statement2 statement3

C# conditional statements slide 13 C# conditional statements slide 14 if (expression3) statement4; statement5; if (expression4) statement6; statement7; statement8; if (expression5) statement9; statement10; statement11; statement12; Cascading if statements slide 15 Constants slide 16 often useful to cascadeif statements use constants (or enumerations) if possible if (expression1) statement1 if (expression2) statement2 statement3 this can be extended to embrace multiple statements by using for example if you had to write a program to determine the state of water at different Fahrenheit temperatures we know infahrenheit water: boils at 212 freezes at 32

Constants slide 17 Enumerations slide 18 using System; public class WaterState public static void Main (string[] args) const int boiling = 212; const int freezing = 32; int QueryTemperature; Console.WriteLine ("Enter the temperature"); QueryTemperature = Convert.ToInt32 (Console.ReadLine ()) ; if (QueryTemperature >= boiling) Console.WriteLine ("at this temperature water becomes stea if (QueryTemperature <= freezing) Console.WriteLine ("at this temperature water becomes ice" Console.WriteLine ("at this temperature water is a liquid" the previous program can be improved ifweuse an Enumeration instead of constants we should use enumerations for constants which are naturally grouped together so we could introduce enum Temperatures boiling = 212, freezing = 32, Enumerations slide 19 Enumerations slide 20 using System; public class WaterState2 enum Temperatures boiling = 212, freezing = 32, public static void Main (string[] args) int QueryTemperature; Console.WriteLine ("Enter the temperature"); QueryTemperature = Convert.ToInt32 (Console.ReadLine if (QueryTemperature >= boiling) Console.WriteLine ("at this temperature water bec if (QueryTemperature <= freezing) Console.WriteLine ("at this temperature water bec Console.WriteLine ("at this temperature water is

C# for loops slide 21 C# do loops slide 22 similar to C, but with some improvements for (int i=1; i<=12; i++) Console.WriteLine ("8 x 0 = 1", i, i*8); int i = 1; do Console.WriteLine ("8 x 0 = 1", i, i*8); i++; while (i<=12); exactly the same as C notice we can declare the variableito live only within thefor loop why isthis sensible? Selection slide 23 foreach statement slide 24 switch andcase statements are available in C# asuperset of C, do not abuse the superset though foreach (char ch in mystring) Console.WriteLine ("now ch is 0", ch); seechartest example in the slide after next for the syntax

Program to determine vowels or consonants slide 25 Program to determine vowels or consonants slide 26 using System; public class chartest static bool is_vowel (char ch) switch (ch) case a : case e : case i : case o : case u : return true; default: return false; static void ProcessLine (string line) foreach (char ch in line) if (is_vowel (ch)) System.Console.WriteLine ("char 0 is a vowel", c System.Console.WriteLine ("char 0 is a consonant Program to determine vowels or consonants slide 27 Switch statements slide 28 in C# you can switch on string constants! public static void Main (string[] args) string line; Console.WriteLine ("Please enter string: "); line = Console.ReadLine (); ProcessLine (line); you can also abuse switch statements do not do it! keep switch statements simple and obvious the language allows you togoto acase clause, again use sparingly if at all

Switch statements slide 29 Switch statements slide 30 for example consider the following code fragment: switch (line) case "red": value = 1; case "purple": value = 2; case "yellow": Console.WriteLine ("yellow is deprecated, use oran goto case orange; case "orange": value = 3; default: value = 4; used sparingly like this is probably acceptable (as a fall through) Switch statements slide 31 Switch statements slide 32 switch (line) case "orange": value = 3; case "red": value = 1; case "purple": value = 2; case "yellow": Console.WriteLine ("yellow is deprecated, use oran goto case orange; default: value = 4; notice that a major differnce between C# and C/C++ is that the fall through must be done via agoto if the statement is present in the fall through case for example: but jumping out of sequence is considered very poor programming style

Switch statements slide 33 Switch statements slide 34 switch (line) case "red": value = 1; case "purple": value = 2; case "yellow": case "orange": value = 3; default: value = 4; notice the automatic fall through foryellow and orange switch (line) case "red": value = 1; case "purple": value = 2; case "yellow": Console.WriteLine ("yellow is deprecated, use oran goto case orange; case "orange": value = 3; default: value = 4; whereas the presence of thewriteline forces us to use agoto to fall through. The compiler would generate an error without thegoto Console input slide 35 Basic input/output via examples slide 36 can be achieved bythe statement: string line; int i;... line = Console.ReadLine ();... if (line!= null) Console.WriteLine ("you wrote: " + line); i=convert.toint32 (line); Console.WriteLine ("as an integer its value is 0", i... Console.Write ("what is your guess? "); line = Console.ReadLine (); if (line!= null) Console.WriteLine ("you wrote: " + line); your_guess = Convert.ToInt32 (line);... Console.WriteLine ("0 is too high, try a lower number"...

Tutorial question slide 37 Tutorial question slide 38 write a console/command line program in C# which chooses a number between 1 and 100 and asks you to guess the number it should allow multiple guesses and tell you whether you are too low ortoo high hint you will need Random rnd = new Random (1234); int secret = rnd.next (100)+1; to obtain a near random number between1..100 it should keep a count of the number of attempts Tutorial question slide 39 Tutorial question slide 40 notice that the statement Random rnd = new Random (1234); generates the same "random" number each time the program is run! implement your own function calledmyrandom which returns an object of typerandom and takes no arguments it should use the system methodrandom and pass the milliseconds from the time of day as its seed use your version ofmyrandom in your guessing number game