SlideShare a Scribd company logo
1 of 65
More “Little Wonders” of C#/.Net
                                    James Michael Hare
                                        2012 Visual C# MVP
                                      Application Architect
                                                 Scottrade
                                           August 3rd, 2012

  http://www.BlackRabbitCoder.net
  Twitter: @BlkRabbitCoder
Contact Information


Me:
 Blog: http://www.BlackRabbitCoder.net
 Twitter: @BlkRabbitCoder


Information on Scottrade Careers:
 http://jobs.scottrade.com
 Twitter: @scottradejobs
What are “Little Wonders”?


 The .NET Framework is full of “macro-sized” goodness that
  can help make our coding lives easier by automating
  common tasks.
 But, the .NET Framework also has a lot of smaller “micro-
  sized” tips and tricks that can improve code.
 Many developers know of most of these, but it is often
  surprising how many times newer developers don’t.
 This presentation picks up where the first “Little Wonders”
  presentation leaves off…
How do they help?


Basically, by employing these small items at the right
time, you can increase application:
  Readability – some of the wonders make code much
  more concise and easy to read.
  Maintainability – often goes hand and hand with
  readability, by removing ambiguity of the code, it is easier
  to maintain without introducing errors.
  Performance – a few of the little wonders can even help
  increase the performance of your code (depending on
  usage).
More Little Wonders


Compiler Candy               Strings
  Optional/Named Arguments     Joining
  Chained Constructors         Constructing
  Generic Constraints        Generic Delegates
  Anonymous types              Action
BCL Classes                    Func
  Enum                       Enumerable Static Methods
  Nullable<T>                  Empty
  Lazy<T>                      Repeat
  Tuple                      Collection Generators
  Interlocked                  ToArray, ToList, etc.
Optional Arguments


Optional arguments allow you to specify a default
value to be used if argument isn’t provided.
Default values must be compile-time constants:
  Numeric constant expressions
  String constant expressions
  null
Can be used to replace redundant overloads when
the only purpose is to default an argument.
This…




Can replace all of these…
Optional Arguments


The default is literally compiled in at call, so this:




Is, in effect, compiled into this:
Named Arguments


Defaults are positional, substituted from left to right:




If you want to “skip”, used named arguments:
Default Parameters


There are a couple of potential pitfalls to be aware of:
  If used across assemblies, can lead to subtle errors.
    If a default parameter is defined in one assembly, and used
    in another, must make sure both get recompiled if value
    changes.
  Default parameters are not inherited.
    Defaults established in an interface or base-class do not
    directly apply (and can be different from) the sub-class.
    Default depends on reference type, not the object type.
Chained Constructors


Constructors can call each other directly, which can
reduce the need for redundant code in overloads.
You could use optional arguments or initializers to
reign in overloads, but each have their limitations:
  Optional arguments must be set to compile-time
  constant expressions.
  Initializers aren’t suitable for immutable classes,
  readonly fields, get-only properties, or items whose
  construction may be “heavy”.
Consider this:
Can simplify by chaining constructors:
Generic Constraints


Generics allow you to make very powerful types and
algorithms, but can be a two-edged sword.
An unconstrained generic type parameter makes no
assumptions, giving you the lowest common
denominator of all types.
Supplying a constraint reduces the types that can be
used to realize the generic, but expands the things
that can be done inside the generic.
Generic Constraints


For example, how would unconstrained T handle null?




Unconstrained T can be compared to null, but this is
always false for value types (except Nullable<T>).
Unconstrained T cannot be assigned to null, though
can be assigned to default(T).
Generic Constraints


