×
You don’t have to be great to start, but you have to start to be great.
--Your friends at LectureNotes
Close

C# and .NET

by Debasish NandaDebasish Nanda
Type: NoteInstitute: Biju Patnaik University of Technology BPUT Course: MCA Specialization: Master of Computer ApplicationsOffline Downloads: 27Views: 488Uploaded: 2 months ago

Share it with your friends

Suggested Materials

Leave your Comments

Contributors

Debasish Nanda
Debasish Nanda
.NET Framework .NET Framework Notes for Professionals 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# ........................................................................................................................................ 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: Strings ......................................................................................................................................................... 5 Section 2.1: Count characters ....................................................................................................................................... 5 Section 2.2: Count distinct characters ......................................................................................................................... 5 Section 2.3: Convert string to/from another encoding ............................................................................................. 5 Section 2.4: Comparing strings .................................................................................................................................... 6 Section 2.5: Count occurrences of a character .......................................................................................................... 6 Section 2.6: Split string into fixed length blocks ......................................................................................................... 6 Section 2.7: Object.ToString() virtual method ............................................................................................................ 7 Section 2.8: Immutability of strings ............................................................................................................................. 8 Chapter 3: DateTime parsing ................................................................................................................................. 9 Section 3.1: ParseExact .................................................................................................................................................. 9 Section 3.2: TryParse .................................................................................................................................................. 10 Section 3.3: TryParseExact ......................................................................................................................................... 12 Chapter 4: Dictionaries ............................................................................................................................................ 13 Section 4.1: Initializing a Dictionary with a Collection Initializer .............................................................................. 13 Section 4.2: Adding to a Dictionary ........................................................................................................................... 13 Section 4.3: Getting a value from a dictionary ......................................................................................................... 13 Section 4.4: Make a Dictionary<string, T> with Case-Insensivitve keys ................................................................. 14 Section 4.5: IEnumerable to Dictionary (≥ .NET 3.5) ................................................................................................ 14 Section 4.6: Enumerating a Dictionary ...................................................................................................................... 14 Section 4.7: ConcurrentDictionary<TKey, TValue> (from .NET 4.0) ...................................................................... 15 Section 4.8: Dictionary to List ..................................................................................................................................... 16 Section 4.9: Removing from a Dictionary ................................................................................................................. 16 Section 4.10: ContainsKey(TKey) ............................................................................................................................... 17 Section 4.11: ConcurrentDictionary augmented with Lazy'1 reduces duplicated computation ........................... 17 Chapter 5: Collections .............................................................................................................................................. 19 Section 5.1: Using collection initializers ...................................................................................................................... 19 Section 5.2: Stack ........................................................................................................................................................ 19 Section 5.3: Creating an initialized List with Custom Types .................................................................................... 20 Section 5.4: Queue ....................................................................................................................................................... 22 Chapter 6: ReadOnlyCollections ......................................................................................................................... 24 Section 6.1: Creating a ReadOnlyCollection .............................................................................................................. 24 Section 6.2: Updating a ReadOnlyCollection ........................................................................................................... 24 Section 6.3: Warning: Elements in a ReadOnlyCollection are not inherently read-only ...................................... 24
Chapter 7: Stack and Heap ................................................................................................................................... 26 Section 7.1: Value types in use ................................................................................................................................... 26 Section 7.2: Reference types in use ........................................................................................................................... 26 Chapter 8: LINQ ........................................................................................................................................................... 28 Section 8.1: SelectMany (flat map) ............................................................................................................................ 28 Section 8.2: Where (filter) ........................................................................................................................................... 29 Section 8.3: Any ........................................................................................................................................................... 29 Section 8.4: GroupJoin ................................................................................................................................................ 30 Section 8.5: Except ...................................................................................................................................................... 31 Section 8.6: Zip ............................................................................................................................................................ 31 Section 8.7: Aggregate (fold) ..................................................................................................................................... 31 Section 8.8: ToLookup ................................................................................................................................................ 32 Section 8.9: Intersect ................................................................................................................................................... 32 Section 8.10: Concat .................................................................................................................................................... 32 Section 8.11: All ............................................................................................................................................................. 32 Section 8.12: Sum ......................................................................................................................................................... 33 Section 8.13: SequenceEqual ...................................................................................................................................... 33 Section 8.14: Min ........................................................................................................................................................... 33 Section 8.15: Distinct .................................................................................................................................................... 34 Section 8.16: Count ...................................................................................................................................................... 34 Section 8.17: Cast ......................................................................................................................................................... 34 Section 8.18: Range ..................................................................................................................................................... 34 Section 8.19: ThenBy ................................................................................................................................................... 35 Section 8.20: Repeat ................................................................................................................................................... 35 Section 8.21: Empty ..................................................................................................................................................... 35 Section 8.22: Select (map) .......................................................................................................................................... 35 Section 8.23: OrderBy ................................................................................................................................................. 36 Section 8.24: OrderByDescending ............................................................................................................................. 36 Section 8.25: Contains ................................................................................................................................................. 36 Section 8.26: First (find) .............................................................................................................................................. 36 Section 8.27: Single ..................................................................................................................................................... 37 Section 8.28: Last ........................................................................................................................................................ 37 Section 8.29: LastOrDefault ....................................................................................................................................... 37 Section 8.30: SingleOrDefault .................................................................................................................................... 38 Section 8.31: FirstOrDefault ........................................................................................................................................ 38 Section 8.32: Skip ......................................................................................................................................................... 38 Section 8.33: Take ....................................................................................................................................................... 39 Section 8.34: Reverse .................................................................................................................................................. 39 Section 8.35: OfType ................................................................................................................................................... 39 Section 8.36: Max ......................................................................................................................................................... 39 Section 8.37: Average ................................................................................................................................................. 39 Section 8.38: GroupBy ................................................................................................................................................. 40 Section 8.39: ToDictionary .......................................................................................................................................... 40 Section 8.40: Union ...................................................................................................................................................... 41 Section 8.41: ToArray .................................................................................................................................................. 42 Section 8.42: ToList ..................................................................................................................................................... 42 Section 8.43: ElementAt .............................................................................................................................................. 42 Section 8.44: ElementAtOrDefault ............................................................................................................................. 42 Section 8.45: SkipWhile ............................................................................................................................................... 42 Section 8.46: TakeWhile .............................................................................................................................................. 43
Section 8.47: DefaultIfEmpty ...................................................................................................................................... 43 Section 8.48: Join ........................................................................................................................................................ 43 Section 8.49: Left Outer Join ...................................................................................................................................... 44 Chapter 9: ForEach .................................................................................................................................................... 46 Section 9.1: Extension method for IEnumerable ....................................................................................................... 46 Section 9.2: Calling a method on an object in a list ................................................................................................. 46 Chapter 10: Reflection .............................................................................................................................................. 47 Section 10.1: What is an Assembly? ........................................................................................................................... 47 Section 10.2: Compare two objects with reflection .................................................................................................. 47 Section 10.3: Creating Object and setting properties using reflection ................................................................... 48 Section 10.4: How to create an object of T using Reflection .................................................................................. 48 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 .......................................................................................... 50 Section 11.2: Simple Expression Tree Generated by the C# Compiler .................................................................... 50 Section 11.3: Expression for retrieving a static field .................................................................................................. 51 Section 11.4: InvocationExpression Class ................................................................................................................... 51 Chapter 12: Custom Types ...................................................................................................................................... 54 Section 12.1: Struct Definition ...................................................................................................................................... 54 Section 12.2: Class Definition ...................................................................................................................................... 54 Chapter 13: Code Contracts ................................................................................................................................... 56 Section 13.1: Contracts for Interfaces ........................................................................................................................ 56 Section 13.2: Installing and Enabling Code Contracts .............................................................................................. 56 Section 13.3: Preconditions ......................................................................................................................................... 58 Section 13.4: Postconditions ........................................................................................................................................ 59 Chapter 14: Settings .................................................................................................................................................. 60 Section 14.1: AppSettings from ConfigurationSettings in .NET 1.x ........................................................................... 60 Section 14.2: Reading AppSettings from ConfigurationManager in .NET 2.0 and later ....................................... 60 Section 14.3: Introduction to strongly-typed application and user settings support from Visual Studio ................................................................................................................................................................................ 61 Section 14.4: Reading strongly-typed settings from custom section of configuration file ................................. 62 Chapter 15: Regular Expressions (System.Text.RegularExpressions) .............................................. 65 Section 15.1: Check if pattern matches input ............................................................................................................ 65 Section 15.2: Remove non alphanumeric characters from string .......................................................................... 65 Section 15.3: Passing Options ..................................................................................................................................... 65 Section 15.4: Match into groups ................................................................................................................................. 65 Section 15.5: Find all matches .................................................................................................................................... 65 Section 15.6: Simple match and replace ................................................................................................................... 66 Chapter 16: File Input/Output ............................................................................................................................... 67 Section 16.1: C# File.Exists() ......................................................................................................................................... 67 Section 16.2: VB WriteAllText ...................................................................................................................................... 67 Section 16.3: VB StreamWriter .................................................................................................................................... 67 Section 16.4: C# StreamWriter ................................................................................................................................... 67 Section 16.5: C# WriteAllText() ................................................................................................................................... 68 Chapter 17: System.IO .............................................................................................................................................. 69 Section 17.1: Reading a text file using StreamReader .............................................................................................. 69 Section 17.2: Serial Ports using System.IO.SerialPorts ............................................................................................. 69 Section 17.3: Reading/Writing Data Using System.IO.File ....................................................................................... 70 Chapter 18: System.IO.File class .......................................................................................................................... 72

Lecture Notes