All .NET developers who uses the BaseClassLibraries can read this post to know what’s New in the .NET FW 4 BCL. Its not the scope of this post to cover all the features but you can read them on BCL team blog at blogs.msdn.com/bclteam.
The following are the features that you can explore in BCL
- Code Contracts
- Parallel extensions
- Tuples
- File IO improvements
- Memory mapped files
- Sorted Set Collection
Code Contracts
One of the major features added to the .NET FW BCL in version 4 is code contracts. Classes and methods should explicitly state that what they require and what they guarantee if those requirements are met, i.e. their contracts.
Parts of the Code Contract System
First part static method calls
Contracts are static method calls and part of the method signature. We can find these static method calls in System.Diagnostics.Contracts namespace.
Second part is ccewrite.exe tool modifies the MSIL and places the contract checks where they belong.
Third part is static checker cccheck.exe, that examines code without executing it and to prove that all of the contracts are satisfied.
What is for developer?
This feature allows the developers to specify pre-conditions, post-conditions and object invariants in your code.
Specifying pre-conditions
public Boolean TryAddCashToAccount(Account account) { Contract.Requires<NullreferenceException>(account!= null); Contract.Requires(account.amount >= 0); }
The Requires method is to include a particular condition must be true upon entry to the method. The benefit of this type of precondition is it always performs the runtime check.
Specifying post-conditions
There two types of post-conditions- guarantees about normal returns and guarantees about exceptional returns.
public Boolean TryAddCashToAccount(Account account)
{
Contract.Ensures(Balance >= 10000);
}
The ensures method simply guarantees a condition to be true at normal return from a method.
Specifying Object Variants
Object Variant is object wide contract can be thought of as post-conditions on every single public member of the object. Object variants are used with the Invariant method on the Contract class.
[ContractInvariantMethod] protected void ObjectInvariant() { Contract.Invariant(Balance >= 10000); }
Tuples
Another addition to .NET BCL is support for tuples introduces a new type called System.Tuple. The System.Tuple is fixed-size collection of heterogeneous data. tuple has a fixed size and can not be changed. It is able to guarantee strong-typing for each element in it.
static void Main(string[] args) { object[] t = new object[2]; t[0] = "Test"; t[1] = "Touple"; PrintStringAndInt((string) t[0], (int) t[1]); } static void PrintStringAndInt(string s, int i) { Console.WriteLine("{0} {1}", s, i); }
The above code would compile but at runtime call to PrintStringAndInt throws an InvalidCastException when we tried to cast string to an integer.
The above code with Touple will look like
static void Main(string[] args) { Tuple<string, int> t = new Tuple<string, int>("Test", 4); PrintStringAndInt(t.Item1, t.Item2); } static void PrintStringAndInt(string s, int i) { Console.WriteLine("{0} {1}", s, i); }
With the touple type we have been able to remove the casting operators from code and give you a better code checking at compile time.
A touple is a data-structure that can be used like annonymous classes which can be created on the fly.
I will discuss more about the above listed features in my upcoming posts.
source: MSDN
Share this post : |
[…] .NET FW 4 BCL Features Some Initial Thoughts On Agile Developer Skills And CertificationCode Coverage for Concurrency How […]
[…] can get the overview on what’s new in .NET FW BCL by reading my post .NET Framework 4 BCL. .NET Framework 4 adds a new collection to System.Collections.Generic, named Sorted […]