Or constrain T to be a value type (for example, since
Nullable<T> can only support value types…
Generic Constraints


Or what if we want to create a new unconstrained T
instance, or access any of T’s members?




Can’t, no way to know if unconstrained T supports
parameter-less construction or other members.
Generic Constraints


Supplying a constraint constrains the matching types, but
also expands what you can do with the generic:
  where T : struct – T must be a value type, must be first.
    Allows T to be used in other value generics (Nullable<T>, etc.).
  where T : class – T must be a reference type, must be first.
    Allows T to be assigned to null in generic.
  where T : U – T must be, inherit, or implement U
    Allows T to access any public members of U in generic.
  where T : new() – T must have a public parameter-less
  constructor, must be last
    Allows T to be constructed in generic.
Generic Constraints


Constraining T to things that implement IDictionary
and have parameter-less constructor:
Generic Constraints


Constraining T only value types that implement
IConvertible:
Anonymous Types


We can construct and initialize any type on the fly
using object initialization syntax:
Anonymous Types


What if we only need a type for a very localized
purpose?
  Why create the boilerplate code for a class that will only
  be used in a small, localized section?
  Most of these classes are simple POCOs, is it worth a full
  definition?
  If you need to use a type as a key (equality, hashing), it
  can get heavy.
    Must implement Equals() and GetHashCode() correctly.
How would we define UserDay, UserDayTotal?
Not as trivial as would think…
Anonymous Types


Anonymous types create throw-away types for you:
  Creates type based on name, type, order of properties.
  Creates suitable Equals() and GetHashCode().
  Syntax is like object initialization, just omit type name.
Anonymous Types

Now, much simpler…
 Don’t need to define UserDay or UserDayTotal
 Don’t need to maintain Equals() and GetHashCode().
 Doesn’t clutter up project with throw-away types.
Anonymous Types


A few things to be aware of:
  Type is generated at compile time based on properties’
  names, types, order – any deviation is a different type:




  Difficult to use outside of defined scope (which
  shouldn’t do anyway, defeats purpose).
The Enum Class


Enum is not only the base of enums, it also has
several useful static methods:
  IsDefined() – Determines if an enum constant exists with
  the given value.
  HasFlag() – Determines if a given set of bits is set, much
  easier to read than the typical bitwise AND.
  TryParse() – Parse a string value into enum value, much
  lighter than Parse().
The Enum Class


Use IsDefined() to see if a value exists




And HasFlag() simplifies flag testing:
The Enum Class


The TryParse() is much lighter than Parse(), especially
if you think you may not have a valid value:
The Nullable<T> Struct


You can indicate an optional value easily with null for
reference types, but value types always exist.
Sometimes, appropriate sentinel values don’t exist.
The Nullable<T> generic struct simulates optional
values for value types.
Nullable<T> has two key properties:
  HasValue – True if Value has been set to a value.
  Value – The value if set, throws if not.
The Nullable<T> Struct


Use when you have a value type which may or may
not contain a valid value.
For example, the DateTime struct’s default value isn’t
very meaningful, use DateTime? Instead.
The Nullable<T> Struct


We then must test to make sure it has a value by
using HasValue property.
Once we know the value exists, access Value to
retrieve it.
The Nullable<T> Struct


Nullable<T> allows some syntactical shortcuts:
  Nullable<T> can be abreviated T?



  Nullable<T> can be assigned to null



  Nullable<T> can be compared with null
The Nullable<T> Struct


Some things to watch out for:
  Nullable<T> doesn’t save space if value not specified,
  still stores a Value, it’s just not usable.
  Accessing Value when HasValue is not true is an error
  and will throw an exception.
  Math or logical operations between Nullable<T>
  wrapped numeric types (int, double, etc.) has caveats:
    Math with null yields null.
    Logical ordered comparison with null yields false.
The Lazy<T> Class


Some classes may be expensive to create, especially if
they are not always needed, in these cases lazy
instances are often used.
No need to create your own lazy-initialization logic
anymore, Lazy<T> does it all for you.
Can either call default constructor, or a generator.
Various thread-safety modes so you can choose the
level of performance and safety you require.
Lazy<T>


Constructor not called until Value accessed.
Lazy<T>


If a constructor isn’t accessible, or desired, you can
always use a factory method:
Lazy<T>


By default, Lazy<T> is thread-safe, but can choose:
The Tuples


Sometimes, you need to just throw together several
values to treat as a set of values.
Tuples allow you to do this in a generic way.
Similar to anonymous types, yet different:
  Both are immutable and have Equals(), GetHashCode()
  Anonymous types have named properties, but not type.
  Tuples have generic property names, but named type.
Tuple has static factory methods to easy creation.
The Tuples


Simple tuples are a single “layer” and have properties
numbered Item1…Item7:
Tuples


Get longer Tuples by using octuple, which has TRest:
Interlocked


Locking to safely increment a count is heavy:
Interlocked


Allows thread-safe, highly performant manipulation
of numeric values.
Much lighter than full locks for simple operations like:
  Increment – adds 1 to the interlocked value
  Decrement – subtracts 1 from the interlocked value
  Exchange – swaps two values
  Add – adds a value to the interlocked value
Interlocked


Using interlocked, we can do the same thing, without
heavy locks:
Strings of Repeated Char


Have you ever seen someone construct an array of
repeated char like this?




Instead, quickly create a string of a repeated
character using one of string’s constructors:
Joining Strings


Many times people write code to do this:




When they can simply do a Join():
Joining Strings


The string.Join() has a lot of power
  Can Join any array or IEnumerable<T>:




  Can join a variable argument list, including mixed types:
Generic Delegates


One of the oldest patterns in the OO playbook is to
create a class that is 99% complete except for one
anonymous “work” method, which is typically
abstract…
Like this…
Generic Delegates


Inheriting from a class simply to provide a missing
method per use is often overkill and poor coupling.
Can’t easily change behavior at runtime.
Bloats projects with classes that just override.
Generic Delegates


We can instead provide behavior through a delegate.
  Behavior can be added with no extra subclasses needed.
  There is very limited coupling between the delegate and
  the class/method using it.
  Behavior could be changed out at runtime.
But defining a new delegate each time gets ugly:


Generic delegates can be used for most delegate
needs.
The Action Delegate


Action is a generic family of delegates that take up to
16 arguments and return nothing:
  Action – takes zero arguments, returns nothing
  Action<T> - takes one argument, returns nothing.
  Action<T1, T2> - takes two arguments, returns nothing.
  …
  Action<T1…T16> - takes 16 arguments, returns nothing.
Action cannot be used for ref or out arguments.
The Func Delegate


Similar to Action, but Func returns a value:
  Func<TResult> – takes zero args, returns a TResult.
  Func<T, TResult> - takes one arg, returns a TResult.
  Func<T1, T2, TResult> - takes two args, returns a TResult.
  …
  Func<T1…T16, TResult> - takes 16 args, returns a TResult.
Func cannot be used for ref or out arguments.
Func<T, bool> is generally preferable to Predicate<T>.
Revised Consumer:
Empty Enumerables


How many times have you created an empty array or
List<T> to represent an empty sequence?
Empty Enumerable


Enumerable has a static method Empty<T>() that
generates a singleton empty IEnumerable<T>.
Enumerable Repeat


Allows you to create a sequence of the same item a
given number of times.
Doesn’t recreate the item each time, takes whatever
the parameter is and creates a sequence.
For example, to create 10 random numbers:
Collection Generators


Often times you’ll have a sequence and want to store
in a collection to:
  Avoid re-generating the sequence during multiple
  iterations.
  Convert the sequence from one sequence type to
  another.
  Pull the sequence into a collection to avoid deferred
  execution issues.
Collection Generators


There are several LINQ extension methods to
generate different collections from sequences:
  ToArray() – stores sequence in a T[].
  ToList() – stores sequence in a List<T>.
  ToDictionary() – transforms sequence to a
  Dictionary<TKey, TValue> given selectors for TKey and
  TValue.
  ToLookup() – transforms sequence to a Lookup<TKey,
  TValue> - similar to dictionary but can have repeated keys.
ToArray and ToList


ToArray() and ToList() are useful for:
  Remove effects of deferred execution.
  Convert a sequence to a particular kind.
ToDictionary


ToDictionary() converts a linear sequence to a
Dictionary that maps a single key to a single value.
  Keys can only exist once, multiple key instances throw.
  Must provide key selector, value selector optional.
ToLookup


ToLookup() is similar to ToDictionary(), but it maps a
key to a set of values (essentially a multi-map).
  Again must provide key selector, value optional.
Questions?
Platinum
Sponsors



Gold
Sponsors




Silver
Sponsors

More Related Content

What's hot

Comparable and comparator – a detailed discussion
Comparable and comparator – a detailed discussionComparable and comparator – a detailed discussion
Comparable and comparator – a detailed discussionDharmendra Prasad
 
Functional Effects - Part 1
Functional Effects - Part 1Functional Effects - Part 1
Functional Effects - Part 1Philip Schwarz
 
C# Programming: Fundamentals
C# Programming: FundamentalsC# Programming: Fundamentals
C# Programming: FundamentalsMahmoud Abdallah
 
IOS Swift language 2nd tutorial
IOS Swift language 2nd tutorialIOS Swift language 2nd tutorial
IOS Swift language 2nd tutorialHassan A-j
 
Coding Best Practices
Coding Best PracticesCoding Best Practices
Coding Best Practicesmh_azad
 
Commenting Best Practices
Commenting Best PracticesCommenting Best Practices
Commenting Best Practicesmh_azad
 
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2Philip Schwarz
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#Hawkman Academy
 
C#3.0 & Vb 9.0 Language Enhancments
C#3.0 & Vb 9.0 Language EnhancmentsC#3.0 & Vb 9.0 Language Enhancments
C#3.0 & Vb 9.0 Language Enhancmentstechfreak
 
Oo Design And Patterns
Oo Design And PatternsOo Design And Patterns
Oo Design And PatternsAnil Bapat
 
Java Building Blocks
Java Building BlocksJava Building Blocks
Java Building BlocksCate Huston
 
Swift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming languageSwift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming languageHossam Ghareeb
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)nirajmandaliya
 
