MIS 324 -- Professor Sandvig MIS 324 Professor Sandvig 11/7/2018 C# Language MIS 324 Professor Sandvig
MIS 324 -- Professor Sandvig 11/7/2018 Overview Why learn C#? C# Basics Variables Data types Operators Control Structures
Why Learn C#? Modern language More user-friendly than older languages Microsoft introduced in 2000 More user-friendly than older languages Managed code Memory allocation Garbage collection 100% object oriented Widely used
MIS 324 -- Professor Sandvig 11/7/2018 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
MIS 324 -- Professor Sandvig 11/7/2018 Variables Strongly Typed Advantage Less error prone Compiler more likely to catch programming errors Disadvantage More explicit data type conversions
Variables Variables are objects Have properties & methods FName = FName.Replace(“S”,”P”);
MIS 324 -- Professor Sandvig 11/7/2018 Variables Commonly Used C# Data Types int 123 string “hi” double 1.23 bool true DateTime Jan. 8, 2010 1:23:46 pm
Datatype Conversion Implicit Conversion Done when no data lost
Datatype Conversion Web Forms .NET MVC Lots of data type conversions All data from web forms type string .NET MVC Data types defined in model Few datatype conversions needed
Variable Scope Control structures limit variable scope Where variable is visible Visible in structure and children
Variable Scope Try to minimize scope Minimize complexity “The road to Hell is paved with global variables” Steve McConnell Code Complete 2nd Ed.
Variable Scope Write code to minimize variable scope Good: Control structures Classes Good:
MIS 324 -- Professor Sandvig 11/7/2018 Common C# Operators Operator Function Example = Assignment int a = 3; == equality if (a == 3) >= Greater or equal if (a >= 3) + Add int c = 3 + 4; Concatenate string s = “Hello ”+ “world”; ++ Increment by 1 i++; += Sum i += 3; s += “ and other planets”; ! Negate if(a != b) Control Structures example
MIS 324 -- Professor Sandvig 11/7/2018 Control Structures Syntax similar to C, JavaScript, PHP, Java, … Curley brackets for statement blocks if (condition) { //Statement block } else //statement block
MIS 324 -- Professor Sandvig 11/7/2018 Control Structures May omit brackets for single statements: if (condition) statement 1; else statement 2;
MIS 324 -- Professor Sandvig 11/7/2018 Control Structures Similar in all languages Syntax may be different if for foreach while
MIS 324 -- Professor Sandvig 11/7/2018 Control Structures 1. if else: if (condition) { //do something } else if (condition) else //do something else
MIS 324 -- Professor Sandvig 11/7/2018 Control Structures 2. for statement increments a counter variable may declare within statement for (int i = 0; i <= 10; i++) { ViewBag.message += “value of i:” + i + “<br>”; }
MIS 324 -- Professor Sandvig 11/7/2018 Control Structures 3. foreach loop iterate through collections arrays, controls, cookies, etc. int[] intArray = {3, 5, 7, 9}; foreach (int item in intArray) { ViewBag.message += “Item: “ + item + “<br>”; }
MIS 324 -- Professor Sandvig 11/7/2018 Control Structures 4. while loop int i = 0; while (i < 5) { i += 1; }
MIS 324 -- Professor Sandvig 11/7/2018 Summary C# modern object oriented programing language Relatively easy to use Managed code C-like syntax (similar to PHP, JavaScript, C, C++) Strongly typed Explicitly convert data types Pick up syntax quickly Visual Studio big help