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

  • Published on

  • View

  • Download

Embed Size (px)


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 Classes Professional Scrum Developer (PSD) - PowerPoint PPT Presentation


Session Title That is Really Long and Covers Two Lines

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

Benjamin DayBrookline, MAConsultant, Coach, & TrainerMicrosoft MVP for Visual Studio ALMTeam Foundation Server, Software Testing, Scrum, Software ClassesProfessional Scrum Developer (PSD)Professional Scrum Foundations (PSF),, @benday

Online courses at

Why did I write this talk?Top 10 things6The List.Be humbleObject-orientationWrite less codeValue Types vs. Reference TypesExceptions

GenericsCollectionsIDisposable, using, & garbage collectionLINQLambda ExpressionsAsync & AwaitSome extras.Virtual, override, & new()Tune out the staticPartial classes & methodsCovarience contravarianceNamed parametersOptional parametersDynamic keyword#1: Be humble.Be humble.Software is complex.We developerswant to pleasethink were awesomealmost always underestimate

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.

Tip for managers.Your devs are afraid of you.

Tip for executives.Your devs are afraid of you.

Your project managers are afraid of you.

Your project managers are afraid of the devs.C# doesnt do Xyz. C# sucks.Lesson I learned.

Theres a reason its built that way.Dont fight it. Embrace it.Learn from the design.#2: Remember Object-OrientationObject-Oriented PrinciplesThe 4 tenets. What are they?


INTERVIEW QUESTION!#3: Write less codeSave some typing.Less is more.(as long as its readable)Everything you write has to be maintained.Whatever has to be maintained is inventory.var vs. object

Auto-Implemented Properties

Read-Only Auto-Implemented Properties

and now Im going to contradict myself.Avoid ternary operators

#4: Value types vs. reference typesWhuh?Value TypesNon-object typesStored in memory stackint, long, char, byte, etc.float, doubledecimalboolUser-definedStructsEnumerationsReference TypesObject typesStored in memory heapVariables are pointers to memory location

INTERVIEW QUESTION!Boxing and UnboxingBoxingProcess of wrapping a value type in an object referenceUnboxingConverting a boxed value type object back into an value type variableINTERVIEW QUESTION!

#5: Exception HandlingThrow vs. throw exthrow;throw ex;INTERVIEW QUESTION!

#6: GenericsWhat 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 differentViewModelFieldDomainObjectManager#7: CollectionsWhat is a Collection?Data type for organizing lists of objects

Similar to an array

Part of the .NET framework

5 namespaces

Array vs. ListArraySize defined when createdListAutomatically expands

ArrayList vs. ListArrayListNot type-safeEverything is an objectWatch out for boxing / unboxingListType-safeEverything must be an instance of T

INTERVIEW QUESTION!#8: IDisposable, Using, andGarbage CollectionWhat is Garbage Collection?Background process in .NET Determines when an object is not neededDeletes it automagicallyFrees up memory

You worry much less about memory management.IDisposable

IDisposable: Custom CleanupGets called when the Garbage Collector is disposing your objectAdd custom logic

For example, close any open database connections

What does the using statement do?Wraps instance of IDisposable for block of code Instance is disposed automatically at the end of the code blockINTERVIEW QUESTION!

Wrap database connections in using blocksMost database classes implement IDisposable

Why should you wrap calls to database object in using statements?INTERVIEW QUESTION!But theres a catch.The Garbage Collector doesnt call IDisposable.Dispose().If you want to be bulletproofimplement IDisposable along with a Destructor.#9: LINQLINQLanguage-Integrated QueryEnables SQL-like querying of objects via IEnumerable

LINQ StuffOperatorsselectfromwhereorderbyUseful functionsFirstOrDefault()First()Min()Max()Count()Skip()Take()Reverse()Sum()

(Code Demo: LinqSample.cs)#10:Lambda expressionsWhats a lambda expression?Anonymous functionsHelpful for delegatesINTERVIEW QUESTION!(Code Demos: LambdaExpressionSample.cs & LambdaExpressionForm.cs)#11: Async & AwaitAsync programming is a pain in thedonkey.

I complain about discuss this in a couple of articles.

Why?Async calls are really 3 calls.

1) the initiator

2) the do-er

3) the return handler

They dont share the same call stack.Who cares?Since they dont share the same call stack you cant

return values using the return keyword

throw an exception(DOH!!!)

Async & AwaitAsync is all over the place in Windows Phone, Windows Store / WinRT, and Silverlight

Async, Await, Task, Taskhelp take the pain out of async programming.

(New for Visual Studio 2012 & .NET 4.5)How does async & await work?INTERVIEW QUESTION!Basically, async & await injects a lot of glue to knit the calls together.Additional ReadingEssential C# 5.0 by Mark Michaelis

Great overview of the language

Additional ReadingCLR via C#by Jeffrey Richter

Whats going on under the hood of C# and the .NET Framework

The List.Be humbleObject-orientationWrite less codeValue Types vs. Reference TypesExceptions

GenericsCollectionsIDisposable, using, & garbage collectionLINQLambda ExpressionsAsync & AwaitThank you. | benday@benday.com71