Tips and Tricks To Improve Code Readability in C#

1dfad8107453e0f493ff1a6a8e48d503?s=47 Jaime
June 10, 2012

Tips and Tricks To Improve Code Readability in C#

1dfad8107453e0f493ff1a6a8e48d503?s=128

Jaime

June 10, 2012
Tweet

Transcript

  1. P2P Code Academy Becoming a C# Warrior-Poet or Tips and

    Tricks To Improve Code Readability in C# 4 30th March
  2. C#

  3. Outline • Object Initializers • Named Arguments • Extension Methods

    • LINQ
  4. Ye Olde Classic Constructor

  5. Object Initializers vs Constructors

  6. Object Initializers Uses Default Constructor!

  7. Object Initializers. Benefits • Improved Readability • Less boiler-plate code

    – Don’t even need a constructor!
  8. DEMO OBJECT INITIALIZERS

  9. Collection Initializers

  10. Named Arguments • Explicitly name method arguments

  11. Named Arguments. Benefits What the heck is true?

  12. Named Arguments. Benefits

  13. Extension Methods Add methods to existing types: • No need

    to create derived type • No need to re-compile existing type • No need to modify original type • Just static methods in disguise!
  14. Extension Methods. Syntax Special use of keyword this Look like

    instance methods!
  15. Extension Methods Add a method to a primitive type Adding

    this method to the string class
  16. Extension Methods Add a method to a primitive type Before

    After
  17. DEMO EXTENSION METHODS

  18. Extension Methods Add a method to a compiled custom type

  19. LINQ Query language within C# and VB.NET • Very Expressive

    • Extensible • Integrated – LINQ to Objects – LINQ to SQL – LINQ to XML – LINQ to Entities
  20. LINQ. Example Filtering a collection (People older than 30)

  21. LINQ. Example Query Syntax Method Chain Syntax

  22. DEMO LINQ

  23. Summary • Object Initializers • Named Arguments • Extension Methods

    • LINQ
  24. If you put it all together... Sonnet 138. The Passionate

    Pilgrim C# version while (myLove.Swears(that: ”She is made of truth”)) { do I.Believe(her); while (she.Lies()) she.Might(s => s.Think(I == ”some untutored youth”)); she.Might(s => s.Think(I == ”unskillful”)); ... }
  25. C#

  26. If you want to know more…

  27. References • Object and Collection Initializers http://bit.ly/csharp-object-collection-initializer • Named Arguments

    http://bit.ly/csharp-named-arguments • Extension Methods http://bit.ly/csharp-extension-methods • LINQ http://bit.ly/linq-language-integrated-query
  28. Thank you