1 / 29

C# Language MacDonald Ch. 2

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

lucio
Download Presentation

C# Language MacDonald Ch. 2

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. C# LanguageMacDonald Ch. 2 MIS 324 Professor Sandvig

  2. Overview • C# Basics • Variables • Datatype Conversion • Control Structures • Methods

  3. 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 */

  4. 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

  5. Variables • Strongly Typed • Advantage • Less error prone • Compiler more likely to catch programming errors • Disadvantage • Many explicit data type conversions

  6. Variables • Declaring Variables • string Name; • int Age; • Declare & assign value • string Name = “Joe”; • int Age = 6;

  7. Variables • Commonly Used C# Data Types

  8. Variables • Variables are objects • Have all properties & methods of class • Defined in “System” namespace • String documentation

  9. 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”);

  10. Variables IDE is very helpful:

  11. Concatenation • Concatenate strings with + string Name = lastName + “, “ + firstName; • End of statement • Semicolen;

  12. 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

  13. Datatype Conversion Convert Class decimal TaxRate = Convert.ToDecimal(8.15);

  14. Datatype Conversion • All server controls return type string:

  15. Datatype Conversion Solution: Re-cast to type double:

  16. Outline • C# Basics • Variables • Datatype Conversion • Control Structures • Methods

  17. Control Structures • Syntax similar to PHP, Java, C, C++, … • Curley brackets for statement blocks if (condition) { //Statement block } else { //statement block }

  18. Control Structures • May omit brackets for single statements: if (condition) statement 1; else statement 2;

  19. Control Structures • Testing conditions • Equality • double equal sign • if (age == 3) • Inequality • if (age > 3) • Negate • if (age != 3) • AND / OR • && and • || or

  20. Control Structures • Control Statements: • if • for • foreach • while

  21. Control Structures • 1. if else if (condition) { //do something } else if (condition) { //do something } else { //do something else }

  22. Control Structures • 2. for statement • increments a counter variable • may declare within statement for (int i = 0; i <= 10; i++) { lblMessage.Text += i + "<br>"; }

  23. 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; }

  24. Control Structures • 4. while loop int i = 0; while (i < 5) { i += 1; }

  25. Overview • C# Basics • Variables • Datatype Conversion • Control Structures • Methods

  26. 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; }

  27. Methods • return “void” if no value returned • void = “empty” void page_load() { //page_load is a page event }

  28. Example • CSharp.aspx • Output • Source

  29. 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

More Related