210 likes | 243 Views
C# Language. MIS 324 Professor Sandvig. Overview. Why learn C#? C# Basics Variables Data types Operators Control Structures. Why Learn C#?. Modern language Microsoft introduced in 2000 More user-friendly than older languages Managed code Memory allocation Garbage collection
E N D
C# Language MIS 324 Professor Sandvig
Overview • Why learn C#? • C# Basics • Variables • Data types • Operators • Control Structures
Why Learn C#? • Modern language • Microsoft introduced in 2000 • More user-friendly than older languages • Managed code • Memory allocation • Garbage collection • 100% object oriented • Widely used
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 • More explicit data type conversions
Variables • Variables are objects • Have properties & methods FName = FName.Replace(“S”,”P”);
Variables • Commonly Used C# Data Types
Datatype Conversion • Implicit Conversion • Done when no data lost
Datatype Conversion • Web Forms • 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 • Control structures • Classes • Good:
Common C# Operators Control Structures examples
Control Structures • Syntax similar to C, JavaScript, PHP, Java, … • 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 • Similar in all languages • Syntax may be different • 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++) { ViewBag.message += “value of i:” + i + “<br>”; }
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>”; }
Control Structures • 4. while loop int i = 0; while (i < 5) { i += 1; }
Summary • C# modern object oriented programing language • Relatively easy to use • Managed code • C-like syntax (similar toPHP, JavaScript, C, C++) • Strongly typed • Explicitly convert data types • Pick up syntax quickly • Visual Studio big help