Download presentation
Presentation is loading. Please wait.
Published byFrancis Poole Modified over 9 years ago
1
How to be a C# ninja in 10 easy steps. Benjamin Day
2
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
3
Online courses at Pluralsight.com
5
Why did I write this talk?
6
TOP 10 THINGS
7
The List. 1. Be humble 2. Object- orientation 3. Write less code 4. Value Types vs. Reference Types 5. Exceptions 6. Generics 7. Collections 8. IDisposable, using, & garbage collection 9. LINQ 10. Lambda Expressions 11. Async & Await
8
Some extras. 11. Virtual, override, & new() 12. Tune out the “static” 13. Partial classes & methods 14. Covarience contravariance 15. Named parameters 16. Optional parameters 17. Dynamic keyword
9
#1: Be humble.
10
Be humble. Software is complex. We developers… –…want to please –…think we’re awesome –…almost always underestimate
11
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.
12
Tip for managers. Your devs are afraid of you.
13
Tip for executives. Your devs are afraid of you. Your project managers are afraid of you. Your project managers are afraid of the devs.
14
“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.
15
#2: Remember Object-Orientation
16
Object-Oriented Principles The 4 tenets. What are they? Encapsulation Polymorphism Inheritance Abstraction
17
#3: Write less code
18
Save some typing.
19
Less is more. (as long as it’s readable)
20
Everything you write has to be maintained.
21
Whatever has to be maintained is “inventory.”
22
var vs. object
23
Auto-Implemented Properties
24
Read-Only Auto-Implemented Properties
25
…and now I’m going to contradict myself.
26
Avoid ternary operators
27
#4: Value types vs. reference types
28
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
29
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
30
#5: Exception Handling
31
Throw vs. throw ex throw;throw ex;
32
#6: Generics
33
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
34
ViewModelField DomainObjectManager
35
#7: Collections
36
What is a Collection? Data type for organizing lists of objects Similar to an array
37
Part of the.NET framework 5 namespaces
38
Array vs. List Array vs. List Array Size defined when created List Automatically expands
39
ArrayList vs. List ArrayList vs. List ArrayList Not type-safe Everything is an object Watch out for boxing / unboxing List Type-safe Everything must be an instance of T
40
#8: IDisposable, Using, and Garbage Collection
41
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.
42
IDisposable
43
IDisposable: Custom Cleanup Gets called when the Garbage Collector is disposing your object Add custom logic For example, close any open database connections
45
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
46
Wrap database connections in ‘using’ blocks Most database classes implement IDisposable
47
Why should you wrap calls to database object in ‘using’ statements?
48
But there’s a catch.
49
The Garbage Collector doesn’t call IDisposable.Dispose().
50
If you want to be bulletproof…
51
…implement IDisposable along with a Destructor.
52
#9: LINQ
53
LINQ Language-Integrated Query Enables SQL-like querying of objects via IEnumerable
54
LINQ Stuff Operators select from where orderby Useful functions FirstOrDefault() First() Min() Max() Count() Skip() Take() Reverse() Sum()
55
(Code Demo: LinqSample.cs)
56
#10: Lambda expressions
57
What’s a “lambda expression”? Anonymous functions Helpful for delegates
58
(Code Demos: LambdaExpressionSample.cs & LambdaExpressionForm.cs)
59
#11: Async & Await
60
Async programming is a pain in the donkey.
61
I complain about discuss this in a couple of articles.
62
http://msdn.microsoft.com/en- us/magazine/jj658977.aspx http://msdn.microsoft.com/en- us/magazine/jj658977.aspx
63
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.
64
Who cares? Since they don’t share the same call stack you can’t… …return values using the return keyword …throw an exception –(DOH!!!)
65
Async & Await Async is all over the place in Windows Phone, Windows Store / WinRT, and Silverlight Async, Await, Task, Task help take the pain out of async programming. (New for Visual Studio 2012 &.NET 4.5)
66
How does async & await work?
67
Basically, async & await injects a lot of glue to knit the calls together.
68
Additional Reading Essential C# 5.0 by Mark Michaelis Great overview of the language http://amzn.com/032187 7586 http://amzn.com/032187 7586
69
Additional Reading CLR via C# by Jeffrey Richter What’s going on under the hood of C# and the.NET Framework http://amzn.com/073566 7454 http://amzn.com/073566 7454
70
The List. 1. Be humble 2. Object- orientation 3. Write less code 4. Value Types vs. Reference Types 5. Exceptions 6. Generics 7. Collections 8. IDisposable, using, & garbage collection 9. LINQ 10. Lambda Expressions 11. Async & Await
71
Thank you. http://www.benday.comhttp://www.benday.com | benday@benday.combenday@benday.com
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.