Complete .NET Framework Secrets & Tips for Professionals

.NET Framework

Complete Complete Tips & Secrets for Professionals

.NET Framework

Tips & Secrets for Professionals

100+ pages

of professional hints and tricks



Free Programming Books

Disclaimer This is an unocial free book created for educational purposes and is

not aliated with ocial .NET Framework group(s) or company(s). All trademarks and registered trademarks are the property of their respective owners

Contents

About ................................................................................................................................................................................... 1 Chapter 1: Getting started with .NET Framework ........................................................................................ 2

Section 1.1: Hello World in C# ........................................................................................................................................ 2 Section 1.2: Hello World in F# ....................................................................................................................................... 2 Section 1.3: Hello World in Visual Basic .NET .............................................................................................................. 3 Section 1.4: Hello World in C++/CLI .............................................................................................................................. 3 Section 1.5: Hello World in IL ......................................................................................................................................... 3 Section 1.6: Hello World in PowerShell ......................................................................................................................... 3 Section 1.7: Hello World in Nemerle ............................................................................................................................. 4 Section 1.8: Hello World in Python (IronPython) ......................................................................................................... 4 Section 1.9: Hello World in Oxygene ............................................................................................................................ 4 Section 1.10: Hello World in Boo ................................................................................................................................... 4

Chapter 2: Collections ................................................................................................................................................. 4

Section 2.1: Using collection initializers ........................................................................................................................ 4 Section 2.2: Stack .......................................................................................................................................................... 5 Section 2.3: Creating an initialized List with Custom Types ...................................................................................... 6 Section 2.4: Queue ......................................................................................................................................................... 7

Chapter 3: XmlSerializer ............................................................................................................................................ 8

Section 3.1: Formatting: Custom DateTime format .................................................................................................... 8 Section 3.2: Serialize object .......................................................................................................................................... 9 Section 3.3: Deserialize object ...................................................................................................................................... 9 Section 3.4: Behaviour: Map array name to property (XmlArray) ........................................................................... 9 Section 3.5: Behaviour: Map Element name to Property ........................................................................................... 9 Section 3.6: Eciently building multiple serializers with derived types specified dynamically ............................. 9

Chapter 4: HTTP clients .......................................................................................................................................... 12

Section 4.1: Reading GET response as string using .HttpClient ........................................................... 12 Section 4.2: Basic HTTP downloader using .Http.HttpClient ................................................................ 12 Section 4.3: Reading GET response as string using .HttpWebRequest .............................................. 13 Section 4.4: Reading GET response as string using .WebClient ......................................................... 13 Section 4.5: Sending a POST request with a string payload using .HttpWebRequest ..................... 14 Section 4.6: Sending a POST request with a string payload using .WebClient ................................. 14 Section 4.7: Sending a POST request with a string payload using .HttpClient .................................. 14

Chapter 5: Exceptions ............................................................................................................................................... 15

Section 5.1: Catching and rethrowing caught exceptions ....................................................................................... 15 Section 5.2: Using a finally block ............................................................................................................................... 16 Section 5.3: Exception Filters ...................................................................................................................................... 16 Section 5.4: Rethrowing an exception within a catch block .................................................................................... 16 Section 5.5: Throwing an exception from a dierent method while preserving its information ........................ 17 Section 5.6: Catching an exception ........................................................................................................................... 17

Chapter 6: LINQ ........................................................................................................................................................... 18

Section 6.1: SelectMany (flat map) ............................................................................................................................ 24 Section 6.2: Where (filter) ........................................................................................................................................... 25 Section 6.3: Any ........................................................................................................................................................... 25 Section 6.4: GroupJoin ................................................................................................................................................ 25 Section 6.5: Except ...................................................................................................................................................... 26 Section 6.6: Zip ............................................................................................................................................................. 27 Section 6.7: Aggregate (fold) ..................................................................................................................................... 27

