.NET Framework Notes for Professionals

GoalKicker.com (CC BY-SA)
Page 1 sur 192Lecteur de document UniversityLib

.NET Framework Notes for Professionals

Programming · course

Voir tous les documents en programmation

.NET Framework

Notes for Professionals

.NET

Framework

Notes for Professionals

100+ pages

of professional hints and tricks

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#

Section 1.2: Hello World in F#

Section 1.3: Hello World in Visual Basic .NET

Section 1.4: Hello World in C++/CLI

Section 1.5: Hello World in IL

Section 1.6: Hello World in PowerShell

Section 1.7: Hello World in Nemerle

Section 1.8: Hello World in Python (IronPython)

Section 1.9: Hello World in Oxygene

Section 1.10: Hello World in Boo

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

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

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

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

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

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

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

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

4

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

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

Chapter 2: Strings

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

5

Section 2.1: Count characters

Section 2.2: Count distinct characters

Section 2.3: Convert string to/from another encoding

Section 2.4: Comparing strings

Section 2.5: Count occurrences of a character

Section 2.6: Split string into fixed length blocks

Section 2.7: Object.ToString() virtual method

Section 2.8: Immutability of strings

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

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

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

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

5

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

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

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

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

Chapter 3: DateTime parsing

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

9

Section 3.1: ParseExact

Section 3.2: TryParse

Section 3.3: TryParseExact

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

9

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

10

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

12

Chapter 4: Dictionaries

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

13

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

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

Section 4.1: Initializing a Dictionary with a Collection Initializer

Section 4.2: Adding to a Dictionary

Section 4.3: Getting a value from a dictionary

Section 4.4: Make a Dictionary<string, T> with Case-Insensivitve keys

Section 4.5: IEnumerable to Dictionary (≥ .NET 3.5)

Section 4.6: Enumerating a Dictionary

Section 4.7: ConcurrentDictionary<TKey, TValue> (from .NET 4.0)

Section 4.8: Dictionary to List

Section 4.9: Removing from a Dictionary

Section 4.10: ContainsKey(TKey)

Section 4.11: ConcurrentDictionary augmented with Lazy'1 reduces duplicated computation

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

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

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

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

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

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

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

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

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

17

Chapter 5: Collections

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

19

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

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

Section 5.1: Using collection initializers

Section 5.2: Stack

Section 5.3: Creating an initialized List with Custom Types

Section 5.4: Queue

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

22

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

20

Chapter 6: ReadOnlyCollections

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

24

Section 6.1: Creating a ReadOnlyCollection

Section 6.2: Updating a ReadOnlyCollection

Section 6.3: Warning: Elements in a ReadOnlyCollection are not inherently read-only

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

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

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

24

2

3

3

3

3

4

4

4

Publicité

4

5

5

6

6

6

7

8

13

13

13

14

14

14

15

16

16

17

19

19

24

24

Chapter 7: Stack and Heap

Section 7.1: Value types in use

Section 7.2: Reference types in use

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

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

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

26

Chapter 8: LINQ

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

28

Section 8.1: SelectMany (flat map)

Section 8.2: Where (filter)

Section 8.3: Any

Section 8.4: GroupJoin

Section 8.5: Except

Section 8.6: Zip

Section 8.7: Aggregate (fold)

Section 8.8: ToLookup

Section 8.9: Intersect

Section 8.10: Concat

Section 8.11: All

Section 8.12: Sum

Section 8.13: SequenceEqual

Section 8.14: Min

Section 8.15: Distinct

Section 8.16: Count

Section 8.17: Cast

Section 8.18: Range

Section 8.19: ThenBy

Section 8.20: Repeat

Section 8.21: Empty

Section 8.22: Select (map)

Section 8.23: OrderBy

Section 8.24: OrderByDescending

Section 8.25: Contains

Section 8.26: First (find)

Section 8.27: Single

Section 8.28: Last

Section 8.29: LastOrDefault

Section 8.30: SingleOrDefault

Section 8.31: FirstOrDefault

