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

Keith Elder Microsoft MVP

Jan 02, 2016

Download

Documents

Brian Ford
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Keith Elder Microsoft MVP

A Programmers Introduction to C#

Keith ElderMicrosoft MVP

http://keithelder.net/blog/

Page 2: Keith Elder Microsoft MVP

AssumptionsI assume you

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

Variables Loops Arrays

Can type over 40 words per minute

Page 3: Keith Elder Microsoft MVP

Demo – Simple Program

Page 4: Keith Elder Microsoft MVP

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

Page 5: Keith Elder Microsoft MVP

Breaking Down C# - NamespaceNamespaceAbstract container

providing organization to items.

Typically organized by:CompanyProductTeamSub-TeamEtc.

Namespace

Class

Method1

Method2

Properties(data)

Page 6: Keith Elder Microsoft MVP

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; } }}

Page 7: Keith Elder Microsoft MVP

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)

Page 8: Keith Elder Microsoft MVP

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

Page 9: Keith Elder Microsoft MVP

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(); } }

Page 10: Keith Elder Microsoft MVP

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;

Page 11: Keith Elder Microsoft MVP

Demo – Our first class

Page 12: Keith Elder Microsoft MVP

C# Syntax

Page 13: Keith Elder Microsoft MVP

All Lines Must End in ;Correct Incorrect

int x = 5; int x = 5

Page 14: Keith Elder Microsoft MVP

Variables Must Declare TypeCorrect Incorrect

int x = 5; x = 5;

Page 15: Keith Elder Microsoft MVP

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

Page 16: Keith Elder Microsoft MVP

Type Cannot Be ChangedCorrect Incorrect

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

Compilation error

Page 17: Keith Elder Microsoft MVP

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.

Page 18: Keith Elder Microsoft MVP

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();

Page 19: Keith Elder Microsoft MVP

Demo – Fun with varibles

Page 20: Keith Elder Microsoft MVP

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 */

Page 21: Keith Elder Microsoft MVP

Demo - Comments

Page 22: Keith Elder Microsoft MVP

OperatorsC# uses standard mathematical operators

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

&&||==!=

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

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

Page 23: Keith Elder Microsoft MVP

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; } }

Page 24: Keith Elder Microsoft MVP

Static KeywordCan be used with

FieldsMethods,PropertiesOperatorsEventsConstructorsCannot be used with indexers and

desconstructors

Page 25: Keith Elder Microsoft MVP

Static KeywordReferenced through the type not the

instance

Page 26: Keith Elder Microsoft MVP

Demo – Keep your privates private

Page 27: Keith Elder Microsoft MVP

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.

Page 28: Keith Elder Microsoft MVP

Demo - Looping in C#

Page 29: Keith Elder Microsoft MVP

A Primer on Object Oriented Programming(section not finished)