Click here to Skip to main content
11,933,037 members (57,852 online)
Click here to Skip to main content
Add your own
alternative version


527 bookmarked

Five Great .NET Framework 4.5 Features

, 6 Feb 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
In this article I have picked my five favorite features introduced in the .NET 4.5 core framework.

Table of contents


It has been almost a year since .NET 4.5 got released. But the problems with most of the recent Microsoft releases have been communication with .NET developers. Only one or two features are known to developers and other features just stay on MSDN and end up becoming simple documents.

For example, the time you ask a .NET developer what is new in the core framework .NET 4.5 most of them will just say async and awaitt (at least with people whom I have interacted have just talked about those features).

Again it’s very difficult to run through all the new features. Because the features may not sound interesting depending on what you are working currently on..

So in this article I have picked my five favorite features introduced in .NET 4.5 core. Again it’s possible my favorites cannot be your favorites. But what I have done is while choosing these features I kept in mind the larger .NET community and I hope I have met that expectation.

Note: This article does not discuss the new features in ASP.NET, WCF, WPF, WWF etc. It only talks about new features related to the core.

Feature 1: async and await (code markers)

This feature has been oversold and every .NET evangelist has talked about it. But this is still my favorite and you will come to know why in a few lines from here.

async and await are markers which mark code positions from where control should resume after a task (thread) completes.

Let us try to make sense of the above statement by understanding the below code. If you see the flow of the below code:

  1. Method() gets called from the Static void main() entry point.
  2. Method() spawns a Task (thread) LongTask which waits for 10 seconds.
  3. At the same time the control comes back to Method() to execute the remaining code after the task was called. In other words as the invocation is multi-threaded (Task.Run…),  LongTask is also running i.e., waiting for 10 seconds and the remaining code of your Method() is also executed.

Now in certain scenarios we want step 3 to behave differently. We want that after LongTask() finishes execution, the control should go back to Method to execute the remaining code. The async and await keywords help to achieve the above behavior.

Now there are three important points to remember about the async and await keywords:

  1. async and await are pair keywords. You cannot use them in a standalone manner.
  2. async is marked on a method. This keyword is just an indicator saying that this method will have the await keyword.
  3. The await keyword marks the position from where the task should resume. So you will always find this keyword in conjunction with Task.

Below is a modified version of the previously discussed code where we have applied async and await. All the other steps remain the same but “Step 3” is executed after “Step 2” completes. In simple words the control comes back to Method() after the task has finished operation.

Now that you have read about “async” and “await”, let me put a cross question. The above behavior can be also achieved by using Task.Wait or Task.ContinueWith, so how do they differ? I am leaving this question as a home work for you.

Feature 2: Zip facility (Zip compression)

Zip is one of the most accepted archive file formats. Zip format is supported in almost all operating systems with some built-in name.

  • In Windows operating system it’s implemented by the name “Compressed folders”.
  • In MAC OS it’s implemented by the name “Archive utility”.

Now in .NET we did not have built-in support for implementing Zip compression. Many developers where using third party components like “DotnetZip”. In .NET 4.5, the Zip feature is baked in the framework itself, inside the namespace System.IO.Compression.

The first step is you need to reference two namespaces:

  • System.IO.Compression.FileSystem
  • System.IO.Compression

The next step is to import the below two namespaces:

using System.IO.Compression;

If you want to Zip files from a folder you can use the CreateFromDirectory function as shown below.


If you wish to unzip, you can use the ExtractToDirectory function as shown in the below code.

