PacktLib: .NET 4.0 Generics Beginner’s Guide

.NET 4.0 Generics

Credits

Foreword

About the Author

Acknowledgement

About the Reviewers

www.PacktPub.com

Preface

Why Generics?

An analogy

Setting up the environment

Summary

Lists

Why bother learning about generic lists?

Types of generic lists

Checking whether a sequence is a palindrome or not

Time for action — creating the generic stack as the buffer

Time for action — completing the rest of the method

Designing a generic anagram finder

Time for action — creating the method

Life is full of priorities, let's bring some order there

Time for action — creating the data structure for the prioritized shopping list

Time for action — let's add some gadgets to the list and see them

Time for action — let's strike off the gadgets with top-most priority after we have bought them

Time for action — let's create an appointment list

Live sorting and statistics for online bidding

Time for action — let's create a custom class for live sorting

Why did we have three LinkedList as part of the data structure?

An attempt to answer questions asked by your boss

Time for action — associating products with live sorted bid amounts

Time for action — finding common values across different bidding amount lists

You will win every scrabble game from now on

Time for action — creating the method to find the character histogram of a word

Time for action — checking whether a word can be formed

Time for action — let's see whether it works

Trying to fix an appointment with a doctor?

Time for action — creating a set of dates of the doctors' availability

Time for action — finding out when both doctors shall be present

Revisiting the anagram problem

Time for action — re-creating the anagram finder

Lists under the hood

Summary

Dictionaries

Types of generic associative structures

Creating a tag cloud generator using dictionary

Time for action — creating the word histogram

Creating a bubble wrap popper game

Time for action — creating the game console

Look how easy it was!

How did we decide we need a dictionary and not a list?

Let's build a generic autocomplete service

Time for action — creating a custom dictionary for autocomplete

Time for action — creating a class for autocomplete

The most common pitfall. Don't fall there!

Let's play some piano

Time for action — creating the keys of the piano

How are we recording the key strokes?

Time for action — switching on recording and playing recorded keystrokes

C# Dictionaries can help detect cancer. Let's see how!

Time for action — creating the KNN API

Time for action — getting the patient records

Time for action — creating the helper class to read a delimited file

Time for action — let's see how to use the predictor

Tuples are great for many occasions including games

Time for action — putting it all together

Why have we used Tuples?

How did we figure out whether the game is over or not?

Summary

LINQ to Objects

What makes LINQ?

Time for action — creating an Extension method

Time for action — consuming our new Extension method

Putting it all together, LINQ Standard Query Operators

Time for action — getting the LINQPad

Time for action — finding all names with *am*

Time for action — finding all vowels

Time for action — finding all running processes matching a Regex

Time for action — playing with the indexed version of Where()

Time for action — learn how to go about creating a Where() clause

Time for action — let's say "Hello" to your buddies

Time for action — radio "Lucky Caller" announcement

Time for action — flattening a dictionary

Time for action — leaving the first few elements

Time for action — picking conditionally

Time for action — skipping save looping

Time for action — reversing word-by-word

Time for action — checking whether a given string is a palindrome or not

Time for action — sorting names alphabetically

Time for action — sorting 2D points by their co-ordinates

Time for action — sorting a list of fruits

Time for action — indexing an array of strings

Time for action — grouping by length

Time for action — finding common names from two names' lists

Time for action — finding all names from the list, removing duplicates

Time for action — pulling it all together including duplicates

Time for action — finding all names that appear mutually exclusively

Time for action — removing duplicate song IDs from the list

Time for action — making sure it works!

Time for action — making a list out of IEnumerable

Time for action — tagging names

Time for action — one-to-many mapping

Time for action — finding the first element that satisfies a condition

Time for action — getting acquainted with FirstOrDefault()

Time for action — checking whether a sequence is palindromic

Time for action — understanding ElementAt()

Time for action — check out DefaultIfEmpty()

Time for action — generating arithmetic progression ranges

Time for action — running a filter on a range

Time for action — let's go round and round with Repeat()

Time for action — checking whether there is only one item matching this pattern

Time for action — set to default if there is more than one matching elements

Time for action — checking Any()

Time for action — how to check whether all items match a condition

Summary

Observable Collections

Active change/Statistical change

Passive change/Non-statistical change

Data sensitive change

Time for action — creating a simple math question monitor

Time for action — creating the collections to hold questions

Time for action — attaching the event to monitor the collections

Time for action — dealing with the change as it happens

Time for action — dealing with the change as it happens

Time for action — putting it all together

Time for action — creating a Twitter browser

Time for action — creating the interface

Time for action — creating the TweetViewer user control design

Time for action — gluing the TweetViewer control

Time for action — putting everything together

Time for action — dealing with the change in the list of names in the first tab

Time for action — a few things to beware of at the form load

Time for action — things to do when names get added or deleted

Time for action — sharing the load and creating a task for each BackgroundWorker

Time for action — a sample run of the application

Summary

Concurrent Collections

Creating and running asynchronous tasks

Simulating a survey (which is, of course, simultaneous by nature)

Time for action — creating the blocks

Devising a data structure for finding the most in-demand item

Time for action — creating the concurrent move-to-front list

Time for action — simulating a bank queue with multiple tellers

Time for action — making our bank queue simulator more useful

Be a smart consumer, don't wait till you have it all

Exploring data structure mapping

Summary

Power Collections

Setting up the environment

BinarySearch()

Time for action — finding a name from a list of names

CartesianProduct()

Time for action — generating names of all the 52 playing cards

RandomShuffle()

Time for action — randomly shuffling the deck

NCopiesOf()

Time for action — creating random numbers of any given length

Time for action — creating a custom random number generator

ForEach()

Time for action — creating a few random numbers of given any length

Rotate() and RotateInPlace()

Time for action — rotating a word

Time for action — creating a word guessing game

RandomSubset()

Time for action — picking a set of random elements

Reverse()

Time for action — reversing any collection

EqualCollections()

Time for action — revisiting the palindrome problem

DisjointSets()

Time for action — checking for common stuff

Time for action — finding anagrams the easiest way

Creating an efficient arbitrary floating point representation

Time for action — creating a huge number API

Creating an API for customizable default values

Time for action — creating a default value API

Mapping data structure

Algorithm conversion strategy

Summary

C5 Collections

Setting up the environment

Time for action — cloning Gender Genie!

Time for action — revisiting the anagram problem

Time for action — Google Sets idea prototype

Time for action — finding the most sought-after item

Sorting algorithms

Summary

Patterns, Practices, and Performance

Generic container patterns

A special Tuple<> pattern

Time for action — refactoring deeply nested if-else blocks

Best practices when using Generics

Selecting a generic collection

Best practices when creating custom generic collections

Performance analysis

How would we do this investigation?

Benchmarking experiment 1

Benchmarking experiment 2

Benchmarking experiment 3

Benchmarking experiment 4

Benchmarking experiment 5

Benchmarking experiment 6

Benchmarking experiment 7

Benchmarking experiment 8

Benchmarking experiment 9

Summary

Performance Cheat Sheet

Performance Cheat Sheet

Migration Cheat Sheet

Pop Quiz Answers

Pop Quiz Answers

Pop Quiz Answers

Pop Quiz Answers