29
A Programmers Introduction to C# Keith Elder Microsoft MVP http://keithelder.net/blog/

Keith Elder Microsoft MVP

Embed Size (px)

Citation preview

A Programmers Introduction to C#

Keith ElderMicrosoft MVP

http://keithelder.net/blog/

AssumptionsI assume you

Have 1 – 2 years of programming experienceUnderstand the basic concepts of

Variables Loops Arrays

Can type over 40 words per minute

Demo – Simple Program

Very Simple ProgramA 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# - NamespaceNamespaceAbstract container

providing organization to items.

Typically organized by:CompanyProductTeamSub-TeamEtc.

Namespace

Class

Method1

Method2

Properties(data)

Breaking Down C# - Namespacenamespace 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# - ClassA 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 / Propertiesnamespace 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# - MethodThink 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 = 1000000; } }}

Using statements specify which Namespaces to use to resolve classes.

using System;using Vendor;

Demo – Our first class

C# Syntax

All Lines Must End in ;Correct Incorrect

int x = 5; int x = 5

Variables Must Declare TypeCorrect Incorrect

int x = 5; x = 5;

Supported C# TypesC# type keywords .Net Framework Typebool System.Boolean

byte System.Byte

sbyte System.Sbyte

char System.Char

decimal System.Decimal

double System.Double

float System.Single

int System.Int32

unit System.UInt32

long System.Int64

ulong System.UInt64

object System.Object

short Sysytem.Int16

ushort System.UInt16

string System.String

Type Cannot Be ChangedCorrect Incorrect

int x = 5; int x = 5;x = “foo”;

Compilation error

Strings must be in quotesCorrect Incorrect

string x = “foo bar”;

string x = foo bar;

TIP: If foo is declared as a variable of type string this is legal.

If / Elseif (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();

Demo – Fun with varibles

Commenting Code// single lines/// for

summaries/* */ block

//int x = 5;

/// <summary> /// This is what kicks the program off. /// </summary> /// <param name="args"></param> static void Main(string[] args) { }

/* this is a comment */

Demo - Comments

OperatorsC# uses standard mathematical operators

+, -, /, *, <, >, <=, >=, Expression operators

&&||==!=

Assignment operators=, +=, *=, -=, *=

http://msdn.microsoft.com/en-us/library/6a71f45d.aspx

Who can see what?Public means that anyone can access itPrivate 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 KeywordCan be used with

FieldsMethods,PropertiesOperatorsEventsConstructorsCannot be used with indexers and

desconstructors

Static KeywordReferenced through the type not the

instance

Demo – Keep your privates private

LoopsForeach loopsFor loopsWhile 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.

Demo - Looping in C#

A Primer on Object Oriented Programming(section not finished)