cs4443 - modern programming language - i lecture (2)

Post on 20-Mar-2017

9 Views

Category:

Education

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CS4443MODERN PROGRAMMING

LANGUAGE – I Mr. Dilawar

Lecturer,Computer Science Faculty,

Bakhtar UniversityKabul, Afghanistan.

Previous Outline• The C# language

• The .NET Architecture and .NET Framework

• CLR, MSIL, JITers, FCL, CLS, CTS and GC

• C# compare to C++

• Development Environment

• Console Applications and Windows Form Application

• Working with First Hello World Program in C#

• Understanding namespaces, using keyword, class keyword, main() method and comments in C#

C# Language FundamentalsChapter 2

Chapter Outline• Basic Data Types and their Mapping to CTS

• Variables, Constants, and Operators

• Working with Flow Control and Conditional Statements

• Type Conversion, String Manipulation and Complex Variable Types

• Arrays in C#

• Foreach loop

Lecture Outline• Basic Data Types and their Mapping to CTS

• Variables, Constants, and Operators

• Working with Flow Control and Conditional Statements

• Type Conversion, String Manipulation and Complex Variable Types

• Arrays in C#

• Foreach loop

Basic Data Types and their Mapping to CTS• Data types are implemented based on their classification.

• Value types (implicit data types, structures, and enumeration)• Reference types (objects, strings, arrays, delegates)

• A data type is a value type if it holds the data within its own memory allocation.• A variable of a value type is passed to method by passing an exact copy.

• A reference type contains a pointer to another memory location that holds the data.• A variable of a reference type is passed to method by passing only their reference.

Basic Data Types and their Mapping to CTS• Implicit data types are defined by the language vender.

• Explicit data types are composed or made by using the implicit data type.

• Implicit data types are complaint with .NET complaint languages and are mapped to types in the CTS.• Hence each implicit data type in C# has its corresponding .NET type.

• The implicit data types in C# are given in the next slide.

Basic Data Types and their Mapping to CTS

C# Type .NET Type Size in Bytes Description

byte Byte 1 May contain integers from 0 – 255

sbyte Sbyte 1 Signed byte from -128 to 127

short Int16 2 Ranges from -32768 to +32767

ushort UInt16 2 Unsigned, ranges from 0 to 65535

int Int32 4 Ranges from -2147483648 to +2147483647

uint UInt32 4 Unsigned, ranges from 0 to 4294967295

long Int64 8 -9223372036854775808 to +9223372036854775808

ulong UInt64 8 Unsigned, ranges from 0 to 18446744073709551615

Basic Data Types and their Mapping to CTS

C# Type .NET Type Size in Bytes Description

float Single 4 Ranges from to precision. Requires the suffix f ot F.

double Double 8 Ranges from to with 15-16 digits precision.

bool Boolean 1 Contains either true or false

char Char 2 Contains any single Unicode character enclosed in single quotation mark such as ‘c’

decimal Decimal 12 Ranges from to with 28-29 digits precision. Requires the suffix ‘m’ or ‘M’

Basic Data Types and their Mapping to CTS• Implicit data types are represented in language using keywords in C#.

• string is also a keyword in C#.

• Implicit data types – are value types and they are stored on stack.• A stack is a data structure that store items in FIFO fashion.• It is an area of memory supported by the processor and its size is determined at

the compile time.

• While user defined types or referenced types are stored using heap.• A heap consists of memory available to the program at runtime.• Reference types are allocated using memory available from the heap dynamically.

Variables• A variable is the name given to a memory location holding a particular

type of data.• Each variable has a data type and a value.

• In C#, variable are declared as: <data type> <variable_name>;

• You can initialize the variable as you declare it (on the fly).• You can also declare/initialize multiple variables of the same type in a single

statement.

Variables• In C#, like other languages you must declare variables before using

them.

• Definite Assignment – Local variables must be initialized before being used.

• C# does not assign default values to local variables.

• C# is also a type safe language.• Values of particular data type can only be stored in their respective data type.• Can’t store integer values in Boolean data types like we do in C/C++.

Naming Conventions for Variable and Methods• Microsoft suggests using Carrel Notation for variables.• First letter in lowercase.

• Pascal Notation for methods.• First letter in uppercase.

• Each word after the first word in the name of both variables and methods should start with a capital letter.

Constants• Constants are variables whose values, once defined, can not be

changed by the program.

• Constant variables are declared using the CONST keyword like: const double PI = 3.142;

• Constant variables must be initialized as they are declared.

• It is conventional to use capital letters when naming constant variables.

