DotNETFrameworkNotesForProfessionals.pdf

(1771 KB) Pobierz
.NET
Framework
.NET Framework
Notes for Professionals
Notes for Professionals
of professional hints and tricks
100+ pages
GoalKicker.com
Free Programming Books
Disclaimer
This is an unocial free book created for educational purposes and is
not aliated with ocial .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#
....................................................................................................................................... 3
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
......................................................................................................................... 4
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
................................................................................................................................................. 5
Section 2.1: Using collection initializers
........................................................................................................................ 5
Section 2.2: Stack
.......................................................................................................................................................... 5
Section 2.3: Creating an initialized List with Custom Types
...................................................................................... 6
Section 2.4: Queue
......................................................................................................................................................... 8
Chapter 3: XmlSerializer
......................................................................................................................................... 10
Section 3.1: Formatting: Custom DateTime format
.................................................................................................. 10
Section 3.2: Serialize object
........................................................................................................................................ 10
Section 3.3: Deserialize object
.................................................................................................................................... 10
Section 3.4: Behaviour: Map array name to property (XmlArray)
......................................................................... 10
Section 3.5: Behaviour: Map Element name to Property
........................................................................................ 11
Section 3.6: Eciently building multiple serializers with derived types specified dynamically
........................... 11
Chapter 4: HTTP clients
.......................................................................................................................................... 14
Section 4.1: Reading GET response as string using System.Net.HttpClient
........................................................... 14
Section 4.2: Basic HTTP downloader using System.Net.Http.HttpClient
................................................................ 14
Section 4.3: Reading GET response as string using System.Net.HttpWebRequest
.............................................. 15
Section 4.4: Reading GET response as string using System.Net.WebClient
......................................................... 15
Section 4.5: Sending a POST request with a string payload using System.Net.HttpWebRequest
..................... 15
Section 4.6: Sending a POST request with a string payload using System.Net.WebClient
................................. 16
Section 4.7: Sending a POST request with a string payload using System.Net.HttpClient
.................................. 16
Chapter 5: Exceptions
............................................................................................................................................... 17
Section 5.1: Catching and rethrowing caught exceptions
....................................................................................... 17
Section 5.2: Using a finally block
............................................................................................................................... 18
Section 5.3: Exception Filters
...................................................................................................................................... 18
Section 5.4: Rethrowing an exception within a catch block
.................................................................................... 19
Section 5.5: Throwing an exception from a dierent method while preserving its information
........................ 19
Section 5.6: Catching an exception
........................................................................................................................... 20
Chapter 6: LINQ
........................................................................................................................................................... 21
Section 6.1: SelectMany (flat map)
............................................................................................................................ 21
Section 6.2: Where (filter)
........................................................................................................................................... 22
Section 6.3: Any
........................................................................................................................................................... 22
Section 6.4: GroupJoin
................................................................................................................................................ 23
Section 6.5: Except
...................................................................................................................................................... 24
Section 6.6: Zip
............................................................................................................................................................. 24
Section 6.7: Aggregate (fold)
..................................................................................................................................... 24
Section 6.8: ToLookup
................................................................................................................................................. 25
Section 6.9: Intersect
................................................................................................................................................... 25
Section 6.10: Concat
.................................................................................................................................................... 25
Section 6.11: All
............................................................................................................................................................. 25
Section 6.12: Sum
......................................................................................................................................................... 26
Section 6.13: SequenceEqual
...................................................................................................................................... 26
Section 6.14: Min
........................................................................................................................................................... 26
Section 6.15: Distinct
.................................................................................................................................................... 27
Section 6.16: Count
...................................................................................................................................................... 27
Section 6.17: Cast
......................................................................................................................................................... 27
Section 6.18: Range
..................................................................................................................................................... 27
Section 6.19: ThenBy
................................................................................................................................................... 28
Section 6.20: Repeat
................................................................................................................................................... 28
Section 6.21: Empty
..................................................................................................................................................... 28
Section 6.22: Select (map)
.......................................................................................................................................... 28
Section 6.23: OrderBy
................................................................................................................................................. 29
Section 6.24: OrderByDescending
............................................................................................................................. 29
Section 6.25: Contains
................................................................................................................................................. 29
Section 6.26: First (find)
.............................................................................................................................................. 29
Section 6.27: Single
...................................................................................................................................................... 30
Section 6.28: Last
........................................................................................................................................................ 30
Section 6.29: LastOrDefault
........................................................................................................................................ 30
Section 6.30: SingleOrDefault
.................................................................................................................................... 31
Section 6.31: FirstOrDefault
........................................................................................................................................ 31
Section 6.32: Skip
......................................................................................................................................................... 31
Section 6.33: Take
....................................................................................................................................................... 32
Section 6.34: Reverse
.................................................................................................................................................. 32
Section 6.35: OfType
................................................................................................................................................... 32
Section 6.36: Max
......................................................................................................................................................... 32
Section 6.37: Average
................................................................................................................................................. 32
Section 6.38: GroupBy
................................................................................................................................................. 33
Section 6.39: ToDictionary
.......................................................................................................................................... 33
Section 6.40: Union
...................................................................................................................................................... 34
Section 6.41: ToArray
.................................................................................................................................................. 35
Section 6.42: ToList
..................................................................................................................................................... 35
Section 6.43: ElementAt
.............................................................................................................................................. 35
Section 6.44: ElementAtOrDefault
............................................................................................................................. 35
Section 6.45: SkipWhile
............................................................................................................................................... 35
Section 6.46: TakeWhile
.............................................................................................................................................. 36
Section 6.47: DefaultIfEmpty
...................................................................................................................................... 36
Section 6.48: Join
........................................................................................................................................................ 36
Section 6.49: Left Outer Join
...................................................................................................................................... 37
Chapter 7: Networking
............................................................................................................................................. 39
Section 7.1: Basic TCP chat (TcpListener, TcpClient, NetworkStream)
.................................................................. 39
Section 7.2: Basic SNTP client (UdpClient)
................................................................................................................ 40
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
........................................................................................... 43
Section 8.7: Updating a package
.............................................................................................................................. 43
Section 8.8: Uninstalling a package
.......................................................................................................................... 44
Section 8.9: Uninstall a specific version of package
............................................................................................... 44
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
.......................................................................................................................................... 48
Section 10.1: Initializing a Dictionary with a Collection Initializer
............................................................................. 48
Section 10.2: Adding to a Dictionary
.......................................................................................................................... 48
Section 10.3: Getting a value from a dictionary
....................................................................................................... 48
Section 10.4: Make a Dictionary<string, T> with Case-Insensivitve keys
............................................................... 49
Section 10.5: IEnumerable to Dictionary (≥ .NET 3.5)
............................................................................................... 49
Section 10.6: Enumerating a Dictionary
.................................................................................................................... 49
Section 10.7: ConcurrentDictionary<TKey, TValue> (from .NET 4.0)
.................................................................... 50
Section 10.8: Dictionary to List
................................................................................................................................... 51
Section 10.9: Removing from a Dictionary
............................................................................................................... 51
Section 10.10: ContainsKey(TKey)
............................................................................................................................. 52
Section 10.11: ConcurrentDictionary augmented with Lazy'1 reduces duplicated computation
.......................... 52
Chapter 11: HTTP servers
........................................................................................................................................ 54
Section 11.1: Basic read-only HTTP file server (ASP.NET Core)
............................................................................... 54
Section 11.2: Basic read-only HTTP file server (HttpListener)
................................................................................. 55
Chapter 12: Settings
................................................................................................................................................... 58
Section 12.1: AppSettings from ConfigurationSettings in .NET 1.x
........................................................................... 58
Section 12.2: Reading AppSettings from ConfigurationManager in .NET 2.0 and later
....................................... 58
Section 12.3: Introduction to strongly-typed application and user settings support from Visual Studio
................................................................................................................................................................................ 59
Section 12.4: Reading strongly-typed settings from custom section of configuration file
................................. 60
Chapter 13: Task Parallel Library (TPL)
........................................................................................................... 63
Section 13.1: Basic producer-consumer loop (BlockingCollection)
......................................................................... 63
Section 13.2: Parallel.Invoke
........................................................................................................................................ 63
Section 13.3: Task: Returning a value
........................................................................................................................ 64
Section 13.4: Parallel.ForEach
..................................................................................................................................... 64
Section 13.5: Parallel.For
............................................................................................................................................. 64
Section 13.6: Task: basic instantiation and Wait
....................................................................................................... 65
Section 13.7: Task.WhenAll
.......................................................................................................................................... 65
Section 13.8: Flowing execution context with AsyncLocal
....................................................................................... 65
Section 13.9: Parallel.ForEach in VB.NET
................................................................................................................... 66
Section 13.10: Task: WaitAll and variable capturing
................................................................................................. 66
Section 13.11: Task: WaitAny
........................................................................................................................................ 67
Section 13.12: Task: handling exceptions (using Wait)
............................................................................................. 67
Section 13.13: Task: handling exceptions (without using Wait)
............................................................................... 67
Section 13.14: Task: cancelling using CancellationToken
......................................................................................... 68
Section 13.15: Task.WhenAny
...................................................................................................................................... 69
Chapter 14: Custom Types
..................................................................................................................................... 70
Section 14.1: Struct Definition
...................................................................................................................................... 70
Section 14.2: Class Definition
...................................................................................................................................... 70
Chapter 15: DateTime parsing
............................................................................................................................. 72
Section 15.1: ParseExact
.............................................................................................................................................. 72
Section 15.2: TryParse
................................................................................................................................................. 73
Section 15.3: TryParseExact
........................................................................................................................................ 75
Chapter 16: Memory management
.................................................................................................................... 76
Section 16.1: Use SafeHandle when wrapping unmanaged resources
.................................................................. 76
Section 16.2: Unmanaged Resources
........................................................................................................................ 76
Chapter 17: Managed Extensibility Framework
........................................................................................... 78
Section 17.1: Connecting (Basic)
................................................................................................................................. 78
Section 17.2: Exporting a Type (Basic)
...................................................................................................................... 78
Section 17.3: Importing (Basic)
................................................................................................................................... 79
Chapter 18: SpeechRecognitionEngine class to recognize speech
................................................... 80
Section 18.1: Asynchronously recognizing speech based on a restricted set of phrases
.................................... 80
Section 18.2: Asynchronously recognizing speech for free text dictation
............................................................. 80
Chapter 19: System.Reflection.Emit namespace
........................................................................................ 81
Section 19.1: Creating an assembly dynamically
..................................................................................................... 81
Chapter 20: System.Runtime.Caching.MemoryCache (ObjectCache)
............................................. 84
Section 20.1: Adding Item to Cache (Set)
.................................................................................................................. 84
Section 20.2: System.Runtime.Caching.MemoryCache (ObjectCache)
................................................................. 84
Chapter 21: JSON Serialization
............................................................................................................................ 86
Section 21.1: Deserialization using System.Web.Script.Serialization.JavaScriptSerializer
.................................... 86
Section 21.2: Serialization using Json.NET
................................................................................................................ 86
Section 21.3: Serialization-Deserialization using Newtonsoft.Json
......................................................................... 87
Section 21.4: Deserialization using Json.NET
............................................................................................................ 87
Section 21.5: Dynamic binding
................................................................................................................................... 87
Section 21.6: Serialization using Json.NET with JsonSerializerSettings
................................................................. 88
Chapter 22: TPL Dataflow
...................................................................................................................................... 89
Section 22.1: Asynchronous Producer Consumer With A Bounded BuerBlock
.................................................. 89
Section 22.2: Posting to an ActionBlock and waiting for completion
.................................................................... 89
Section 22.3: Linking blocks to create a pipeline
..................................................................................................... 89
Section 22.4: Synchronous Producer/Consumer with BuerBlock<T>
................................................................. 90
Chapter 23: File Input/Output
.............................................................................................................................. 92
Section 23.1: C# File.Exists()
........................................................................................................................................ 92
Section 23.2: VB WriteAllText
..................................................................................................................................... 92
Section 23.3: VB StreamWriter
................................................................................................................................... 92
Section 23.4: C# StreamWriter
................................................................................................................................... 92
Section 23.5: C# WriteAllText()
.................................................................................................................................. 93
Chapter 24: Platform Invoke
................................................................................................................................ 94
Section 24.1: Marshaling structs
................................................................................................................................. 94
Section 24.2: Marshaling unions
................................................................................................................................ 95
Section 24.3: Calling a Win32 dll function
................................................................................................................. 96
Section 24.4: Using Windows API
............................................................................................................................... 97
Section 24.5: Marshalling arrays
............................................................................................................................... 97
Chapter 25: Code Contracts
.................................................................................................................................. 98
Section 25.1: Contracts for Interfaces
....................................................................................................................... 98
Section 25.2: Installing and Enabling Code Contracts
............................................................................................. 98
Section 25.3: Preconditions
...................................................................................................................................... 100
Section 25.4: Postconditions
.................................................................................................................................... 101
Zgłoś jeśli naruszono regulamin