Good to know!? C# 5.0 Key Features Reference – Part 2


This are copy notes from the main key-points found in the MS 70-483 prep book. This might be useful to someone. A checklist of things in C#.

Also check out the exam link and the actual book:

Exam Ref 70-483: Programming in C#

Implement multithreading and asynchronous processing
Using multiple threads can improve responsiveness and enables you to make use of multiple processors.
The Thread class can be used if you want to create your own threads explicitly. Otherwise, you can use the ThreadPool to queue work and let the runtime handle things.
A Task object encapsulates a job that needs to be executed. Tasks are the recommended way to create multithreaded code.
The Parallel class can be used to run code in parallel.
PLINQ is an extension to LINQ to run queries in parallel.
The new async and await operators can be used to write asynchronous code more easily.
Concurrent collections can be used to safely work with data in a multithreaded (concurrent access) environment.
Manage multithreading
When accessing shared data in a multithreaded environment, you need to synchronize
access to avoid errors or corrupted data.
 Use the lock statement on a private object to synchronize access to a piece of code.
 You can use the Interlocked class to execute simple atomic operations.
 You can cancel tasks by using the CancellationTokenSource class with a
Implement program flow
 Boolean expressions can use several operators: ==, !=, <, >, <=, >=, !. Those operators
can be combined together by using AND (&&), OR (||) and XOR (^).
 You can use the if-else statement to execute code depending on a specific condition.
 The switch statement can be used when matching a value against a couple of options.
56 Chapter 1 Manage program flow
The for loop can be used when iterating over a collection where you know the number of iterations in advance.
A while loop can be used to execute some code while a condition is true; do-while should be used when the code should be executed at least once.
foreach can be used to iterate over collections.
Jump statements such as break, goto, and continue can be used to transfer control to another line of the program.
Create and implement events and callbacks method
 Delegates can be instantiated, passed around, and invoked.
 Lambda expressions, also known as anonymous methods, use the => operator and
form a compact way of creating inline methods.
 Events are a layer of syntactic sugar on top of delegates to easily implement the
publish-subscribe pattern.
 Events can be raised only from the declaring class. Users of events can only remove
and add methods the invocation list.
 You can customize events by adding a custom event accessor and by directly using the
underlying delegate type.
Implement exception handling
 In the .NET Framework, you should use exceptions to report errors instead of error
 Exceptions are objects that contain data about the reason for the exception.
 You can use a try block with one or more catch blocks to handle different types of
 You can use a finally block to specify code that should always run after, whether or not
an exception occurred.
 You can use the throw keyword to raise an exception.
 You can define your own custom exceptions when you are sure that users of your code
will handle it in a different way. Otherwise, you should use the standard .NET Framework
Create types
 Types in C# can be a value or a reference type.
 Generic types use a type parameter to make the code more flexible.
 Constructors, methods, properties, fields, and indexer properties can be used to create
a type.
 Optional and named parameters can be used when creating and calling methods.
 Overloading methods enable a method to accept different parameters.
 Extension methods can be used to add new functionality to an existing type.
 Overriding enables you to redefine functionality from a base class in a derived class.
Consume types
 Boxing occurs when a value type is treated as a reference type.
When converting between types, you can have an implicit or an explicit conversion.
An explicit conversion is called casting and requires special syntax.
You can create your own implicit and explicit user-defined conversions.
The .NET Framework offers several helper methods for converting types.
The dynamic keyword can be used to ease the static typing of C# and to improve
interoperability with other languages.
Enforce encapsulation
Encapsulation is important in object-oriented software. It hides internal details and
improves the usability of a type.
Data can be encapsulated with a property.
Properties can have both a get and a set accessor that can run additional code, commonly
known as getters and setters.
Types and type elements can have access modifiers to restrict accessibility.
The access modifiers are public, internal, protected, protected, internal, and private.
Explicit interface implementation can be used to hide information or to implement
interfaces with duplicate member signatures.
Create and implement a class hierarchy
Inheritance is the process in which a class is derived from another class or from an interface.
An interface specifies the public elements that a type must implement.
A class can implement multiple interfaces.
A base class can mark methods as virtual; a derived class can then override those methods to add or replace behavior.
A class can be marked as abstract so it can’t be instantiated and can function only as a base class.
A class can be marked as sealed so it can’t be inherited.
The .NET Framework offers default interfaces such as IComparable, IEnumerable, IDisposable and IUnknown.
Find, execute, and create types at runtime by using reflection
A C# assembly stores both code and metadata.
Attributes are a type of metadata that can be applied in code and queried at runtime.
Reflection is the process of inspecting the metadata of a C# application.
Through reflection you can create types, call methods, read properties, and so forth.
The CodeDOM can be used to create a compilation unit at runtime. It can be compiled or converted to a source file.
Expression trees describe a piece of code. They can be translated to something else (for example, SQL) or they can be compiled and executed.
Manage the object life cycle
 Memory in C# consists of both the stack and the heap.
 The heap is managed by the garbage collector.
 The garbage collector frees any memory that is not referenced any more.
 A finalizer is a special piece of code that’s run by the garbage collector when it removes