Section 8.32: Skip

Section 8.33: Take

Section 8.34: Reverse

Section 8.35: OfType

Section 8.36: Max

Section 8.37: Average

Section 8.38: GroupBy

Section 8.39: ToDictionary

Section 8.40: Union

Section 8.41: ToArray

Section 8.42: ToList

Section 8.43: ElementAt

Section 8.44: ElementAtOrDefault

Section 8.45: SkipWhile

Section 8.46: TakeWhile

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

28

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

26

26

29

29

30

31

Publicité

31

31

32

32

32

32

33

33

33

34

34

34

34

35

35

35

35

36

36

36

36

37

37

37

38

38

38

39

39

39

39

39

40

40

41

42

42

42

42

42

43

Section 8.47: DefaultIfEmpty

Section 8.48: Join

Section 8.49: Left Outer Join

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

43

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

43

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

44

Chapter 9: ForEach

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

46

Section 9.1: Extension method for IEnumerable

Section 9.2: Calling a method on an object in a list

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

46

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

46

Chapter 10: Reflection

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

47

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

Section 10.1: What is an Assembly?

Section 10.2: Compare two objects with reflection

Section 10.3: Creating Object and setting properties using reflection

Section 10.4: How to create an object of T using Reflection

Section 10.5: Getting an attribute of an enum with reflection (and caching it)

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

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

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

48

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

Chapter 11: Expression Trees

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

50

Section 11.1: building a predicate of form field == value

Section 11.2: Simple Expression Tree Generated by the C# Compiler

Section 11.3: Expression for retrieving a static field

Section 11.4: InvocationExpression Class

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

50

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

50

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

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

Chapter 12: Custom Types

Section 12.1: Struct Definition

Section 12.2: Class Definition

Chapter 13: Code Contracts

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

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

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

Section 13.1: Contracts for Interfaces

Section 13.2: Installing and Enabling Code Contracts

Section 13.3: Preconditions

Section 13.4: Postconditions

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

56

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

56

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

56

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

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

Chapter 14: Settings

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

60

Section 14.1: AppSettings from ConfigurationSettings in .NET 1.x

Section 14.2: Reading AppSettings from ConfigurationManager in .NET 2.0 and later

Section 14.3: Introduction to strongly-typed application and user settings support from Visual Studio

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

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

60

60

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

61

Section 14.4: Reading strongly-typed settings from custom section of configuration file

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

62

Chapter 15: Regular Expressions (System.Text.RegularExpressions)

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

65

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

65

Section 15.1: Check if pattern matches input

Section 15.2: Remove non alphanumeric characters from string

Section 15.3: Passing Options

Section 15.4: Match into groups

Section 15.5: Find all matches

Section 15.6: Simple match and replace

Publicité

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

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

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

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

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

65

Chapter 16: File Input/Output

Section 16.1: C# File.Exists()

Section 16.2: VB WriteAllText

Section 16.3: VB StreamWriter

Section 16.4: C# StreamWriter

Section 16.5: C# WriteAllText()

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

67

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

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

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

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

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

Chapter 17: System.IO

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

69

Section 17.1: Reading a text file using StreamReader

Section 17.2: Serial Ports using System.IO.SerialPorts

Section 17.3: Reading/Writing Data Using System.IO.File

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

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

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

70

Chapter 18: System.IO.File class

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

72

47

47

48

48

51

51

54

54

54

58

59

65

65

65

66

67

67

67

67

68

69

69

Section 18.1: Delete a file

Section 18.2: Strip unwanted lines from a text file

Section 18.3: Convert text file encoding

Section 18.4: Enumerate files older than a specified amount

Section 18.5: Move a File from one location to another

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

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

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

72

73

73

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

74

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

74

Chapter 19: Reading and writing Zip files

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

76

Section 19.1: Listing ZIP contents

Section 19.2: Extracting files from ZIP files

Section 19.3: Updating a ZIP file

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

76

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

76

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

76

Chapter 20: Managed Extensibility Framework

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

78

