how to be a c# ninja in 10 easy steps

55
How to be a C# ninja in 10 easy steps Benjamin Day

Upload: otylia

Post on 23-Feb-2016

56 views

Category:

Documents


0 download

DESCRIPTION

How to be a C# ninja in 10 easy steps. Benjamin Day. About: Benjamin Day. Brookline, MA Consultant , Coach, Trainer Microsoft MVP for Visual Studio ALM Team Foundation Server, Software Testing, Architecture, Windows Azure TechEd , VSLive , DevTeach - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: How to be a C# ninja  in 10 easy steps

How to be a C# ninja in 10 easy steps

Benjamin Day

Page 2: How to be a C# ninja  in 10 easy steps

About: Benjamin Day• Brookline, MA• Consultant, Coach, Trainer• Microsoft MVP for Visual Studio ALM• Team Foundation Server, Software Testing,

Architecture, Windows Azure• TechEd, VSLive, DevTeach• Visual Studio Magazine, Redmond Developer News• Scrum.org Classes

– Professional Scrum Developer (PSD)– Professional Scrum Foundations (PSF)

• www.benday.com• [email protected]• @benday

Page 3: How to be a C# ninja  in 10 easy steps

© 1993-2011 Scrum.org, All Rights Reserved

Professional Scrum at Scrum.org

Professional Scrum

Product Owner

Professional Scrum Foundations

Professional Scrum Master

Professional Scrum

Developer.NET or Java

Product OwnersExecutives Scrum Masters

ArchitectsBusiness Analysts

DB SpecialistsDesigners

DevelopersTesters

Everyone

Page 4: How to be a C# ninja  in 10 easy steps

Why did I write this talk?

Page 5: How to be a C# ninja  in 10 easy steps

TOP 10 THINGS

Page 6: How to be a C# ninja  in 10 easy steps

The List.

1. Be humble2. Object-

orientation3. Write less code4. Value Types vs.

Reference Types5. Exceptions

6. Generics7. Collections8. IDisposable,

using, & garbage collection

9. LINQ10. Lambda

Expressions

Page 7: How to be a C# ninja  in 10 easy steps

• .NET 4.5• async / await

Bonus Item: Coming soon

Page 8: How to be a C# ninja  in 10 easy steps

BE HUMBLE.

Page 9: How to be a C# ninja  in 10 easy steps

• Software is complex.• We developers…

– …want to please– …think we’re awesome– …almost always underestimate

Be humble.

Page 10: How to be a C# ninja  in 10 easy steps

• Keep it simple.• Expect to make mistakes.• Not everyone will understand your

abstractions.• Favor maintainability over “slickness”.

• Write unit tests. Lots of unit tests.

Tips.

Page 11: How to be a C# ninja  in 10 easy steps

• Lesson I learned.

• There’s a reason it’s built that way.• Don’t fight it. • Embrace it.• Learn from the design.

“C# doesn’t do Xyz. C# sucks.”

Page 12: How to be a C# ninja  in 10 easy steps

REMEMBER OBJECT-ORIENTATION

Page 13: How to be a C# ninja  in 10 easy steps

• 4 tenets

• Encapsulation• Polymorphism• Inheritance• Abstraction

Object-Oriented Principles

INTERVIEW

QUESTION!

Page 14: How to be a C# ninja  in 10 easy steps

WRITE LESS CODE

Page 15: How to be a C# ninja  in 10 easy steps

Save some typing.

Page 16: How to be a C# ninja  in 10 easy steps

Less is more.(as long as it’s readable)

Page 17: How to be a C# ninja  in 10 easy steps

Everything you write has to be maintained.

Page 18: How to be a C# ninja  in 10 easy steps

var vs. object

Page 19: How to be a C# ninja  in 10 easy steps

Auto-Implemented Properties

Page 20: How to be a C# ninja  in 10 easy steps

Read-Only Auto-Implemented Properties

Page 21: How to be a C# ninja  in 10 easy steps

Avoid ternary operators

Page 22: How to be a C# ninja  in 10 easy steps

VALUE TYPES VS. REFERENCE TYPES

Page 23: How to be a C# ninja  in 10 easy steps

Whuh?

Value Types• Non-object types• Stored in memory

“stack”• int, long, char, byte, etc.• float, double• decimal• bool• User-defined

– Structs– Enumerations

Reference Types• Object types• Stored in memory

“heap”• Variables are

“pointers” to memory location

INTERVIEW

QUESTION!

Page 24: How to be a C# ninja  in 10 easy steps

Boxing and Unboxing

• Boxing– Process of

