Keith Elder Microsoft MVP
Assumptions I assume you Have 1 – 2 years of programming experience Understand the basic concepts of Variables Loops Arrays Can type over 40 words per minute
Very Simple Program A C# program requires one Main method. This is the entry point to the program. All code is held within a class. Blocks of code are represented with curly braces {}. Each line ends with a semi-colon; C# is a strongly typed language. C# is an object oriented language class MyFirstProgram { static void Main() { int x = 5; int y = 10; int z = x + y; } C# Quick Facts
Breaking Down C# - Namespace Namespace Abstract container providing organization to items. Typically organized by: Company Product Team Sub-Team Etc. Namespace Class Method1 Method2 Properties (data)
Breaking Down C# - Namespace namespace SkunkWorks { public class Loan { public decimal LoanAmount { get; set; } public bool IsApproved { get; set; } } namespace Vendor { public class Loan { public decimal LoanAmount { get; set; } public bool IsApproved { get; set; } public int CreditScore { get; set; } }
Breaking Down A C# - Class A class is just a blue print for an object. An object is an allocated region of storage. An object doesn’t exist until memory is allocated. A class holds data (properties/fields) and is made up of one or more methods (functions). Namespace Class Method1 Method2 Properties (data)
Breaking Down C# – Class / Properties namespace SkunkWorks { public class Loan { public decimal LoanAmount { get; set; } public bool IsApproved { get; set; } } Loan myLoan = new Loan(); When an instance of a Loan is created in memory, this becomes an object. Thus the variable myLoan is an object. Properties Class
Breaking Down C# - Method Think of methods as actions that classes can perform. Take a function, put it in a class, we call it a method. C# doesn’t allow functions to live and breathe outside of a class. Thus, all we have is methods. Methods can access other methods within the class and properties, and call other methods in other classes. public class Loan { public decimal LoanAmount { get; set; } public bool IsApproved { get; set; } public void MarkApproved() { IsApproved = true; OnMarkedApproved(); }
Putting it all together namespace ConsoleApplication { class Program { static void Main() { Loan myLoan = new Loan(); myLoan.LoanAmount = ; } Using statements specify which Namespaces to use to resolve classes. using System; using Vendor;
All Lines Must End in ; Correct Incorrect int x = 5;int x = 5
Variables Must Declare Type Correct Incorrect int x = 5;x = 5;
Supported C# Types C# type keywords.Net Framework Type boolSystem.Boolean byteSystem.Byte sbyteSystem.Sbyte charSystem.Char decimalSystem.Decimal doubleSystem.Double floatSystem.Single intSystem.Int32 unitSystem.UInt32 longSystem.Int64 ulongSystem.UInt64 objectSystem.Object shortSysytem.Int16 ushortSystem.UInt16 stringSystem.String
Type Cannot Be Changed Correct Incorrect int x = 5; x = “foo”; Compilation error
Strings must be in quotes Correct Incorrect string x = “foo bar”; string x = foo bar; TIP: If foo is declared as a variable of type string this is legal.
If / Else if (expression) { } else { } if (expression) { } else if (expression) { } else { } int x = 5; if (x > 5) { x = 10; } else { x = 0; } TIP: If you only have one line in your if block, you can skip the curly braces. int x = 5; If (x % 2 == 0) CallSomeMethod();
C# Is Strongly Typed Correct Incorrect z = 5; name = “Anders”; int z = 5; string name = “Anders”; You have to tell the compiler what type of data you are storing. shortintlong
Commenting Code // single lines /// for summaries /* */ block //int x = 5; /// /// This is what kicks the program off. /// static void Main(string[] args) { } /* this is a comment */
Operators C# uses standard mathematical operators +, -, /, *,, =, Expression operators && || == != Assignment operators =, +=, *=, -=, *=
Who can see what? Public means that anyone can access it Private means that only other members can access it public class Loan { public decimal LoanAmount { get; set; } public bool IsApproved { get; set; } private bool DocsCompleted { get; set; } }
Static Keyword Can be used with Fields Methods, Properties Operators Events Constructors Cannot be used with indexers and desconstructors
Static Keyword Referenced through the type not the instance
Loops Foreach loops For loops While loops foreach (var item in collection) { Console.WriteLine(item.Property); } for (int i = 0; i < length; i++) { Console.WriteLine(i); } while (expression) // x < 5 { } TIP: Use code snippets to stub these out.
(section not finished)
Nullable Types
Collections Arrays ArrayList List Generic collections
Generics
Value Type vs Reference Type
Reading / Writing Files
LINQ
XML
Handling Exceptions Try / Catch / Finally
Regular Expressions
Reflection