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

How to be a C# ninja in 10 easy steps

  • Upload
    hogan

  • View
    49

  • Download
    0

Embed Size (px)

DESCRIPTION

How to be a C# ninja in 10 easy steps. Benjamin Day. Benjamin Day. Brookline, MA Consultant, Coach, & Trainer Microsoft MVP for Visual Studio ALM Team Foundation Server, Software Testing, Scrum , Software Architecture Scrum.org Classes Professional Scrum Developer (PSD) - PowerPoint PPT Presentation

Citation preview

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

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

Scrum, Software Architecture• 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

Online courses at Pluralsight.com

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

Why did I write this talk?

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

TOP 10 THINGS

Page 7: 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

Expressions11. Async & Await

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

#1: Be humble.

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

Be humble.• Software is complex.• We developers…

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

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

Tips.• Keep it simple.

• Expect to make mistakes.

• Not everyone will understand your abstractions.

• Favor maintainability over “slickness”.

• Write unit tests. Lots of unit tests.

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

Tip for managers.• Your devs are afraid of you.

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

Tip for executives.• Your devs are afraid of you.

• Your project managers are afraid of you.

• Your project managers are afraid of the devs.

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

“C# doesn’t do Xyz. C# sucks.”• Lesson I learned.

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

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

#2: Remember Object-Orientation

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

Object-Oriented Principles• The 4 tenets. What are they?

• Encapsulation• Polymorphism• Inheritance• Abstraction

INTERVIEW

QUESTION!

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

#3: Write less code

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

Save some typing.

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

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

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

Everything you write has to be maintained.

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

Whatever has to be maintained is “inventory.”

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

var vs. object

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

Auto-Implemented Properties

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

Read-Only Auto-Implemented Properties

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

…and now I’m going to contradict myself.

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

Avoid ternary operators

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

#4: Value types vs. reference types

Page 27: 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 28: 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 29: How to be a C# ninja  in 10 easy  steps

#5: Exception Handling

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

Throw vs. throw exthrow; throw ex;

INTERVIEW

QUESTION!

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

#6: Generics

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

What are generics?• Syntax that allows you to use similar

functionality with different types in a type-safe way

• Implementation is the same

• Data types are different

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

• ViewModelField<T>• DomainObjectManager<T>

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

#7: Collections

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

What is a Collection?• Data type for organizing lists of objects

• Similar to an array

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

• Part of the .NET framework

• 5 namespaces

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

Array vs. List<T>Array• Size defined when

created

List<T>• Automatically expands

Page 38: 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 39: How to be a C# ninja  in 10 easy  steps

#8: IDisposable, Using, and

Garbage Collection

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

What is Garbage Collection?• Background process in .NET • Determines when an object is not needed• Deletes it “automagically”• Frees up memory

• You worry much less about memory management.

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

IDisposable

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

IDisposable: Custom Cleanup• Gets called when the Garbage Collector is

disposing your object• Add custom logic

• For example, close any open database connections

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

What does the ‘using’ statement do?• Wraps instance

of IDisposable for block of code

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

INTERVIEW

QUESTION!

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

Wrap database connections in ‘using’ blocks• Most database classes implement

IDisposable

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

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

INTERVIEW

QUESTION!

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

But there’s a catch.

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

The Garbage Collector doesn’t call IDisposable.Dispose().

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

If you want to be bulletproof…

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

…implement IDisposable along with a Destructor.

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

#9: LINQ

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

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

IEnumerable<T>

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

LINQ StuffOperators• select• from• where• orderby

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

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

(Code Demo: LinqSample.cs)

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

#10:Lambda expressions

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

What’s a “lambda expression”?• Anonymous functions• Helpful for delegates

INTERVIEW

QUESTION!

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

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

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

#11: Async & Await

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

Async programming is a pain in thedonkey.

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

I complain about discuss this in a

couple of articles.

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

http://msdn.microsoft.com/en-us/magazine/jj658977.aspx

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

Why?• Async calls are really 3 calls.

• 1) the initiator

• 2) the do-er

• 3) the return handler

• They don’t share the same call stack.

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

Who cares?• Since they don’t share the same call stack

you can’t…

• …return values using the return keyword

• …throw an exception– (DOH!!!)

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

Async & Await• Async is all over the place in

Windows Phone, Windows Store / WinRT, and Silverlight

• Async, Await, Task, Task<TResult>help take the pain out of async programming.

• (New for Visual Studio 2012 & .NET 4.5)

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

How does async & await work?

INTERVIEW

QUESTION!

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

Basically, async & await injects a lot of glue to knit the calls together.

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

Additional Reading• Essential C# 5.0

by Mark Michaelis

• Great overview of the language

• http://amzn.com/0321877586

Page 68: 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

• http://amzn.com/0735667454

Page 69: 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

Expressions11. Async & Await

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

Thank you.

http://www.benday.com | [email protected]