Download presentation
Presentation is loading. Please wait.
1
Keith Elder Microsoft MVP http://keithelder.net/blog/
2
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
4
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
5
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)
6
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; } }
7
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)
8
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
9
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(); }
10
Putting it all together namespace ConsoleApplication { class Program { static void Main() { Loan myLoan = new Loan(); myLoan.LoanAmount = 1000000; } Using statements specify which Namespaces to use to resolve classes. using System; using Vendor;
13
All Lines Must End in ; Correct Incorrect int x = 5;int x = 5
14
Variables Must Declare Type Correct Incorrect int x = 5;x = 5;
15
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
16
Type Cannot Be Changed Correct Incorrect int x = 5; x = “foo”; Compilation error
17
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.
18
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();
20
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
21
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 */
23
Operators C# uses standard mathematical operators +, -, /, *,, =, Expression operators && || == != Assignment operators =, +=, *=, -=, *= http://msdn.microsoft.com/en-us/library/6a71f45d.aspx
24
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; } }
25
Static Keyword Can be used with Fields Methods, Properties Operators Events Constructors Cannot be used with indexers and desconstructors
26
Static Keyword Referenced through the type not the instance
28
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.
30
(section not finished)
31
Nullable Types
32
Collections Arrays ArrayList List Generic collections
33
Generics
34
Value Type vs Reference Type
35
Reading / Writing Files
36
LINQ
37
XML
38
Handling Exceptions Try / Catch / Finally
39
Regular Expressions
40
Reflection
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.