JavaScripts internals #1
JavaScripts internals #1JavaScripts internals #1
JavaScripts internals #1Martin Pernica
 
Definitions of Functional Programming
Definitions of Functional ProgrammingDefinitions of Functional Programming
Definitions of Functional ProgrammingPhilip Schwarz
 
Java se 8 fundamentals
Java se 8 fundamentalsJava se 8 fundamentals
Java se 8 fundamentalsmegharajk
 

What's hot (20)

Comparable and comparator – a detailed discussion
Comparable and comparator – a detailed discussionComparable and comparator – a detailed discussion
Comparable and comparator – a detailed discussion
 
Functional Effects - Part 1
Functional Effects - Part 1Functional Effects - Part 1
Functional Effects - Part 1
 
Books
BooksBooks
Books
 
C# Programming: Fundamentals
C# Programming: FundamentalsC# Programming: Fundamentals
C# Programming: Fundamentals
 
Cs30 New
Cs30 NewCs30 New
Cs30 New
 
IOS Swift language 2nd tutorial
IOS Swift language 2nd tutorialIOS Swift language 2nd tutorial
IOS Swift language 2nd tutorial
 
Coding Best Practices
Coding Best PracticesCoding Best Practices
Coding Best Practices
 
Commenting Best Practices
Commenting Best PracticesCommenting Best Practices
Commenting Best Practices
 
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#
 