Section 6.8: ToLookup ................................................................................................................................................. 27 Section 6.9: Intersect ................................................................................................................................................... 28 Section 6.10: Concat .................................................................................................................................................... 28 Section 6.11: All ............................................................................................................................................................. 28 Section 6.12: Sum ......................................................................................................................................................... 28 Section 6.13: SequenceEqual ...................................................................................................................................... 29 Section 6.14: Min ........................................................................................................................................................... 29 Section 6.15: Distinct .................................................................................................................................................... 29 Section 6.16: Count ...................................................................................................................................................... 29 Section 6.17: Cast ......................................................................................................................................................... 29 Section 6.18: Range ..................................................................................................................................................... 30 Section 6.19: ThenBy ................................................................................................................................................... 30 Section 6.20: Repeat ................................................................................................................................................... 30 Section 6.21: Empty ..................................................................................................................................................... 31 Section 6.22: Select (map) .......................................................................................................................................... 31 Section 6.23: OrderBy ................................................................................................................................................. 31 Section 6.24: OrderByDescending ............................................................................................................................. 31 Section 6.25: Contains ................................................................................................................................................. 32 Section 6.26: First (find) .............................................................................................................................................. 32 Section 6.27: Single ...................................................................................................................................................... 32 Section 6.28: Last ........................................................................................................................................................ 32 Section 6.29: LastOrDefault ........................................................................................................................................ 32 Section 6.30: SingleOrDefault .................................................................................................................................... 33 Section 6.31: FirstOrDefault ........................................................................................................................................ 33 Section 6.32: Skip ......................................................................................................................................................... 33 Section 6.33: Take ....................................................................................................................................................... 34 Section 6.34: Reverse .................................................................................................................................................. 34 Section 6.35: OfType ................................................................................................................................................... 34 Section 6.36: Max ......................................................................................................................................................... 34 Section 6.37: Average ................................................................................................................................................. 34 Section 6.38: GroupBy ................................................................................................................................................. 35 Section 6.39: ToDictionary .......................................................................................................................................... 35 Section 6.40: Union ...................................................................................................................................................... 36 Section 6.41: ToArray .................................................................................................................................................. 36 Section 6.42: ToList ..................................................................................................................................................... 37 Section 6.43: ElementAt .............................................................................................................................................. 37 Section 6.44: ElementAtOrDefault ............................................................................................................................. 37 Section 6.45: SkipWhile ............................................................................................................................................... 37 Section 6.46: TakeWhile .............................................................................................................................................. 37 Section 6.47: DefaultIfEmpty ...................................................................................................................................... 38 Section 6.48: Join ........................................................................................................................................................ 38 Section 6.49: Left Outer Join ...................................................................................................................................... 39

Chapter 7: Networking ............................................................................................................................................. 39

Section 7.1: Basic TCP chat (TcpListener, TcpClient, NetworkStream) .................................................................. 39 Section 7.2: Basic SNTP client (UdpClient) ................................................................................................................ 41

Chapter 8: NuGet packaging system ............................................................................................................... 42

Section 8.1: Uninstalling a package from one project in a solution ....................................................................... 42 Section 8.2: Installing a specific version of a package ............................................................................................ 42 Section 8.3: Adding a package source feed (MyGet, Klondike, ect) ...................................................................... 42 Section 8.4: Installing the NuGet Package Manager ............................................................................................... 42

Section 8.5: Managing Packages through the UI ..................................................................................................... 43 Section 8.6: Managing Packages through the console ........................................................................................... 44 Section 8.7: Updating a package .............................................................................................................................. 44 Section 8.8: Uninstalling a package .......................................................................................................................... 44 Section 8.9: uninstall a specific version of package ................................................................................................ 45

Chapter 9: Reflection ................................................................................................................................................ 45

Section 9.1: What is an Assembly? ............................................................................................................................. 45 Section 9.2: Compare two objects with reflection ................................................................................................... 45 Section 9.3: Creating Object and setting properties using reflection .................................................................... 46 Section 9.4: How to create an object of T using Reflection .................................................................................... 46 Section 9.5: Getting an attribute of an enum with reflection (and caching it) ...................................................... 46

Chapter 10: Dictionaries .......................................................................................................................................... 47

Section 10.1: Initializing a Dictionary with a Collection Initializer ............................................................................. 47 Section 10.2: Adding to a Dictionary .......................................................................................................................... 47 Section 10.3: Getting a value from a dictionary ....................................................................................................... 48 Section 10.4: Make a Dictionary with Case-Insensivitve keys. .............................................................. 48 Section 10.5: IEnumerable to Dictionary (? .NET 3.5) ............................................................................................... 48 Section 10.6: Enumerating a Dictionary .................................................................................................................... 49 Section 10.7: ConcurrentDictionary (from .NET 4.0) ..................................................................... 49 Section 10.8: Dictionary to List ................................................................................................................................... 50 Section 10.9: Removing from a Dictionary ............................................................................................................... 50 Section 10.10: ContainsKey(TKey) ............................................................................................................................. 51 Section 10.11: ConcurrentDictionary augmented with Lazy'1 reduces duplicated computation .......................... 51

Chapter 11: HTTP servers ........................................................................................................................................ 52

Section 11.1: Basic read-only HTTP file server ( Core) ............................................................................... 52 Section 11.2: Basic read-only HTTP file server (HttpListener) ................................................................................. 53

Chapter 12: Settings ................................................................................................................................................... 55

Section 12.1: AppSettings from ConfigurationSettings in .NET 1.x ........................................................................... 55 Section 12.2: Reading AppSettings from ConfigurationManager in .NET 2.0 and later ....................................... 56 Section 12.3: Introduction to strongly-typed application and user settings support from Visual Studio

................................................................................................................................................................................ 57 Section 12.4: Reading strongly-typed settings from custom section of configuration file ................................. 58

Chapter 13: Task Parallel Library (TPL) ........................................................................................................... 59

