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. 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


How to be a C# ninja in 10 easy steps

How to be a C# ninja in 10 easy stepsBenjamin Day

About: Benjamin DayBrookline, MAConsultant, Coach, TrainerMicrosoft MVP for Visual Studio ALMTeam Foundation Server, Software Testing, Architecture, Windows AzureTechEd, VSLive, DevTeachVisual Studio Magazine, Redmond Developer ClassesProfessional Scrum Developer (PSD)Professional Scrum Foundations (PSF)

Professional Scrum at Scrum.orgProfessional Scrum Product Owner Professional Scrum FoundationsProfessional Scrum MasterProfessional Scrum Developer.NET or JavaProduct OwnersExecutivesScrum MastersArchitectsBusiness AnalystsDB SpecialistsDesignersDevelopersTestersEveryone 1993-2011, All Rights ReservedWhy did I write this talk?Top 10 thingsThe List.Be humbleObject-orientationWrite less codeValue Types vs. Reference TypesExceptions

GenericsCollectionsIDisposable, using, & garbage collectionLINQLambda Expressions.NET 4.5async / awaitBonus Item: Coming soonSome extras.Virtual, override, & new()Tune out the staticPartial classes & methodsCovarience contravarianceNamed parametersOptional parametersDynamic keywordBe humble.Software is complex.We developerswant to pleasethink were awesomealmost always underestimate

Be humble.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.Lesson I learned.

Theres a reason its built that way.Dont fight it. Embrace it.Learn from the design.C# doesnt do Xyz. C# sucks.Remember Object-Orientation4 tenets


Object-Oriented PrinciplesINTERVIEW QUESTION!Write less codeSave some typing.Less is more.(as long as its readable)Everything you write has to be maintained.var vs. object

Auto-Implemented Properties

Read-Only Auto-Implemented Properties

Avoid ternary operators

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 variable

INTERVIEW QUESTION!Exception HandlingThrow vs. throw exthrow;throw ex;

INTERVIEW QUESTION!(code demo)genericsSyntax that allows you to use similar functionality with different types in a type-safe wayImplementation is the sameData types are differentWhat are generics?ViewModelFieldDomainObjectManagerCollectionsData type for organizing lists of objectsSimilar to an array

What is a Collection?Part of the .NET framework5 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!Idisposable, Using, andGarbage CollectionBackground process in .NET Determines when an object is not neededDeletes it automagicallyFrees up memory

You worry much less about memory management.What is Garbage Collection?IDisposable

Gets called when the Garbage Collector is disposing your objectAdd custom logic

For example, close any open database connectionsIDisposable: Custom Cleanup

Wraps instance of IDisposable for block of code Instance is disposed automatically at the end of the code blockWhat does the using statement do?

INTERVIEW QUESTION!Most database classes implement IDisposableWrap database connections in using blocks

Why should you wrap calls to database object in using statements?INTERVIEW QUESTION!Hint: Wrap Enterprise Library Data Access Blockin using() {}LINQLanguage-Integrated QueryEnables SQL-like querying of objects via IEnumerable

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

(Code Demo: LinqSample.cs)Lambda expressionsAnonymous functionsHelpful for delegatesWhats a lambda expression?INTERVIEW QUESTION!(Code Demos: LambdaExpressionSample.cs & LambdaExpressionForm.cs)Additional ReadingEssential C# 4.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 ExpressionsThank you. |