C#3.0 & Vb 9.0 Language Enhancments
C#3.0 & Vb 9.0 Language EnhancmentsC#3.0 & Vb 9.0 Language Enhancments
C#3.0 & Vb 9.0 Language Enhancments
 
Oo Design And Patterns
Oo Design And PatternsOo Design And Patterns
Oo Design And Patterns
 
Java Building Blocks
Java Building BlocksJava Building Blocks
Java Building Blocks
 
Swift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming languageSwift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming language
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)
 
C#
C#C#
C#
 
M C6java3
M C6java3M C6java3
M C6java3
 
JavaScripts internals #1
JavaScripts internals #1JavaScripts internals #1
JavaScripts internals #1
 
Definitions of Functional Programming
Definitions of Functional ProgrammingDefinitions of Functional Programming
Definitions of Functional Programming
 
Java se 8 fundamentals
Java se 8 fundamentalsJava se 8 fundamentals
Java se 8 fundamentals
 

Viewers also liked

Viewers also liked (8)

C#/.NET Little Wonders
C#/.NET Little WondersC#/.NET Little Wonders
C#/.NET Little Wonders
 
Programming in c#
Programming in c#Programming in c#
Programming in c#
 
Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674Core java complete notes - Contact at +91-814-614-5674
Core java complete notes - Contact at +91-814-614-5674
 