an object.
 IDisposable can be implemented to free any unmanaged resources in a deterministic
 Objects implementing IDisposable can be used with a using statement to make sure
they are always freed.
 A WeakReference can be used to maintain a reference to items that can be garbage
collected when necessary.
Manipulate strings
A string is an immutable reference type.
When doing a lot of string manipulations, you should use a StringBuilder.
The String class offers a lot of methods for dealing with strings like IndexOf, LastIndexOf, StartsWith, EndsWith, and Substring.
Strings can be enumerated as a collection of characters.
Formatting is the process of displaying an object as a string.
You can use format strings to change how an object is converted to a string.
You can implement formatting for your own types.
Validate application input
 Validating application input is important to protect your application against both
mistakes and attacks.
 Data integrity should be managed both by your application and your data store.
 The Parse, TryParse, and Convert functions can be used to convert between types.
 Regular expressions, or regex, can be used to match input against a specified pattern
or replace specified characters with other values.
 When receiving JSON and XML files, it’s important to validate them using the built-in
types, such as with JavaScriptSerializer and XML Schemas.
Perform symmetric and asymmetric
 An asymmetric algorithm uses a public and private key that are mathematically linked.
 Hashing is the process of converting a large amount of data to a smaller hash code.
 Digital certificates can be used to verify the authenticity of an author.
 CAS are used to restrict the resources and operations an application can access and
 System.Security.SecureString can be used to keep sensitive string data in memory.
Manage assemblies
An assembly is a compiled unit of code that contains metadata.
An assembly can be strongly signed to make sure that no one can tamper with the content.
Signed assemblies can be put in the GAC.
An assembly can be versioned, and applications will use the assembly version they were developed with. It’s possible to use configuration files to change these bindings.
A WinMD assembly is a special type of assembly that is used by WinRT to map non-native languages to the native WinRT components.
Debug an application
Visual Studio build configurations can be used to configure the compiler.
 A debug build outputs a nonoptimized version of the code that contains extra instructions
to help debugging.
 A release build outputs optimized code that can be deployed to a production
 Compiler directives can be used to give extra instructions to the compiler. You can use
them, for example, to include code only in certain build configurations or to suppress
certain warnings.
 A program database (PDB) file contains extra information that is required when debugging
an application.
Implement diagnostics in an application
 Logging and tracing are important to monitor an application that is in production and
should be implemented right from the start.
 You can use the Debug and TraceSource classes to log and trace messages. By configuring
different listeners, you can configure your application to know which data to send
 When you are experiencing performance problems, you can profile your application to
find the root cause and fix it.
 Performance counters can be used to constantly monitor the health of your applications.
Perform I/O operations
 You can work with drives by using Drive and DriveInfo.
 For folders, you can use Directory and DirectoryInfo.
 File and FileInfo offer methods to work with files.
 The static Path class can help you in creating and parsing file paths.
 Streams are an abstract way of working with a series of bytes.
 There are many Stream implementations for dealing with files, network operations,
and any other types of I/O.
 Remember that the file system can be accessed and changed by multiple users at the
same time. You need to keep this in mind when creating reliable applications.
 When performing network requests, you can use the WebRequest and WebResponse
classes from the System.Net namespace.
 Asynchronous I/O can help you create a better user experience and a more scalable
Consume data
 ADO.NET uses a provider model that enables you to connect to different types of
 You use a DbConnection object to create a connection to a database.
 You can execute queries that create, update, read, and delete (CRUD) data from a
 When creating queries it’s important to use parameterized queries so you avoid SQL
Objective 4.3: Query and manipulate data and objects by using LINQ CHAPTER 4 291
You can consume a web service from your application by creating a proxy for it.
You can work with XML by using the XmlReader, XmlWriter, XPathNavigator, and XmlDocument classes.
Query and manipulate data and objects by using LINQ
 LINQ, which stands for Language Integrated Query, is a uniform way of writing queries
