870 likes | 1.1k Vues
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.
E N D
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
THE BIG DIFFERENCE C# IS CASE SENSITIVE
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)
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
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”);
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++
C# Blocks (Example) namespace Validate { public static class ValidateNumbers { public static boolIsInteger( string arg) { } } }
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 */
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
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
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
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
Value Types • The contents of the variable is simply the value • See figure 2-2 on page 17
Reference Types • There are two parts to a reference type • An object • A reference to the object • See figure 2-3 on page 18
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
The Type Taxonomy • Page 20
Data Types (Selected) See Table 2-1 on page 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
Local Variable Declaration (Examples) • VB Dim count As Integer Dim MyName As String = “Ekedahl” • C# int count; string MyName = “Ekedahl”;
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;
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)
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
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
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++;
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
Specialized Floating-point Operations • +- infinity has a special value Not a Number (NaN) • Division by 0
The Decimal Data Type • Use for man-made values such as currency • 28-29 significant digits of precision
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!
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
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
Conditional Operators • These are quite different but do the same thing
Conditional Operators (Short Circuiting) • They are used to test for a null reference that would otherwise throw an exception if used • The improve efficiency
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 }
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. }
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. }
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
switchstatement (Example) switch (day) { case 0: DayOfWeek = “Sunday”; break; case 1: DayOfWeek = “Monday”; break; }
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
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 = ‘\\’;
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
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
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
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
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
Procedures (Example 1) • The following procedure does not return a value private voidInitializeLocal() { // Statements }