Operators • Symbols used to perform operation on the data.

• Arithmetic operators +, -, *, /, %• Increment and Decrement ++, --

• Prefix and Postfix notation

• In prefix, the compiler will increment the variable and then will use it.

• In postfix, the compiler will first use and then increment it.

• Arithmetic Assignment Operators S+=, -=, *=, /=, %=• Relational Operators ==, !=, >, <, >=, <=• Logical and Bitwise Operators&, |, ^, !, &&, ||• Other operators <<, >>, ., [], (), ? :

Expressions• C# contains number of operators for this purpose.• By combining operators with variables and literal value (together referred as

operands), you can create expressions, which is the building blocks of computation.

Operator Precedence

Flow Control and Conditional StatementsThe if Statement

Flow Control and Conditional StatementsThe if…….else….. Statement

Nested if and Nested if-else

Flow Control and Conditional StatementsThe switch……..case Statement

Flow Control and Conditional StatementsThe for loop Statement

Use of Continue and Break Statement.

Flow Control and Conditional StatementsThe do……while Loop Statement

Flow Control and Conditional StatementsThe while Loop Statement

Flow Control and Conditional StatementsThe goto Statement

Type Conversion• There are two types of type conversion:• Implicit and Explicit conversion

• Implicit conversion requires no work on your part and no additional code.

Type Conversion

Type Conversion• As the name suggests, an explicit conversion occurs when you

explicitly ask the compiler to convert a value from one data type to another. • Requires extra code, and format of this code may vary, depending on the

exact conversion method.

Type Conversion• Before working with explicit conversion, try the code below:

Type Conversion• To get the code compile, you need to add some code.• in this context, you have to cast the short variable into a byte (as suggested by

the preceding error string).

• Casting basically means forcing data from one type into another, and it uses the following simple syntax:

<(destinationType)sourceVar>

Type Conversion

Type Conversion

Type Conversion• Attempting to fit a value into a variable when that value is too big for

the type of that variable results in an overflow, and this is the situation you want to check for.

• Two keywords exist for setting what is called the overflow checking context for an expression: checked and unchecked. checked (<expressions>)

unchecked (<expressions>)

Type Conversion

Type Conversion• You can also use Convert command to make explicit conversion.

String Manipulation• Writing strings to console, reading strings from the console, and

concatenating strings using the + operator.

• A string type variable can be treated as a read-only array of char variables.• You can access individual characters using syntax like the following:

String Manipulation• To get a char array you have to use ToCharArray() command of

the array variable.

• Later, you can manipulate it as normal array of characters.

• You can access the number of elements using myString.Length.

String Manipulation• You can work with other operations like <string.ToUpper()>

and <string.ToLower()> and etc…

• As it not changes the original value so it’s better to assign it into another variable again.

String Manipulation• For removing extra spaces for ease of

interpretation so you can use <string>.Trim() command.

• You can also specify which string to trim from the string.

• You can work <string>.TrimStart() and <string>.TrimEnd()• Spaces from the beginning and end of the string.

• You can use <string>.PadLeft() and <string>.PadRight().

Complex Variable Types• Enumeration (often referred to as enum), structs (referred to as

structures) and arrays.

• Enumeration allow the definition of a type that can take one of a finite set of values that you supply.• Orientation example – creating enum type called orientation.

• It creates a user-defined type and then it is applied to a variable.

Complex Variable Types• You can use enum keyword to define

enumeration as given.

• Next, you can declare variable of this new type.

• You can assign values using the given syntax.

Complex Variable Types• structs are data structures

that are composed of several pieces of data, possibly of different types.

• You can use struct keyword to define structure as given.

Arrays• Situations where you want to store a lot of data, so you have to

declare individual variables as

• Arrays are indexed lists of variables stored in a single array type variable.• An array called friendNames that stores the three names (elements) and can

be accessed through number (index) in square brackets, as shown:

Arrays• Arrays are declared in the following way:

• Arrays can be initialized in two ways:

Arrays• Alternative ways

foreach Loop• A foreach loop enables you to address each element in an array

using this simple syntax:

• This loop will cycle through each element, placing it in the variable <name> in turn, without danger of accessing illegal elements.

• It gives you read-only access to the array contents.

foreach Loop

Summery• Basic Data Types and their Mapping to CTS

• Variables, Constants, and Operators

• Working with Flow Control and Conditional Statements

• Type Conversion, String Manipulation and Complex Variable Types

• Arrays in C#

• foreach loop

Thank YouFor your Patience

top related