Java Notes
Java NotesJava Notes
Java Notes
 
Java SE 8 best practices
Java SE 8 best practicesJava SE 8 best practices
Java SE 8 best practices
 
Core java slides
Core java slidesCore java slides
Core java slides
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Alphorm.com support de la formation programmer en C# 6
Alphorm.com support de la formation programmer en C# 6Alphorm.com support de la formation programmer en C# 6
Alphorm.com support de la formation programmer en C# 6
 

Similar to More Little Wonders of C#/.NET

Typescript: Beginner to Advanced
Typescript: Beginner to AdvancedTypescript: Beginner to Advanced
Typescript: Beginner to AdvancedTalentica Software
 
On Coding Guidelines
On Coding GuidelinesOn Coding Guidelines
On Coding GuidelinesDIlawar Singh
 
Using Rhino Mocks for Effective Unit Testing
Using Rhino Mocks for Effective Unit TestingUsing Rhino Mocks for Effective Unit Testing
Using Rhino Mocks for Effective Unit TestingMike Clement
 
An Introduction To C++Templates
An Introduction To C++TemplatesAn Introduction To C++Templates
An Introduction To C++TemplatesGanesh Samarthyam
 
C# interview-questions
C# interview-questionsC# interview-questions
C# interview-questionsnicolbiden
 
EuroAD 2021: ChainRules.jl
EuroAD 2021: ChainRules.jl EuroAD 2021: ChainRules.jl
EuroAD 2021: ChainRules.jl Lyndon White
 
Generic Programming seminar
Generic Programming seminarGeneric Programming seminar
Generic Programming seminarGautam Roy
 
Generic Programming in java
Generic Programming in javaGeneric Programming in java
Generic Programming in javaGarik Kalashyan
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldDavid McCarter
 
Style & Design Principles 01 - Code Style & Structure
Style & Design Principles 01 - Code Style & StructureStyle & Design Principles 01 - Code Style & Structure
Style & Design Principles 01 - Code Style & StructureNick Pruehs
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldDavid McCarter
 
Unit 1 introduction to visual basic programming
Unit 1 introduction to visual basic programmingUnit 1 introduction to visual basic programming
Unit 1 introduction to visual basic programmingAbha Damani
 

Similar to More Little Wonders of C#/.NET (20)

Generics
GenericsGenerics
Generics
 
Metaprogramming
MetaprogrammingMetaprogramming
Metaprogramming
 
Typescript: Beginner to Advanced
Typescript: Beginner to AdvancedTypescript: Beginner to Advanced
Typescript: Beginner to Advanced
 
Code Metrics
Code MetricsCode Metrics
Code Metrics
 
On Coding Guidelines
On Coding GuidelinesOn Coding Guidelines
On Coding Guidelines
 
Using Rhino Mocks for Effective Unit Testing
Using Rhino Mocks for Effective Unit TestingUsing Rhino Mocks for Effective Unit Testing
Using Rhino Mocks for Effective Unit Testing
 
Generics
GenericsGenerics
Generics
 
An Introduction To C++Templates
An Introduction To C++TemplatesAn Introduction To C++Templates
An Introduction To C++Templates
 
Generic
GenericGeneric
Generic
 
C# interview-questions
C# interview-questionsC# interview-questions
C# interview-questions
 
EuroAD 2021: ChainRules.jl
EuroAD 2021: ChainRules.jl EuroAD 2021: ChainRules.jl
EuroAD 2021: ChainRules.jl
 
Generic Programming seminar
Generic Programming seminarGeneric Programming seminar
Generic Programming seminar
 
Generic Programming in java
Generic Programming in javaGeneric Programming in java
Generic Programming in java
 
Java performance
Java performanceJava performance
Java performance
 
C#ppt
C#pptC#ppt
C#ppt
 
Generics
GenericsGenerics
Generics
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real World
 