wrapping a value type in an object reference

• Unboxing– Converting a

boxed value type object back into an value type variable

INTERVIEW

QUESTION!

Page 25: How to be a C# ninja  in 10 easy steps

EXCEPTION HANDLING

Page 26: How to be a C# ninja  in 10 easy steps

Throw vs. throw ex

throw; throw ex;

INTERVIEW

QUESTION!

Page 27: How to be a C# ninja  in 10 easy steps

(code demo)

Page 28: How to be a C# ninja  in 10 easy steps

GENERICS

Page 29: How to be a C# ninja  in 10 easy steps

• Syntax that allows you to use similar functionality with different types in a type-safe way

• Implementation is the same• Data types are different

What are generics?

Page 30: How to be a C# ninja  in 10 easy steps

• ViewModelField<T>• DomainObjectManager<T>

Page 31: How to be a C# ninja  in 10 easy steps

COLLECTIONS

Page 32: How to be a C# ninja  in 10 easy steps

• Data type for organizing lists of objects• Similar to an array

What is a Collection?

Page 33: How to be a C# ninja  in 10 easy steps

• Part of the .NET framework

• 5 namespaces

Page 34: How to be a C# ninja  in 10 easy steps

Array vs. List<T>

Array• Size defined when

created

List<T>• Automatically

expands

Page 35: How to be a C# ninja  in 10 easy steps

ArrayList vs. List<T>

ArrayList• Not type-safe• Everything is an

object• Watch out for boxing /

unboxing

List<T>• Type-safe• Everything must be

an instance of T

INTERVIEW

QUESTION!

Page 36: How to be a C# ninja  in 10 easy steps

IDISPOSABLE, USING, ANDGARBAGE COLLECTION

Page 37: How to be a C# ninja  in 10 easy steps

• Background process in .NET • Determines when an object is not

needed• Deletes it “automagically”• Frees up memory

• You worry much less about memory management.

What is Garbage Collection?

Page 38: How to be a C# ninja  in 10 easy steps

IDisposable

Page 39: How to be a C# ninja  in 10 easy steps

• Gets called when the Garbage Collector is disposing your object

• Add custom logic

• For example, close any open database connections

IDisposable: Custom Cleanup

Page 40: How to be a C# ninja  in 10 easy steps
Page 41: How to be a C# ninja  in 10 easy steps

• Wraps instance of IDisposable for block of code

• Instance is disposed automatically at the end of the code block

What does the ‘using’ statement do?

INTERVIEW

QUESTION!

Page 42: How to be a C# ninja  in 10 easy steps

• Most database classes implement IDisposable

Wrap database connections in ‘using’ blocks

Page 43: How to be a C# ninja  in 10 easy steps

Why should you wrap calls to database object in ‘using’ statements?

INTERVIEW

QUESTION!

Page 44: How to be a C# ninja  in 10 easy steps

Hint: Wrap Enterprise Library Data Access

Blockin using() {}

Page 45: How to be a C# ninja  in 10 easy steps

LINQ

Page 46: How to be a C# ninja  in 10 easy steps

• Language-Integrated Query• Enables SQL-like querying of objects via

IEnumerable<T>

LINQ

Page 47: How to be a C# ninja  in 10 easy steps

LINQ Stuff

Operators• select• from• where• orderby

Useful functions• FirstOrDefault()• First()• Min()• Max()• Count()• Skip()• Take()• Reverse()• Sum()

Page 48: How to be a C# ninja  in 10 easy steps

(Code Demo: LinqSample.cs)

Page 49: How to be a C# ninja  in 10 easy steps

LAMBDA EXPRESSIONS

Page 50: How to be a C# ninja  in 10 easy steps

• Anonymous functions• Helpful for delegates

What’s a “lambda expression”?

INTERVIEW

QUESTION!

Page 51: How to be a C# ninja  in 10 easy steps

(Code Demos: LambdaExpressionSample.cs & LambdaExpressionForm.cs)

Page 52: How to be a C# ninja  in 10 easy steps

Additional Reading

• Essential C# 4.0

by Mark Michaelis

• Great overview of the language

Page 53: How to be a C# ninja  in 10 easy steps

Additional Reading

• CLR via C#by Jeffrey Richter

• What’s going on under the hood of C# and the .NET Framework

Page 54: How to be a C# ninja  in 10 easy steps

The List.

1. Be humble2. Object-

orientation3. Write less code4. Value Types vs.

Reference Types5. Exceptions

6. Generics7. Collections8. IDisposable,

using, & garbage collection

9. LINQ10. Lambda

Expressions