Section 13.1: Basic producer-consumer loop (BlockingCollection) ......................................................................... 59 Section 13.2: Parallel.Invoke ........................................................................................................................................ 60 Section 13.3: Task: Returning a value ........................................................................................................................ 60 Section 13.4: Parallel.ForEach ..................................................................................................................................... 61 Section 13.5: Parallel.For ............................................................................................................................................. 61 Section 13.6: Task: basic instantiation and Wait ....................................................................................................... 61 Section 13.7: Task.WhenAll .......................................................................................................................................... 62 Section 13.8: Flowing execution context with AsyncLocal ....................................................................................... 62 Section 13.9: Parallel.ForEach in ................................................................................................................... 63 Section 13.10: Task: WaitAll and variable capturing ................................................................................................. 63 Section 13.11: Task: WaitAny ........................................................................................................................................ 63 Section 13.12: Task: handling exceptions (using Wait) ............................................................................................. 63 Section 13.13: Task: handling exceptions (without using Wait) ............................................................................... 64 Section 13.14: Task: cancelling using CancellationToken ......................................................................................... 64 Section 13.15: Task.WhenAny ...................................................................................................................................... 65

Chapter 14: Custom Types ..................................................................................................................................... 66

Section 14.1: Struct Definition ...................................................................................................................................... 66

Section 14.2: Class Definition ...................................................................................................................................... 66

Chapter 15: DateTime parsing ............................................................................................................................. 67

Section 15.1: ParseExact .............................................................................................................................................. 67 Section 15.2: TryParse ................................................................................................................................................. 68 Section 15.3: TryParseExact ........................................................................................................................................ 69

Chapter 16: Memory management .................................................................................................................... 70

Section 16.1: Use SafeHandle when wrapping unmanaged resources .................................................................. 70 Section 16.2: Unmanaged Resources ........................................................................................................................ 70

Chapter 17: Managed Extensibility Framework ........................................................................................... 71

Section 17.1: Connecting (Basic) ................................................................................................................................. 71 Section 17.2: Exporting a Type (Basic) ...................................................................................................................... 71 Section 17.3: Importing (Basic) ................................................................................................................................... 72

Chapter 18: SpeechRecognitionEngine class to recognize speech ................................................... 72

Section 18.1: Asynchronously recognizing speech based on a restricted set of phrases .................................... 73 Section 18.2: Asynchronously recognizing speech for free text dictation ............................................................. 73

Chapter 19: System.Reflection.Emit namespace ........................................................................................ 73

Section 19.1: Creating an assembly dynamically ..................................................................................................... 73

Chapter 20: System.Runtime.Caching.MemoryCache (ObjectCache) ............................................. 76

Section 20.1: Adding Item to Cache (Set) .................................................................................................................. 76 Section 20.2: System.Runtime.Caching.MemoryCache (ObjectCache) ................................................................. 76

Chapter 21: JSON Serialization ............................................................................................................................ 77

Section 21.1: Deserialization using System.Web.Script.Serialization.JavaScriptSerializer .................................... 77 Section 21.2: Serialization using ................................................................................................................ 77 Section 21.3: Serialization-Deserialization using Newtonsoft.Json ......................................................................... 78 Section 21.4: Deserialization using ............................................................................................................ 78 Section 21.5: Dynamic binding ................................................................................................................................... 79 Section 21.6: Serialization using with JsonSerializerSettings ................................................................. 79

Chapter 22: TPL Dataflow ...................................................................................................................................... 80

Section 22.1: Asynchronous Producer Consumer With A Bounded BuerBlock .................................................. 80 Section 22.2: Posting to an ActionBlock and waiting for completion .................................................................... 80 Section 22.3: Linking blocks to create a pipeline ..................................................................................................... 80 Section 22.4: Synchronous Producer/Consumer with BuerBlock ................................................................. 81

Chapter 23: File Input/Output .............................................................................................................................. 82

Section 23.1: C# File.Exists() ........................................................................................................................................ 82 Section 23.2: VB WriteAllText ..................................................................................................................................... 82 Section 23.3: VB StreamWriter ................................................................................................................................... 82 Section 23.4: C# StreamWriter ................................................................................................................................... 82 Section 23.5: C# WriteAllText() .................................................................................................................................. 82

Chapter 24: Platform Invoke ................................................................................................................................ 83

Section 24.1: Marshaling structs ................................................................................................................................. 83 Section 24.2: Marshaling unions ................................................................................................................................ 85 Section 24.3: Calling a Win32 dll function ................................................................................................................. 85 Section 24.4: Using Windows API ............................................................................................................................... 86 Section 24.5: Marshalling arrays ............................................................................................................................... 86

Chapter 25: Code Contracts .................................................................................................................................. 86

Section 25.1: Contracts for Interfaces ....................................................................................................................... 86 Section 25.2: Installing and Enabling Code Contracts ............................................................................................. 87 Section 25.3: Preconditions ........................................................................................................................................ 88 Section 25.4: Postconditions ....................................................................................................................................... 89

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download