Style & Design Principles 01 - Code Style & Structure
Style & Design Principles 01 - Code Style & StructureStyle & Design Principles 01 - Code Style & Structure
Style & Design Principles 01 - Code Style & Structure
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real World
 
Unit 1 introduction to visual basic programming
Unit 1 introduction to visual basic programmingUnit 1 introduction to visual basic programming
Unit 1 introduction to visual basic programming
 

Recently uploaded

TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 

Recently uploaded (20)

TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 

More Little Wonders of C#/.NET

  • 1. More “Little Wonders” of C#/.Net James Michael Hare 2012 Visual C# MVP Application Architect Scottrade August 3rd, 2012 http://www.BlackRabbitCoder.net Twitter: @BlkRabbitCoder
  • 2. Contact Information Me: Blog: http://www.BlackRabbitCoder.net Twitter: @BlkRabbitCoder Information on Scottrade Careers: http://jobs.scottrade.com Twitter: @scottradejobs
  • 3. What are “Little Wonders”?  The .NET Framework is full of “macro-sized” goodness that can help make our coding lives easier by automating common tasks.  But, the .NET Framework also has a lot of smaller “micro- sized” tips and tricks that can improve code.  Many developers know of most of these, but it is often surprising how many times newer developers don’t.  This presentation picks up where the first “Little Wonders” presentation leaves off…
  • 4. How do they help? Basically, by employing these small items at the right time, you can increase application: Readability – some of the wonders make code much more concise and easy to read. Maintainability – often goes hand and hand with readability, by removing ambiguity of the code, it is easier to maintain without introducing errors. Performance – a few of the little wonders can even help increase the performance of your code (depending on usage).
  • 5. More Little Wonders Compiler Candy Strings Optional/Named Arguments Joining Chained Constructors Constructing Generic Constraints Generic Delegates Anonymous types Action BCL Classes Func Enum Enumerable Static Methods Nullable<T> Empty Lazy<T> Repeat Tuple Collection Generators Interlocked ToArray, ToList, etc.
  • 6. Optional Arguments Optional arguments allow you to specify a default value to be used if argument isn’t provided. Default values must be compile-time constants: Numeric constant expressions String constant expressions null Can be used to replace redundant overloads when the only purpose is to default an argument.
  • 8. Optional Arguments The default is literally compiled in at call, so this: Is, in effect, compiled into this:
  • 9. Named Arguments Defaults are positional, substituted from left to right: If you want to “skip”, used named arguments:
  • 10. Default Parameters There are a couple of potential pitfalls to be aware of: If used across assemblies, can lead to subtle errors. If a default parameter is defined in one assembly, and used in another, must make sure both get recompiled if value changes. Default parameters are not inherited. Defaults established in an interface or base-class do not directly apply (and can be different from) the sub-class. Default depends on reference type, not the object type.
  • 11. Chained Constructors Constructors can call each other directly, which can reduce the need for redundant code in overloads. You could use optional arguments or initializers to reign in overloads, but each have their limitations: Optional arguments must be set to compile-time constant expressions. Initializers aren’t suitable for immutable classes, readonly fields, get-only properties, or items whose construction may be “heavy”.
  • 13. Can simplify by chaining constructors:
  • 14. Generic Constraints Generics allow you to make very powerful types and algorithms, but can be a two-edged sword. An unconstrained generic type parameter makes no assumptions, giving you the lowest common denominator of all types. Supplying a constraint reduces the types that can be used to realize the generic, but expands the things that can be done inside the generic.
  • 15. Generic Constraints For example, how would unconstrained T handle null? Unconstrained T can be compared to null, but this is always false for value types (except Nullable<T>). Unconstrained T cannot be assigned to null, though can be assigned to default(T).
  • 16. Generic Constraints Or constrain T to be a value type (for example, since Nullable<T> can only support value types…
  • 17. Generic Constraints Or what if we want to create a new unconstrained T instance, or access any of T’s members? Can’t, no way to know if unconstrained T supports parameter-less construction or other members.
  • 18. Generic Constraints Supplying a constraint constrains the matching types, but also expands what you can do with the generic: where T : struct – T must be a value type, must be first. Allows T to be used in other value generics (Nullable<T>, etc.). where T : class – T must be a reference type, must be first. Allows T to be assigned to null in generic. where T : U – T must be, inherit, or implement U Allows T to access any public members of U in generic. where T : new() – T must have a public parameter-less constructor, must be last Allows T to be constructed in generic.
  • 19. Generic Constraints Constraining T to things that implement IDictionary and have parameter-less constructor:
  • 20. Generic Constraints Constraining T only value types that implement IConvertible:
  • 21. Anonymous Types We can construct and initialize any type on the fly using object initialization syntax:
  • 22. Anonymous Types What if we only need a type for a very localized purpose? Why create the boilerplate code for a class that will only be used in a small, localized section? Most of these classes are simple POCOs, is it worth a full definition? If you need to use a type as a key (equality, hashing), it can get heavy. Must implement Equals() and GetHashCode() correctly.
  • 23. How would we define UserDay, UserDayTotal?
  • 24. Not as trivial as would think…
  • 25. Anonymous Types Anonymous types create throw-away types for you: Creates type based on name, type, order of properties. Creates suitable Equals() and GetHashCode(). Syntax is like object initialization, just omit type name.
  • 26. Anonymous Types Now, much simpler… Don’t need to define UserDay or UserDayTotal Don’t need to maintain Equals() and GetHashCode(). Doesn’t clutter up project with throw-away types.
  • 27. Anonymous Types A few things to be aware of: Type is generated at compile time based on properties’ names, types, order – any deviation is a different type: Difficult to use outside of defined scope (which shouldn’t do anyway, defeats purpose).
  • 28. The Enum Class Enum is not only the base of enums, it also has several useful static methods: IsDefined() – Determines if an enum constant exists with the given value. HasFlag() – Determines if a given set of bits is set, much easier to read than the typical bitwise AND. TryParse() – Parse a string value into enum value, much lighter than Parse().
  • 29. The Enum Class Use IsDefined() to see if a value exists And HasFlag() simplifies flag testing:
  • 30. The Enum Class The TryParse() is much lighter than Parse(), especially if you think you may not have a valid value:
  • 31. The Nullable<T> Struct You can indicate an optional value easily with null for reference types, but value types always exist. Sometimes, appropriate sentinel values don’t exist. The Nullable<T> generic struct simulates optional values for value types. Nullable<T> has two key properties: HasValue – True if Value has been set to a value. Value – The value if set, throws if not.
  • 32. The Nullable<T> Struct Use when you have a value type which may or may not contain a valid value. For example, the DateTime struct’s default value isn’t very meaningful, use DateTime? Instead.
  • 33. The Nullable<T> Struct We then must test to make sure it has a value by using HasValue property. Once we know the value exists, access Value to retrieve it.
  • 34. The Nullable<T> Struct Nullable<T> allows some syntactical shortcuts: Nullable<T> can be abreviated T? Nullable<T> can be assigned to null Nullable<T> can be compared with null
  • 35. The Nullable<T> Struct Some things to watch out for: Nullable<T> doesn’t save space if value not specified, still stores a Value, it’s just not usable. Accessing Value when HasValue is not true is an error and will throw an exception. Math or logical operations between Nullable<T> wrapped numeric types (int, double, etc.) has caveats: Math with null yields null. Logical ordered comparison with null yields false.
  • 36. The Lazy<T> Class Some classes may be expensive to create, especially if they are not always needed, in these cases lazy instances are often used. No need to create your own lazy-initialization logic anymore, Lazy<T> does it all for you. Can either call default constructor, or a generator. Various thread-safety modes so you can choose the level of performance and safety you require.
  • 37. Lazy<T> Constructor not called until Value accessed.
  • 38. Lazy<T> If a constructor isn’t accessible, or desired, you can always use a factory method:
  • 39. Lazy<T> By default, Lazy<T> is thread-safe, but can choose:
  • 40. The Tuples Sometimes, you need to just throw together several values to treat as a set of values. Tuples allow you to do this in a generic way. Similar to anonymous types, yet different: Both are immutable and have Equals(), GetHashCode() Anonymous types have named properties, but not type. Tuples have generic property names, but named type. Tuple has static factory methods to easy creation.
  • 41. The Tuples Simple tuples are a single “layer” and have properties numbered Item1…Item7:
  • 42. Tuples Get longer Tuples by using octuple, which has TRest:
  • 43. Interlocked Locking to safely increment a count is heavy:
  • 44. Interlocked Allows thread-safe, highly performant manipulation of numeric values. Much lighter than full locks for simple operations like: Increment – adds 1 to the interlocked value Decrement – subtracts 1 from the interlocked value Exchange – swaps two values Add – adds a value to the interlocked value
  • 45. Interlocked Using interlocked, we can do the same thing, without heavy locks:
  • 46. Strings of Repeated Char Have you ever seen someone construct an array of repeated char like this? Instead, quickly create a string of a repeated character using one of string’s constructors:
  • 47. Joining Strings Many times people write code to do this: When they can simply do a Join():
  • 48. Joining Strings The string.Join() has a lot of power Can Join any array or IEnumerable<T>: Can join a variable argument list, including mixed types:
  • 49. Generic Delegates One of the oldest patterns in the OO playbook is to create a class that is 99% complete except for one anonymous “work” method, which is typically abstract…
  • 51. Generic Delegates Inheriting from a class simply to provide a missing method per use is often overkill and poor coupling. Can’t easily change behavior at runtime. Bloats projects with classes that just override.
  • 52. Generic Delegates We can instead provide behavior through a delegate. Behavior can be added with no extra subclasses needed. There is very limited coupling between the delegate and the class/method using it. Behavior could be changed out at runtime. But defining a new delegate each time gets ugly: Generic delegates can be used for most delegate needs.
  • 53. The Action Delegate Action is a generic family of delegates that take up to 16 arguments and return nothing: Action – takes zero arguments, returns nothing Action<T> - takes one argument, returns nothing. Action<T1, T2> - takes two arguments, returns nothing. … Action<T1…T16> - takes 16 arguments, returns nothing. Action cannot be used for ref or out arguments.
  • 54. The Func Delegate Similar to Action, but Func returns a value: Func<TResult> – takes zero args, returns a TResult. Func<T, TResult> - takes one arg, returns a TResult. Func<T1, T2, TResult> - takes two args, returns a TResult. … Func<T1…T16, TResult> - takes 16 args, returns a TResult. Func cannot be used for ref or out arguments. Func<T, bool> is generally preferable to Predicate<T>.
  • 56. Empty Enumerables How many times have you created an empty array or List<T> to represent an empty sequence?
  • 57. Empty Enumerable Enumerable has a static method Empty<T>() that generates a singleton empty IEnumerable<T>.
  • 58. Enumerable Repeat Allows you to create a sequence of the same item a given number of times. Doesn’t recreate the item each time, takes whatever the parameter is and creates a sequence. For example, to create 10 random numbers:
  • 59. Collection Generators Often times you’ll have a sequence and want to store in a collection to: Avoid re-generating the sequence during multiple iterations. Convert the sequence from one sequence type to another. Pull the sequence into a collection to avoid deferred execution issues.
  • 60. Collection Generators There are several LINQ extension methods to generate different collections from sequences: ToArray() – stores sequence in a T[]. ToList() – stores sequence in a List<T>. ToDictionary() – transforms sequence to a Dictionary<TKey, TValue> given selectors for TKey and TValue. ToLookup() – transforms sequence to a Lookup<TKey, TValue> - similar to dictionary but can have repeated keys.
  • 61. ToArray and ToList ToArray() and ToList() are useful for: Remove effects of deferred execution. Convert a sequence to a particular kind.
  • 62. ToDictionary ToDictionary() converts a linear sequence to a Dictionary that maps a single key to a single value. Keys can only exist once, multiple key instances throw. Must provide key selector, value selector optional.
  • 63. ToLookup ToLookup() is similar to ToDictionary(), but it maps a key to a set of values (essentially a multi-map). Again must provide key selector, value optional.