Section 20.1: Connecting (Basic)

Section 20.2: Exporting a Type (Basic)

Section 20.3: Importing (Basic)

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

78

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

78

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

79

Chapter 21: SpeechRecognitionEngine class to recognize speech

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

80

Section 21.1: Asynchronously recognizing speech based on a restricted set of phrases

Section 21.2: Asynchronously recognizing speech for free text dictation

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

80

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

80

Chapter 22: System.Runtime.Caching.MemoryCache (ObjectCache)

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

81

Section 22.1: Adding Item to Cache (Set)

Section 22.2: System.Runtime.Caching.MemoryCache (ObjectCache)

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

81

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

81

Chapter 23: System.Reflection.Emit namespace

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

83

Section 23.1: Creating an assembly dynamically

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

83

Chapter 24: .NET Core

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

86

Section 24.1: Basic Console App

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

86

Chapter 25: ADO.NET

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

87

Section 25.1: Best Practices - Executing Sql Statements

Section 25.2: Executing SQL statements as a command

Section 25.3: Using common interfaces to abstract away vendor specific classes

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

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

Publicité

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

89

Chapter 26: Dependency Injection

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

90

Section 26.1: How Dependency Injection Makes Unit Testing Easier

Section 26.2: Dependency Injection - Simple example

Section 26.3: Why We Use Dependency Injection Containers (IoC Containers)

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

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

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

91

Chapter 27: Platform Invoke

Section 27.1: Marshaling structs

Section 27.2: Marshaling unions

Section 27.3: Calling a Win32 dll function

Section 27.4: Using Windows API

Section 27.5: Marshalling arrays

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

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

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

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

96

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

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

Chapter 28: NuGet packaging system

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

98

Section 28.1: Uninstalling a package from one project in a solution

Section 28.2: Installing a specific version of a package

Section 28.3: Adding a package source feed (MyGet, Klondike, ect)

Section 28.4: Installing the NuGet Package Manager

Section 28.5: Managing Packages through the UI

Section 28.6: Managing Packages through the console

Section 28.7: Updating a package

Section 28.8: Uninstalling a package

Section 28.9: Uninstall a specific version of package

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

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

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

98

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

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

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

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

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

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

87

88

90

90

94

94

95

97

97

98

98

98

99

99

99

100

100

Chapter 29: Globalization in ASP.NET MVC using Smart internationalization for ASP.NET

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

101

Section 29.1: Basic configuration and setup

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

101

Chapter 30: System.Net.Mail

Section 30.1: MailMessage

Section 30.2: Mail with Attachment

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

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

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

104

Chapter 31: Using Progress<T> and IProgress<T>

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

105

Section 31.1: Simple Progress reporting

Section 31.2: Using IProgress<T>

Chapter 32: JSON Serialization

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

105

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

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

Section 32.1: Deserialization using System.Web.Script.Serialization.JavaScriptSerializer

Section 32.2: Serialization using Json.NET

Section 32.3: Serialization-Deserialization using Newtonsoft.Json

Section 32.4: Deserialization using Json.NET

Section 32.5: Dynamic binding

Section 32.6: Serialization using Json.NET with JsonSerializerSettings

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

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

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

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

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

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

107

Chapter 33: JSON in .NET with Newtonsoft.Json

Section 33.1: Deserialize an object from JSON text

Section 33.2: Serialize object into JSON

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

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

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

110

Chapter 34: XmlSerializer

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

111

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

Section 34.1: Formatting: Custom DateTime format

Section 34.2: Serialize object

Section 34.3: Deserialize object

Section 34.4: Behaviour: Map array name to property (XmlArray)

Section 34.5: Behaviour: Map Element name to Property

Section 34.6: Eciently building multiple serializers with derived types specified dynamically

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

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

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

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

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

112

Chapter 35: VB Forms

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

115

Section 35.1: Hello World in VB.NET Forms

Section 35.2: For Beginners

Section 35.3: Forms Timer

Chapter 36: JIT compiler

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

Publicité

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