against multiple data sources.
 Important language features when working with LINQ queries are implicit typing, object
initialization syntax, lambdas, extension methods, and anonymous types.
 You can use LINQ with a method-based syntax and the query syntax.
 LINQ queries are deferred-execution, which means that the query executes when it is
first iterated.
 You can use LINQ to XML to query, create, and update XML.
Serialize and deserialize data
 Serialization is the process of transforming an object to a flat file or a series of bytes.
 Deserialization takes a series of bytes or a flat file and transforms it into an object.
 XML serialization can be done by using the XmlSerializer.
 You can use special attributes to configure the XmlSerializer.
 Binary serialization can be done by using the BinaryFormatter class.
 WCF uses another type of serialization that is performed by the DataContractSerializer.
 JSON is a compact text format that can be created by using the DataContractJsonSerializer.
Store data in and retrieve data from collections
 The .NET Framework offers both generic and nongeneric collections. When possible,
you should use the generic version.
 Array is the most basic type to store a number of items. It has a fixed size.
 List is a collection that can grow when needed. It’s the most-used collection.
 Dictionary stores and accesses items using key/value pairs.
 HashSet stores unique items and offers set operations that can be used on them.
 A Queue is a first-in, first-out (FIFO) collection.
 A Stack is a first-in, last-out (FILO) collection.
 You can create a custom collection by inheriting from a collection class or inheriting
from one of the collection interfaces.

Good to know!? C# 5.0 Key Features Reference – Part 1

Hi here is my reference links to C# 5.0. hope it helps someone!! 🙂

Manage program flow – Multithreading, asynchronous processing, events and callbacks, exception handling

