700 likes | 836 Views
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)
E N D
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) • Professional Scrum Foundations (PSF) • www.benday.com, benday@benday.com, @benday
The List. • Be humble • Object-orientation • Write less code • Value Types vs. Reference Types • Exceptions • Generics • Collections • IDisposable, using, & garbage collection • LINQ • Lambda Expressions • Async & Await
Be humble. • Software is complex. • We developers… • …want to please • …think we’re awesome • …almost 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# 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.
Object-Oriented Principles • The 4 tenets. What are they? • Encapsulation • Polymorphism • Inheritance • Abstraction INTERVIEW QUESTION!
Whuh? Value Types Reference Types Object types Stored in memory “heap” Variables are “pointers” to memory location • Non-object types • Stored in memory “stack” • int, long, char, byte, etc. • float, double • decimal • bool • User-defined • Structs • Enumerations INTERVIEW QUESTION!
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!
Throw vs. throw ex throw; throw ex; INTERVIEW QUESTION!
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
ViewModelField<T> • DomainObjectManager<T>
What is a Collection? • Data type for organizing lists of objects • Similar to an array
Part of the .NET framework • 5 namespaces
Array vs. List<T> Array List<T> Automatically expands • Size defined when created
ArrayList vs. List<T> ArrayList List<T> Type-safe Everything must be an instance of T • Not type-safe • Everything is an object • Watch out for boxing / unboxing INTERVIEW QUESTION!
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.
IDisposable: Custom Cleanup • Gets called when the Garbage Collector is disposing your object • Add 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 block INTERVIEW QUESTION!
Wrap database connections in ‘using’ blocks • Most database classes implement IDisposable
Why should you wrap calls to database object in ‘using’ statements? INTERVIEW QUESTION!