1 / 80

The C# Language and Program Structure

The C# Language and Program Structure. Lecture Overview (1). Basics of the C# language for the VB programmer Language syntax The basics of data types Numeric types Integral types Floating point types String types. Lecture Overview (2). Expressions / Parameters / Statements.

kaili
Télécharger la présentation

The C# Language and Program Structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The C# Language and Program Structure

  2. Lecture Overview (1) • Basics of the C# language for the VB programmer • Language syntax • The basics of data types • Numeric types • Integral types • Floating point types • String types

  3. Lecture Overview (2) • Expressions / Parameters / Statements

  4. THE BIG DIFFERENCE C# IS CASE SENSITIVE

  5. C# Syntax • All words are categorized as • Identifiers (programmer chosen names) • Keywords (words reserved by the C# language) • You can force a keyword to operate as a variable using the @ character • Literals (numbers or strings you embed into the application)

  6. C# Syntax • Punctuators give structure to the program • The semicolon terminates a statement • The { } group statements to form a block • Similar to the Endx in VB • Operators (usually a symbol) combine and transform expressions • +, -, *, / • Comments are ignored by the system

  7. Statements • In C#, all statements end with a semicolon ; • Statements can span multiple lines • There is no need for a continuation character as in VB • Example C# System.Console.WriteLine( “This is a line of text”); • Example VB System.Console.WriteLine( _ “This is a line of text”);

  8. C# Blocks • In VB, blocks are marked with keywords • Sub – End Sub • If – End If • Do Loop • While End While • In C#, blocks are all marked with {} as in Java or C++

  9. C# Blocks (Example) namespace Validate { public static class ValidateNumbers { public static boolIsInteger( string arg) { } } }

  10. Comments • XMLSummary comments begin with /// • Single line comments are marked with // • Multi-line comments start with /* and end with */ // A comment. /* This is Also a comment */

  11. Introduction to Types • A type is a blueprint for a value • That blueprint defines the range of allowable values and other information • Types are predefined by the C# language • Custom types you create work the same way • We call this type symmetry

  12. Predefined Types • These are types that are supported by the C# compiler • int, string, double, bool, …. • We call these built-in type • Not all data types are ‘built-in’ • The DateTime data type is part of the .NET Framework class library

  13. Custom Types • Complex types are created from primitive types and other complex types • These types contain • Data members • Function members • A special function member called a constructuor

  14. Types of Types • All types fall into one of the following two categories • Value types comprise most built-in types • enum and struct are also value types • Arrays and classes are reference types • Along with just about everything else

  15. Value Types • The contents of the variable is simply the value • See figure 2-2 on page 17

  16. Reference Types • There are two parts to a reference type • An object • A reference to the object • See figure 2-3 on page 18

  17. Null – the Special Case • A special case when a reference type variable DOES NOT point to an object • Here is where null reference exceptions come from

  18. Instance and Static Members

  19. The Type Taxonomy • Page 20

  20. Data Types (Selected) See Table 2-1 on page 21

  21. Variable Declarations • Variable declarations work the same way in both C# and VB • Local variables are declared inside of a procedure • Module-level variables are declared outside of a procedure • In C#, the declaration syntax is reversed from VB

  22. Local Variable Declaration (Examples) • VB Dim count As Integer Dim MyName As String = “Ekedahl” • C# int count; string MyName = “Ekedahl”;

  23. Variable Declarations (Examples) • VB syntax to declare a module-level variable • Private Counter As Integer • C# syntax to declare the same variable • private int Counter;

  24. C# Predefined Type Aliases • The .NET Framework defines all types • So • System.Int32 I = 5; • Is equivalent to • inti=5; • The C# int is just an alias (VB does the same thing)

  25. Introduction to Constructors • In an OOP language, objects are created from their underlying types • StreamReader, TextBox, Button… • The new keyword creates a new instance of a custom type

  26. Numeric Type Conversion • C# implicitly allows “widening type coercion” • More restrictive types are implicitly converted to less restrictive types • The reverse conversion MUST be expicit

  27. Operators (Arithmetic) • Mathematical operators are the same (+, -, #, /) for both VB and C# with a few exceptions • % is the modulus operator (Mod) • ++ and -- are post and pre-increment and decrement operators • Increment count (Example) count++;

  28. Specialized Integral Operations • When operands are both integers, any remainder is truncated • Exceptions are not thrown for underflow or overflow • Beware of short and byte

  29. Specialized Floating-point Operations • +- infinity has a special value Not a Number (NaN) • Division by 0

  30. The Decimal Data Type • Use for man-made values such as currency • 28-29 significant digits of precision

  31. The Boolean Data Type • bool is equivalent (aliased) to System.Boolean • It stores the literal value true or false • The runtime stores the value in one byte • Use the BitArray class for more efficient storage • Don’t convert from numeric to boolean data types!

  32. Operators (Logical) (Boolean) • They are pretty much the same from VB and C# • Inequality (<>) in VB is (!=) in C# • Equality (=) in VB is (==) in C# • In C#, the single (=) is always used for assignment statements • Use caution with floating point values and rounding error

  33. Logical Operators and Value / Reference Types • The notion of equality is obvious for numeric types • Reference types work differently • Equality is based on reference, rather than content • Reference types are equal if two variables point to (reference) the same object

  34. Conditional Operators • These are quite different but do the same thing

  35. Conditional Operators (Short Circuiting) • They are used to test for a null reference that would otherwise throw an exception if used • The improve efficiency

  36. Decision-Making Statements (if) • if statements take a boolean expression as an argument • Note the parentheses are required if (i >= 0) { // do something if i is // greater than 0 }

  37. Decision-Making Statements ( 2-way if) • Use the else keyword to create a 2-way if statement if ( i >= 0) { // Do something if i is // greater than 0. } else { // Do something else. }

  38. Decision-Making Statements ( multi-way if) • Use else if to create multi-way decisions if (i > 0) { // Do something if i is // greater than 0. } else if (i < 0) { // Do something else. } else { // i must be equal to 0. }

  39. Decision-Making Statements (switch) • C# uses the switch statement instead of Select Case • Both work the same way • The break keyword must appear at the end of each case

  40. switchstatement (Example) switch (day) { case 0: DayOfWeek = “Sunday”; break; case 1: DayOfWeek = “Monday”; break; }

  41. Strings (VB Comparison) • Definition - An immutable sequence of Unicode characters • Use the System.String data type as in VB; • The C# string type maps to System.String • The string concatenation operator is + instead of & • The members are the same between C# and VB

  42. The char Data Type • It’s a single Unicode character • Use the backslash “\” character to ‘escape’ special characters • Store the letter ‘a’ in the char variable cchar c= ‘a’; • Store the backslash character in the variable bschar bs = ‘\\’;

  43. The char Data Type (Escape sequences

  44. Using the char Data Type • Every char has a unique UNICODE value • We can get this value and test character codes • We can insert characters not on your keyboard

  45. The string Data Type • It’s an alias for System.String • Literal values appear inside double quotes • Even though strings are reference types, the test for equality tests the string value rather than the string reference • The <> are not supported. Use the CompareTo method • To create a verbatim string literal preface the literal with the @ character

  46. Variables and Parameters • Remember that a variable is just a storage location. And a variable contains a modifiable value • Variables can be categorized as: • Local variables (declared in a procedure or block) • Parameters passed to procedures (arguments) • Instance or static fields (roughly public and private variables) • Arrayelements

  47. Memory Allocation of Variables • Logically, memory is allocated from two places • The stack (stores local variables and parameters) • It’s also how we return from a procedure • The heap stores reference type instances • The CLR periodically cleans the heap

  48. Procedures and Parameters • Visual Basic has Function and Sub procedures • C# works a bit differently • Procedures that don’t return a value have a data type of void • Procedures that do return a value have an explicitly defined data type

  49. Procedures (Example 1) • The following procedure does not return a value private voidInitializeLocal() { // Statements }

More Related