ZipFile.ExtractToDirectory(@"D:\", @"D:\data\unzip");

Feature 3: Regex timeout (TimeOut)

“Regex” has been the most preferred way of doing validations. In case you are new to Regex, please see the Regex video where I have explained how regex is implemented. But because of the typical parsing logic of regex it is exposed to DOS attacks. Let us try to understand in detail what I mean by that.

For instance consider this regular expression - “^(\d+)$”. This regex expression says that it can have only numbers. You can also see the regex symbolic diagram which shows how the regex will be evaluated .Now let’s say if we want to validate “123456X”. It will have six paths as shown in the below figure.

But if we add one more number to it, it will take seven paths. In other words as the length increases a regex takes more time to evaluate. In other words the time taken to evaluate is linearly proportional to the length of the characters.

Now let’s complicate the previously defined regex from “^(\d+)$” to “^(\d+)+$” . If you see the regex symbolic diagram it’s pretty complex. If we now try to validate “123456X”, it will run through 32 paths. If you add one more character the number pf paths become 64. 

In other words for the above regex, the time taken to evaluate rises exponentially with the number of characters.

Now the question you would ask is, how does it matter? This linear rise of evaluation time can be exploited by hackers to do a DOS (Denial of Service) attack. They can put a long, a really long string and make your application hang forever.

The proper solution for this would be to have a timeout on the regex operation. Good news, in .NET 4.5 you can now define a timeout property as shown in the below code. So if you get any kind of malicious string, the application will not go in a loop forever.

  var regEx = new Regex(@”^(\d+)+$”, RegexOptions.Singleline, TimeSpan.FromSeconds(2));
  var match = regEx.Match(“123453109839109283090492309480329489812093809x”);
catch (RegexMatchTimeoutException ex)
  Console.WriteLine(“Regex Timeout”);

Below is a nice video which is uploaded on Facebook which demonstrates the DOS attack by regular expression and how it can be prevented using regex timeout feature of .NET 4.5. Do not miss it, worth a watch.


Feature 4: Profile optimization (Improved startup performance)

We all know .NET code is in a half compiled format. During runtime, the JIT (Just-in-Time) compiler runs and translates this half compiled IL code to native machine code. One of the big complaints about JIT is that when a .NET applications runs the first time, it runs slow as JIT is busy translating IL code to machine code.

In order to bring down this startup time, in .NET 4.5, we have something called “profile optimization”. Profile is nothing but a simple file which has a list of methods which the application will need during startup. So when the application starts, a background JIT runs and starts translating IL code for those methods into machine / native code.

This background JIT compilation of startup methods happens across multiple processors thus minimizing the start up time further. Also note you need to have a multicore box to implement profile optimization. In case you do not have a multicore box then this setting is ignored.

In order to create the “profile” file, you first need to import the System.Runtime namespace. You can then call the SetProfileRoot and StartProfile methods of the static class ProfileOptimization. Now when the application starts the background JIT it will read from the profile file and compile your start up methods in the background thus reducing your startup time.

using System.Runtime;

// Call the Setprofilerroot and Startprofile method


One important note: Profileoptimization is enabled by default for ASP.NET 4.5 and Silverlight 5 applications. So the above code need not be written for these technologies.

Feature 5: Garbage collector (GC background cleanup)

Garbage collector is one real heavy task in a .NET application. And it becomes heavier when it is an ASP.NET application. ASP.NET applications run on the server and a lot of clients send requests to the server thus creating loads of objects, making the GC really work hard for cleaning up unwanted objects.

In .NET  4.0, when the GC runs for cleanup, all the application threads are suspended. You can see in the above figure we have three application threads running. We have two GCs running on separate threads. One GC thread for one logical processor. Now the application threads run and do their work. Now as these application threads are performing their task they also create managed objects.

At some point of time the background GC runs and starts clean up. When these background GCs start cleanup, they suspend all the application threads. This makes the server/application less responsive for that moment.

To overcome the above problem, server GC was introduced. In server GC there is one more thread created which runs in the background. This thread works in the background and keeps cleaning generation 2 (see this video for GC generation 0, 1, and 2)objects thus minimizing the load on the main GC thread. Due to double GC threads running, the main application threads are less suspended, thus increasing application throughput. To enable server GC, we need to use the gcServer XML tag and enable it to true.

      <gcServer enabled="true"/>

Three more features worth exploring

Set default culture to App Domain 

In the previous versions of .NET if I needed to set culture I needed to do it in every thread. Below is a sample code which demonstrates the pain of setting culture at thread levels. That was a real pain when we had heavily multi-threaded applications.

CultureInfo cul = new CultureInfo(strCulture);
Thread.CurrentThread.CurrentCulture = cul;
Thread.CurrentThread.CurrentUICulture = cul;

In 4.5 we can set culture at the app domain level and all the threads inside that appdomain will inherit that culture. Below is a sample code of how to implement DefaultThreadCurrentCulture.

CultureInfo culture = CultureInfo.CreateSpecificCulture("fr-FR");
CultureInfo.DefaultThreadCurrentCulture = culture;

Array support more than two gigabyte size

I am not sure in what kind of a scenario we would need a 2 GB collection. So I personally do not see where we would need this feature. If I ever need such a big collection I would break it into parts. But I am sure there should be a good reason for this feature to be enabled in the framework.

Unicode support for console

I left this feature out from the discussion as very less people work with console applications. I have seen people using consoles for academic purposes. All said and done we now have Unicode support for console apps also.


When you have free time, do visit my site for .NET 4.5 interview questions with answer videos, I have put in a lot of effort in developing this.   


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Shivprasad koirala
India India

You may also be interested in...

Comments and Discussions

QuestionQuestion about async and await Pin
amol_th16-Jun-15 1:38
memberamol_th16-Jun-15 1:38 
GeneralVery helpful Pin
Mr. Barik Ali29-May-15 7:35
memberMr. Barik Ali29-May-15 7:35 
QuestionVery Helpfull Pin
RhishikeshLathe16-Apr-15 4:54
professionalRhishikeshLathe16-Apr-15 4:54 
GeneralMy vote of 4 Pin
Member 896436917-Mar-15 1:53
memberMember 896436917-Mar-15 1:53 
QuestionGood one Pin
Member 1029672029-Jan-15 1:54
memberMember 1029672029-Jan-15 1:54 
Questionasync & await Pin
Member 467683323-Jan-15 8:14
memberMember 467683323-Jan-15 8:14 
GeneralMy vote of 5 Pin
Hassan Mokdad26-Nov-14 0:31
memberHassan Mokdad26-Nov-14 0:31 
Questiontoo good Pin
Member 36800204-Nov-14 11:05
memberMember 36800204-Nov-14 11:05 
SuggestionGood Article Sir Pin
aarif moh shaikh2-Oct-14 1:30
professionalaarif moh shaikh2-Oct-14 1:30 
QuestionConsole application usage Pin
S.T.Kannan8-Jul-14 1:26
memberS.T.Kannan8-Jul-14 1:26 
QuestionInteresting !!! Pin
anushree07.kini10-Jun-14 2:57
memberanushree07.kini10-Jun-14 2:57 
Questiongood article Pin
Readiprint3-Jun-14 3:04
memberReadiprint3-Jun-14 3:04 
QuestionGood summary Pin
ugo.marchesini12-May-14 4:06
professionalugo.marchesini12-May-14 4:06 
QuestionNice Article Pin
ankita vishal4-May-14 22:12
memberankita vishal4-May-14 22:12 
GeneralMessage Automatically Removed Pin
16-Apr-14 21:06
professionalPiyush K Patel16-Apr-14 21:06 
GeneralAwesome Pin
Sarvesh Kushwaha13-Apr-14 22:17
memberSarvesh Kushwaha13-Apr-14 22:17 
QuestionToo good Pin
Ankita M8-Apr-14 21:07
memberAnkita M8-Apr-14 21:07 
QuestionAsync and Await query Pin
masaab7-Apr-14 13:54
membermasaab7-Apr-14 13:54 
GeneralMy vote of 5 Pin
Piyush Patell6-Mar-14 7:45
memberPiyush Patell6-Mar-14 7:45 
QuestionThank You Pin
Member 382308620-Feb-14 17:24
memberMember 382308620-Feb-14 17:24 
GeneralMy vote of 5 Pin
Prasad Khandekar11-Feb-14 21:54
professionalPrasad Khandekar11-Feb-14 21:54 
QuestionAsync & await is avail for framework 4.0 Pin
Mpisano11-Feb-14 5:01
memberMpisano11-Feb-14 5:01 
GeneralMy vote of 5 Pin
akbarjankeer9-Feb-14 20:23
memberakbarjankeer9-Feb-14 20:23 
QuestionNot what I expected Pin
JV99996-Feb-14 22:33
professionalJV99996-Feb-14 22:33 
GeneralA Must Read for every .NET engineer! Pin
vijayjangid24-Nov-13 23:52
membervijayjangid24-Nov-13 23:52 
Questioncan't refrence System.IO.Compression.FileSystem! Pin
cnlucker12-Nov-13 22:38
membercnlucker12-Nov-13 22:38 
GeneralMy vote of 5 Pin
Snesh Prajapati10-Nov-13 21:54
memberSnesh Prajapati10-Nov-13 21:54 
GeneralTask.Waits/Task.ContinueWith vs Async/await Pin
Member 81280406-Nov-13 20:14
memberMember 81280406-Nov-13 20:14 
GeneralHigh Five Pin
Bilal Haider22-Oct-13 23:11
memberBilal Haider22-Oct-13 23:11 
QuestionNice article Pin
Member 1011024022-Oct-13 9:08
memberMember 1011024022-Oct-13 9:08 
GeneralMy vote of 5 Pin
jackmos22-Oct-13 8:16
memberjackmos22-Oct-13 8:16 
QuestionRe: Good Article !!! Pin
Steve Naidamast22-Oct-13 6:18
memberSteve Naidamast22-Oct-13 6:18 
AnswerRe: Good Article !!! Pin
Member 1011024022-Oct-13 9:06
memberMember 1011024022-Oct-13 9:06 
QuestionWow... Pin
Sachin Dev Tripathi21-Oct-13 21:52
professionalSachin Dev Tripathi21-Oct-13 21:52 
QuestionGreat article. Simple and useful. But one question.. Pin
Amogh Natu21-Oct-13 4:52
professionalAmogh Natu21-Oct-13 4:52 
AnswerRe: Great article. Simple and useful. But one question.. Pin
Member 1011024021-Oct-13 10:20
memberMember 1011024021-Oct-13 10:20 
GeneralRe: Great article. Simple and useful. But one question.. Pin
Amogh Natu22-Oct-13 7:12
professionalAmogh Natu22-Oct-13 7:12 
Question5ed! Pin
Canny Brisk21-Oct-13 4:45
professionalCanny Brisk21-Oct-13 4:45 
GeneralMy vote of 5 Pin
fredatcodeproject21-Oct-13 3:47
memberfredatcodeproject21-Oct-13 3:47 
GeneralVery useful great article Pin
Pasan Indeewara12-Oct-13 17:40
memberPasan Indeewara12-Oct-13 17:40 
QuestionGreat Article! Pin
Member 103273589-Oct-13 23:33
memberMember 103273589-Oct-13 23:33 
GeneralNice article Pin
suresh kumar goudampally1-Oct-13 19:51
membersuresh kumar goudampally1-Oct-13 19:51 
QuestionLarge Arrays? Pin
Gates VP30-Sep-13 10:13
memberGates VP30-Sep-13 10:13 
QuestionMy vote of 5 Pin
Peace ON27-Sep-13 7:15
memberPeace ON27-Sep-13 7:15 
GeneralMy vote of 5 Pin
soumendu_n26-Sep-13 20:36
membersoumendu_n26-Sep-13 20:36 
GeneralMy vote of 5 Pin
Jayanta Chatterjee26-Sep-13 4:48
memberJayanta Chatterjee26-Sep-13 4:48 
GeneralMy vote of 5 Pin
Member 1027707326-Sep-13 1:47
memberMember 1027707326-Sep-13 1:47 and c# Pin
Member 1029111725-Sep-13 20:36
memberMember 1029111725-Sep-13 20:36 
AnswerRe: .net and c# Pin
Shivprasad koirala26-Sep-13 6:22
mvpShivprasad koirala26-Sep-13 6:22 
GeneralMy vote of 5 Pin
Southmountain24-Sep-13 14:54
memberSouthmountain24-Sep-13 14:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 7 Feb 2014
Article Copyright 2013 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid