Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Tips and Tricks To Improve Code Readability in C#
Search
Jaime
June 10, 2012
Programming
5
980
Tips and Tricks To Improve Code Readability in C#
Jaime
June 10, 2012
Tweet
Share
More Decks by Jaime
See All by Jaime
Mastering the Arcane Art of JavaScriptmancy @ DevSum
vintharas
1
840
From Angular 1 to Angular 2
vintharas
0
2.1k
Mastering The Arcane Art of JavaScriptmancy - Swetugg Edition
vintharas
1
1.5k
Mastering the Arcane Art of JavaScript-mancy
vintharas
0
3.8k
Level-Up Your Code-Fu With Chuck Norris (and ReSharper, VsVim and NCrunch)
vintharas
0
1.4k
The Web Of The Future - Knockout Components
vintharas
1
1.2k
Introduction to RavenDB
vintharas
0
1.3k
The Path to Jedi Text Editing With VIM - Padawan Edition
vintharas
0
1.8k
TypeScript
vintharas
3
350
Other Decks in Programming
See All in Programming
ARA Ansible for the teams
kksat
0
150
sappoRo.R #12 初心者セッション
kosugitti
0
250
Unity Android XR入門
sakutama_11
0
160
CNCF Project の作者が考えている OSS の運営
utam0k
6
710
Pythonでもちょっとリッチな見た目のアプリを設計してみる
ueponx
1
550
『GO』アプリ データ基盤のログ収集システムコスト削減
mot_techtalk
0
120
How mixi2 Uses TiDB for SNS Scalability and Performance
kanmo
36
14k
Lottieアニメーションをカスタマイズしてみた
tahia910
0
130
プログラミング言語学習のススメ / why-do-i-learn-programming-language
yashi8484
0
130
苦しいTiDBへの移行を乗り越えて快適な運用を目指す
leveragestech
0
560
仕様変更に耐えるための"今の"DRY原則を考える / Rethinking the "Don't repeat yourself" for resilience to specification changes
mkmk884
0
110
一休.com のログイン体験を支える技術 〜Web Components x Vue.js 活用事例と最適化について〜
atsumim
0
450
Featured
See All Featured
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
29
1k
A Philosophy of Restraint
colly
203
16k
Practical Orchestrator
shlominoach
186
10k
A better future with KSS
kneath
238
17k
Producing Creativity
orderedlist
PRO
344
39k
Reflections from 52 weeks, 52 projects
jeffersonlam
348
20k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
21
2.5k
Building Applications with DynamoDB
mza
93
6.2k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.2k
Navigating Team Friction
lara
183
15k
The Pragmatic Product Professional
lauravandoore
32
6.4k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.3k
Transcript
P2P Code Academy Becoming a C# Warrior-Poet or Tips and
Tricks To Improve Code Readability in C# 4 30th March
C#
Outline • Object Initializers • Named Arguments • Extension Methods
• LINQ
Ye Olde Classic Constructor
Object Initializers vs Constructors
Object Initializers Uses Default Constructor!
Object Initializers. Benefits • Improved Readability • Less boiler-plate code
– Don’t even need a constructor!
DEMO OBJECT INITIALIZERS
Collection Initializers
Named Arguments • Explicitly name method arguments
Named Arguments. Benefits What the heck is true?
Named Arguments. Benefits
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!
Extension Methods. Syntax Special use of keyword this Look like
instance methods!
Extension Methods Add a method to a primitive type Adding
this method to the string class
Extension Methods Add a method to a primitive type Before
After
DEMO EXTENSION METHODS
Extension Methods Add a method to a compiled custom type
LINQ Query language within C# and VB.NET • Very Expressive
• Extensible • Integrated – LINQ to Objects – LINQ to SQL – LINQ to XML – LINQ to Entities
LINQ. Example Filtering a collection (People older than 30)
LINQ. Example Query Syntax Method Chain Syntax
DEMO LINQ
Summary • Object Initializers • Named Arguments • Extension Methods
• LINQ
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”)); ... }
C#
If you want to know more…
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
Thank you