Click here to Skip to main content
15,314,824 members
Articles / Web Development / ASP.NET
Article
Posted 29 May 2013

Stats

720.8K views
543 bookmarked

Five Great .NET Framework 4.5 Features

Rate me:
Please Sign up or sign in to vote.
4.91/5 (357 votes)
6 Feb 2014CPOL10 min read
In this article I have picked my five favorite features introduced in the .NET 4.5 core framework.

Table of contents

Introduction

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.

Image 1

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.

Image 2

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.

Image 3

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.

Image 4

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)

Image 5

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:

C#
using System.IO.Compression;

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

C#
ZipFile.CreateFromDirectory(@"D:\data",@"D:\data.zip");

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

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

Feature 3: Regex timeout (TimeOut)

Image 6

“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.

Image 7

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.

Image 8

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. 

Image 9

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

Image 10

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.

C#
try
{
  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.

Image 11 

Feature 4: Profile optimization (Improved startup performance)

Image 12

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.

Image 13

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.

C#
using System.Runtime;

// Call the Setprofilerroot and Startprofile method
ProfileOptimization.SetProfileRoot(@"D:\ProfileFile");

ProfileOptimization.StartProfile("ProfileFile");

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)

Image 14

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.

Image 15

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.

Image 16

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.

XML
<configuration>
   <runtime>
      <gcServer enabled="true"/>
   </runtime>
</configuration> 

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.

C#
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.

C#
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.

References

Image 17<movie height="360" width="630">

For further reading do watch the below interview preparation videos and step by step video series.

License

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

Share

About the Author


Comments and Discussions

 
GeneralMy vote of 4 Pin
Ehsan Azami1-Aug-13 17:40
professionalEhsan Azami1-Aug-13 17:40 
GeneralMy vote of 5 Pin
Walter Casella25-Jun-13 17:17
MemberWalter Casella25-Jun-13 17:17 
GeneralMy vote of 5 Pin
TGKN24-Jun-13 21:54
MemberTGKN24-Jun-13 21:54 
QuestionWhat is the file size limit on compression? Pin
PApoots24-Jun-13 6:58
MemberPApoots24-Jun-13 6:58 
AnswerRe: What is the file size limit on compression? Pin
Ashley van Gerven24-Jun-13 15:05
MemberAshley van Gerven24-Jun-13 15:05 
GeneralRe: What is the file size limit on compression? Pin
rtpHarry30-Sep-13 0:25
professionalrtpHarry30-Sep-13 0:25 
GeneralCongrats!!! Pin
Mohammed Hameed21-Jun-13 19:01
professionalMohammed Hameed21-Jun-13 19:01 
GeneralMy vote of 5 Pin
Rai Pawan18-Jun-13 23:49
MemberRai Pawan18-Jun-13 23:49 
Nice compact list of new features. Thanks for sharing.
QuestionConsole output? Pin
Illia Ratkevych18-Jun-13 6:16
MemberIllia Ratkevych18-Jun-13 6:16 
AnswerRe: Console output? Pin
csakii24-Jun-13 4:48
Membercsakii24-Jun-13 4:48 
GeneralMy vote of 5 Pin
snehal harawande17-Jun-13 20:02
Membersnehal harawande17-Jun-13 20:02 
GeneralMy vote of 5 Pin
Abhijit Jana15-Jun-13 19:41
professionalAbhijit Jana15-Jun-13 19:41 
GeneralMy vote of 5 Pin
Renju Vinod13-Jun-13 21:24
professionalRenju Vinod13-Jun-13 21:24 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA13-Jun-13 20:38
professionalȘtefan-Mihai MOGA13-Jun-13 20:38 
QuestionServer GC threads Pin
Marius Bancila12-Jun-13 0:35
professionalMarius Bancila12-Jun-13 0:35 
GeneralMy vote of 5 Pin
sandeep nagabhairava10-Jun-13 21:35
Membersandeep nagabhairava10-Jun-13 21:35 
GeneralMy vote of 5 Pin
Sridhar Patnayak9-Jun-13 19:33
professionalSridhar Patnayak9-Jun-13 19:33 
GeneralMy vote of 5 Pin
Aydin Homay8-Jun-13 1:14
MemberAydin Homay8-Jun-13 1:14 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey7-Jun-13 3:29
professionalManoj Kumar Choubey7-Jun-13 3:29 
GeneralMy vote of 5 Pin
Sandesh M Patil7-Jun-13 3:09
MemberSandesh M Patil7-Jun-13 3:09 
GeneralMy vote of 5 Pin
RaffyTheAlmighty6-Jun-13 22:37
MemberRaffyTheAlmighty6-Jun-13 22:37 
GeneralMy vote of 5 Pin
GeethaVani5-Jun-13 5:29
MemberGeethaVani5-Jun-13 5:29 
QuestionThanks Pin
Gonzalo F.5-Jun-13 4:54
MemberGonzalo F.5-Jun-13 4:54 
GeneralMy vote of 5 Pin
Jitendra Singh4-Jun-13 23:26
MemberJitendra Singh4-Jun-13 23:26 
GeneralMy vote of 5 Pin
Ashes In Snow4-Jun-13 20:26
professionalAshes In Snow4-Jun-13 20:26 

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.