Anúncio
Anúncio

Mais conteúdo relacionado

Anúncio
Anúncio

CS4443 - Modern Programming Language - I Lecture (2)

  1. CS4443 MODERN PROGRAMMING LANGUAGE – I Mr. Dilawar Lecturer, Computer Science Faculty, Bakhtar University Kabul, Afghanistan.
  2. 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#
  3. C# Language Fundamentals Chapter 2
  4. 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
  5. 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
  6. 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.
  7. 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.
  8. 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
  9. Basic Data Types and their Mapping to CTS C# Type .NET Type Size in Bytes Description float Single 4 Ranges from ±1.5 × 10 − 45 to ±3.4 × 10 − 38 precision. Requires the suffix f ot F. double Double 8 Ranges from ±5.0 × 10 − 324 to ±1.7 × 10308 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 1.0 × 10 − 28 to 7.9 × 1028 with 28- 29 digits precision. Requires the suffix ‘m’ or ‘M’
  10. 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.
  11. 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.
  12. 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++.
  13. 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.
  14. 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.
  15. 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 <<, >>, ., [], (), ? :
  16. 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.
  17. Operator Precedence
  18. Flow Control and Conditional Statements The if Statement
  19. Flow Control and Conditional Statements The if…….else….. Statement Nested if and Nested if-else
  20. Flow Control and Conditional Statements The switch……..case Statement
  21. Flow Control and Conditional Statements The for loop Statement Use of Continue and Break Statement.
  22. Flow Control and Conditional Statements The do……while Loop Statement
  23. Flow Control and Conditional Statements The while Loop Statement
  24. Flow Control and Conditional Statements The goto Statement
  25. 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.
  26. Type Conversion
  27. 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.
  28. Type Conversion • Before working with explicit conversion, try the code below:
  29. 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>
  30. Type Conversion
  31. Type Conversion
  32. 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>)
  33. Type Conversion
  34. Type Conversion • You can also use Convert command to make explicit conversion.
  35. 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:
  36. 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.
  37. 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.
  38. 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().
  39. 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.
  40. 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.
  41. 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.
  42. 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:
  43. Arrays • Arrays are declared in the following way: • Arrays can be initialized in two ways:
  44. Arrays • Alternative ways
  45. 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.
  46. foreach Loop
  47. 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
  48. Thank You For your Patience

Notas do Editor

  1. They can classified according to whether a variable of particular type stores its own data or a pointer to the data. Namespaces, modules, events, properties and procedures, Variables, constants, and fields.
  2. The GC searches for non-referenced data in heap during the execution of program and returns that space to OS.
  3. &, |, ^ (XOR -- not both true or false) are rarely used in usual programming practice. ! Operator is used to negate a Boolean or bitwise expression. Unary, Binary, Ternary
  4. Switch takes less time as compare to several if-else. You can use either string or integer. The expression must be constant. Illegal to use variable after case. A colon is used after the case statement and not a semicolon. You can use multiple statements under single case and default statements. Brackets are not used. C# does not allow fall-through. Without break not allowed. Position of default is not important.
  5. Loops are used for iteration purposes (doing task multiple times until the termination condition is met).
  6. Loops are used for iteration purposes (doing task multiple times until the termination condition is met).
  7. Loops are used for iteration purposes (doing task multiple times until the termination condition is met).
  8. Loops are used for iteration purposes (doing task multiple times until the termination condition is met).
  9. Bool and string have no implicit conversion.
  10. Any type A whose range of possible values completely fits inside the range of possible values of type B can be implicitly converted into that type.
  11. 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.
  12. With this code execution, it will crash with the error message. It can be configured using windows as well.
  13. myString = myString.PadLeft(10, ‘-’); Write a program to split the text. Write a console application that accepts a string from the user and outputs a string with the characters in reverse order. Write a console application that accepts a string and replaces all occurrences of the string no with yes. Write a console application that places double quotes around each word in a string.
  14. Array entries are often referred to as elements.
  15. Friends.length
  16. Friends.length
Anúncio