290 likes | 399 Views
C# Language MacDonald Ch. 2. MIS 324 Professor Sandvig. Overview. C# Basics Variables Datatype Conversion Control Structures Methods. C# Basics. Many similarities to PHP Case sensitive Curly brackets Statements end with semi-colons; double equal signs to test equality
E N D
C# LanguageMacDonald Ch. 2 MIS 324 Professor Sandvig
Overview • C# Basics • Variables • Datatype Conversion • Control Structures • Methods
C# Basics • Many similarities to PHP • Case sensitive • Curly brackets • Statements end with semi-colons; • double equal signs to test equality • if (i == 3) … • Comments: // single line /* Multi-line comment like this */
Variables • Declaring Variables • C# strongly typed • Similar to VB.NET • PHP loosely typed • Strongly typed: • A variable accepts only one data type • Type is explicitly declared when variable is declared
Variables • Strongly Typed • Advantage • Less error prone • Compiler more likely to catch programming errors • Disadvantage • Many explicit data type conversions
Variables • Declaring Variables • string Name; • int Age; • Declare & assign value • string Name = “Joe”; • int Age = 6;
Variables • Commonly Used C# Data Types
Variables • Variables are objects • Have all properties & methods of class • Defined in “System” namespace • String documentation
Variables Example: • C# declaration: string FName = “Sam”; • “FName” is string object • Has all attributes of string class • Use string replace method: FName = FName.Replace(“S”,”P”);
Variables IDE is very helpful:
Concatenation • Concatenate strings with + string Name = lastName + “, “ + firstName; • End of statement • Semicolen;
Datatype Conversion • C# strongly typed language • Data types must be explicitly converted • Several ways to convert data types • Convert Class • most robust • easy to use
Datatype Conversion Convert Class decimal TaxRate = Convert.ToDecimal(8.15);
Datatype Conversion • All server controls return type string:
Datatype Conversion Solution: Re-cast to type double:
Outline • C# Basics • Variables • Datatype Conversion • Control Structures • Methods
Control Structures • Syntax similar to PHP, Java, C, C++, … • Curley brackets for statement blocks if (condition) { //Statement block } else { //statement block }
Control Structures • May omit brackets for single statements: if (condition) statement 1; else statement 2;
Control Structures • Testing conditions • Equality • double equal sign • if (age == 3) • Inequality • if (age > 3) • Negate • if (age != 3) • AND / OR • && and • || or
Control Structures • Control Statements: • if • for • foreach • while
Control Structures • 1. if else if (condition) { //do something } else if (condition) { //do something } else { //do something else }
Control Structures • 2. for statement • increments a counter variable • may declare within statement for (int i = 0; i <= 10; i++) { lblMessage.Text += i + "<br>"; }
Control Structures • 3. foreach loop • iterate through collections • arrays, controls, cookies, etc. int[] intArray = {3, 5, 7, 9, 11}; int sum = 0; foreach (int item in intArray) { sum += item; }
Control Structures • 4. while loop int i = 0; while (i < 5) { i += 1; }
Overview • C# Basics • Variables • Datatype Conversion • Control Structures • Methods
Methods • Also known as: • Subroutines in VB • Functions in PHP • Chunks of code that do something useful • Pass parameters in, return a result intmyDoubler(intAnyInteger) { return AnyInteger* 2; }
Methods • return “void” if no value returned • void = “empty” void page_load() { //page_load is a page event }
Example • CSharp.aspx • Output • Source
Summary • C# modern object oriented programing language • Many features that we do not use in 324 • C-like syntax (similar toPHP, JavaScript) • Strongly typed • Explicitly convert data types • Pick up syntax quickly • Visual Studio big help