Asynchronous Programming with Async and Await (C# and Visual Basic)
C# Operators
CancellationTokenSource Class
Covariance and contravariance
Delegate.GetInvocationList Method
Exceptions and Exception Handling (C# Programming Guide)
Flow Control
Interlocked Methods
Jump Statements
Lambda Expressions (C# Programming Guide)
lock Statement (C# Reference)
Parallel LINQ (PLINQ)
System.Collections.Concurrent Namespace
System.Threading.Tasks Namespace
Thread Class
ThreadLocal<T> Class
ThreadPool Class
Using Delegates (C# Programming Guide)
Volatile Class

Types – Create types, Consume types, encapsulation, class hierarchy, reflection, string/text manipulation

Abstract and Sealed Classes and Class Members (C# Programming Guide)
Access Modifiers (C# Reference)
Anonymous Methods (C# Programming Guide)
Attributes (C# and Visual Basic)
Basic String Operations
Best Practices for Using Strings in the .NET Framework
C# – Understanding The NET Garbage Collector
Constructor Design
CultureInfo Class
Enumeration Types (C# Programming Guide)
ExceptionDispatchInfo Class
Expression Trees (C# and Visual Basic)
Extension Methods (C# Programming Guide)
Finalize Methods and Destructors
Generics (C# Programming Guide)
IDisposable Interface
Immutability in C# Part One: Kinds of Immutability
Interfaces (C# Programming Guide)
IUnknown interface
Managed Extensibility Framework (MEF)
Operator Keywords (C# Reference)
Properties (C# Programming Guide)
The Big Ball of Mud and Other Architectural Disasters
Types (C# Programming Guide)
unsafe (C# Reference)
Using Conversion Operators (C# Programming Guide)
using Statement
Using the CodeDOM
Using the StringBuilder Class
Using Type dynamic (C# Programming Guide)
Walkthrough: Creating and Using Dynamic Objects (C# and Visual Basic)
WeakReference Class
virtual (C# Reference)

Application management and development and security – Validation, encryption, assemblies, debugging, diagnostics

.NET Framework Cryptography Model
.NET Framework Cryptography Model
.NET Framework Regular Expressions
Analyzing Application Performance by Using Profiling Tools
Beginners Guide to Performance Profiling
C# Preprocessor Directives
Code Access Security
Conditional (C# Programming Guide)
Debug Class
Debugger Roadmap
Debugging, Tracing, and Profiling
EventLog Class
Gacutil.exe (Global Assembly Cache Tool)
JavaScriptSerializer Class
Makecert.exe (Certificate Creation Tool)
Object.GetHashCode Method
Parsing Strings
PDB Files (C# and Visual Basic)
PerformanceCounter Class
Regular Expression Language – Quick Reference
Runtime Profiling
SecureString Class
Sn.exe (Strong Name Tool)
Step 1: Examining the Configuration Files
Stopwatch Class
Symbol Server and Symbol Stores
System.ComponentModel.DataAnnotations Namespace
System.Diagnostics Namespace
TraceSource Class
Using DebuggerDisplay Attribute
XML Schema Definition Tool (Xsd.exe)

Data access and manipulation – I/O operations, consume data, LINQ data manipulation, serialization, collections

101 LINQ Samples
ADO.NET Architecture
ADO.NET DataSets
Arrays Tutorial
BinaryFormatter Class
Collections (C# and Visual Basic)
Collections and Data Structures
ConfigurationManager Class
DataContractSerializer Class
DataContractSerializer Sample
DbConnection, DbCommand and DbException
DbConnectionStringBuilder Class
Directory Class
DirectorySecurity Class
DriveInfo Class
Encoding Class
Entity Framework
Examples of XML Serialization
File Class
FileInfo Class
FileStream Class
Getting Started with LINQ in C#
How to: Extend the Async Walkthrough by Using Task.WhenAll (C# and Visual Basic)
How to: Parse XML with XmlReader
HttpClient Class
Implementing an Implicit Transaction using Transaction Scope
MemoryStream Class
NonSerializedAttribute Class
Parameters and Execution Plan Reuse
Path Class
Retrieving and Modifying Data in ADO.NET
Serialization (C# and Visual Basic)
SQL Injection
SqlCommand Class
SqlConnection Class
SqlDataReader Class
StreamReader Class
StreamWriter Class
System.IO.Compression Namespace
System.Net Namespace
Using Data Contracts
Using the DbContext API
var (C# Reference)
Web.config Transformation Syntax for Web Project Deployment Using Visual Studio
WebRequest Class
WebResponse Class
What Is Windows Communication Foundation
XML Standards Reference
XmlAttributes.XmlArray Property
XmlAttributes.XmlArrayItems Property
XmlAttributes.XmlAttribute Property
XmlAttributes.XmlElements Property
XmlAttributes.XmlIgnore Property
XmlSerializer Class

Other important links:

Visual Studio Plugins
Database ConnectionStrings

Lost files in Sharepoint Recycle Bin that can not be restored

This time I am writing a post about a problem that a client of mine had.

The problem is rather simple BUT required allot of time and energy to fix the problem or rather in my case to recover the items:

The client at some point by someone deleted files which ended up in the recycle bin. Later the client wanted to restore these files BUT was unable to. Sharepoint was telling that the restore operation failed even though you where able to clearly see that the files where available to restore in the Recycle bin of the site.

NOTICE: Before you start to try any of the things written in this post take notice TO NOT DO these things in you client product/live environment. If you chose to do so you will cause your client to lose their MS support. MS does not support manipulation of the content databases directly to SQL Server. So take a database backup, restore it in a test or development environment and fix you problem there.

NOTICE: Use this code at your own risk. The code does some crazy stuff directly to SharePoint content database. I will not take any responsibility on anything that you do with the application and/or any part of it and it’s code.

Here are the steps which I ended up taking and a more detailed explanation on them:

  1. Trying to restore the items through the object model
  2. Investigating the Sharepoint content database
  3. Trying to restore the files by fixing content database Recycle Bin table missing information in the files
  4. Trying to recover the files directly from the AllDocsStream table

Lets start from the first item and advance from there:

1. Trying to restore the items through the object model

This is basically the same as the UI BUT I decided to give it a quick try since nothing from the UI worked and sometimes you can do things through code that you can not do through the UI.

Long story short the object model did not offer much help and any restore operation failed again or nothing happened. For this a created a forms application(code is included at the end of this post).

Here is how the final version of the application looks like: appppp

In this first step I create the functionality in the “Blue” area. These where the object model fixes which did not help.

There basic functionality are provided which are in the “Blue” area:

  1. Restore the selected items from recycle bin
  2. Delete selected items
  3. And move them to the Site Collection Recycle Bin

2. Investigating the Sharepoint content database

At this point because the object model and the UI did not do what they are supposed to I simply gave up on using the “preferred” way of Microsoft and went directly into the content database. My first destination was the “Recycle Bin” table.

After a few moments of careful examination of the clients content database table I noticed that the documents that can not be restored have 4 table fields which are null while other data row in the table all have data in them.

The fields which where null where:

  1. ListId
  2. ListItemId
  3. AuthorId
  4. ListDirName

So I decided to create a code that would restore the missing values. I decided that the AuthorId and ListItemId would be defined by me during the code(since they could be anything any existing user and any integer number that is large enough not to conflict with existing ID enumeration in the target list) and the ListId and ListDirName are to be retrieved through the object model. This had to be done by retrieving the values of the following fields WebId, SiteId and DirName. In the code used these values to get the site, web and the list. After I got the list it was easy to just update the table with the list ID and the rootfolder ServerRelative URL into the ListDirName field.

You can find the code for this in the button named: “Repair Recycle Bin Items(DB)”

3. Trying to restore the files by fixing content database Recycle Bin table missing information in the files

Unfortunately what I did in step two was not enough because the sites and lists did not exist anymore for these deleted files that needed to be restored from the bin.

The next step was to try to update the following tables so that the document/file would be restored to a different web and list that actually exists.

I had to make modifications to the following tables:

  1. AllDocs = Data regarding the file/document itself, where it is, it’s parent, state etc
  2. AllDocStreams = The actual binary data of the file
  3. RecycleBin = The data of a deleted file that has ended up in the recycle bin

By retrieving the DocId value from RecycleBin table from a file that can not be restored you can correlate to the documents information in the AllDocs and AllDocStream.

This step is very complex and I due to time constraints I did not get it to work properly. Some piece of data was missing to get the file restored to another location. I managed to get the file restored BUT while the list showed that it had files in it you could not access the file. So something was missing from somewhere.

You can check the code for this in the button: “Repair Recycle Bin items(DB) to inserted URL”

4. Trying to recover the files directly from the AllDocsStream table

Finally the last step was to go directly into the AllDocsStream with ADO .NET, read the binary data, retrieve the documents real name from the RecycleBin table and write that data into the hard drive.

This was SUCCESSFUL!!! It worked like a charm and the client was happy to get their files restored.

In the AllDocsStrems table the most important field is the “Content” field. This is the actual binary data that you need to retrieve and use it to write it into your hard drive.

From the RecycleBin table you need the value from the LeafName field.

Using these two piece of information you can reconstruct the file.

Here is the code that solved my problem, you can find it behind the button: “Recover files from DB”


String connectionString = ConfigurationSettings.AppSettings[“DBConnectionString”];

            SqlDataReader rdr = null;

            this.textBox2.Text += “Opening connection to DB”;

            using (SqlConnection connection = new SqlConnection(connectionString))



                this.textBox2.Text += “Connection opened”;

                List<RecycleBinDBItem> items = new List<RecycleBinDBItem>();



                    // Get all such items which can not be restored properly from the UI or by object model

                    SqlCommand cmd = new SqlCommand(“SELECT * FROM RecycleBin WHERE ListDirName IS NULL”, connection);


                    rdr = cmd.ExecuteReader();

                    this.textBox2.Text += “Select clause executed”;


                    while (rdr.Read())


                        this.textBox2.Text += “Reading line: \n”;

                        this.textBox2.Text += rdr[“DirName”].ToString();

                        items.Add(new RecycleBinDBItem(rdr[“DocId”].ToString(), rdr[“LeafName”].ToString()));





                    // close the reader

                    if (rdr != null)




                    int itemIDCounter = 5000;

                    int authorID = 1;

                    // Loop through every recycle bin items that is missing needed field data from the RecycleBin table to be restored properly

                    foreach (RecycleBinDBItem item in items)




                            SqlCommand cmd = new SqlCommand(String.Format(“SELECT Content FROM AllDocStreams WHERE Id = ‘{0}'”, item.docID ), connection);


                            // 2. Call Execute reader to get query results

                            byte [] bArray = (byte[])cmd.ExecuteScalar();

                            // Craete the file and write the binary data from the Database

                            FileStream fs = File.Create(item.fileName, (int)bArray.Length);

                            fs.Write(bArray, 0, bArray.Length);




                        catch (SqlException sqlex)


                            this.textBox2.Text += sqlex.Message + ” #######\n” + sqlex.StackTrace;


                        catch (Exception ex)


                            this.textBox2.Text += ex.Message + ” #######\n” + ex.StackTrace;




                            if (rdr != null)








            this.textBox2.Text += “DB operations done!”;

Final words;

Hope this might help someone else who is suffering from this problem. The application and it’s code is not the prettiest BUT I did what I could in the time that was given to me and also what the client is ready to spend to solve this problem. It’s only a tool and should be treated as such.

FINAL NOTICE: Use this code at your own risk. The code does some crazy stuff directly to SharePoint content database. I will not take any responsibility on anything that you do with the application and/or any part of it and it’s code.

Check out the CodePlex Project for the code: