Thursday, February 27, 2014

New Features of .NET Framework 4.5 and Microsoft Visual Studio 2013

New Features of .Net 4.5
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 sayasync 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.


Feature 1: async and await (code markers)

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 awaitkeyword.
  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 toMethod() after the task has finished operation.



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 namespaceSystem.IO.Compression.
The first step is you need to reference two namespaces:
  • System.IO.Compression.FileSystem
  • System.IO.Compression


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



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



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.


New Features of Visual Studio 2013

Peek Definition (Alt + F12)

We know developers move around code a lot when browsing definitions. While designing Visual Studio 2013 features, we looked at the elements and gestures that will help developers stay in context of their code while browsing through definitions. Peek Definition is one such feature that allows you to view definitions inline in the Editor without having to open a new document tab. To see it in action, right click on a symbol and click on the “Peek Definition” command in the context menu or invoke the keyboard shortcut Alt + F12.

Using Peek Definition saves you time when understanding a code base because it allows you to view definitions without losing sight of the code that you are already in. You can even browse through a series of definitions without leaving the original file. When you invoke the Peek Definition command inside an existing Peek view, we navigate you to the definition inside the Peek view and add a breadcrumb for the previous Peek view. These breadcrumbs help you visualize the path you take to reach any given definition.
When designing this feature, we chose to build on top of the “Preview Tab” concept that we introduced in VS 2012, because the Peek view is temporary and saves you from opening more documents. The breadcrumb style navigation is another piece we added to help visualize the navigation stack without losing context of your current code. In fact, if you hover over the breadcrumbs, we’ll show a tooltip with a short snippet of code so you can quickly see where you’re going.
In Visual Studio 2013 Preview, the Peek view is read only. In our testing with internal users, we heard a clear desire to be able to edit in the peek view, and we’ve already been working on that. You’ll be able to see how that works for you soon!

Enhanced Scrollbar


The Enhanced Scrollbar has been one of the most popular Productivity Power Tools extensions and is now part of the Visual Studio 2013 product! The Enhanced Scrollbar provides you visual cues about your file on the vertical scrollbar. Markers on the scrollbar allow you to quickly view the location of errors, warnings, breakpoints, bookmarks, find results and other useful information in your file. Here, too, we focus on bringing information to your fingertips – you don’t have to scroll away from your current position to get information about other parts of your file.

Navigate To (Ctrl+,)

Using the new Navigate To, you type in any part of a symbol and can find its definition, using smart semantic search. You can also type in part of a file name in your solution and quickly switch to it, regardless of whether or not it was previously open.
Navigate To in Visual Studio 2013 supports all previous capabilities in a fluid, non-modal and space efficient way. We positioned the new search window around the same upper-right area as the in-editor Find. This positioning allows us to display both the preview tab and maximize the number of Navigate To results we can display on screen without blocking your view of the preview code.
Selecting a result automatically displays it in a preview tab. This helps ensure the selected result is what you're searching for, so you can make a better decision before committing to the new view. To make sure context is easily preserved, pressing Escape takes you back to your initial location if the result wasn't what you were looking for.
When designing features, we intend to not only improve raw runtime performance, but also improve the workflow. The new Navigate To is optimized for search based keyboard driven usage, compared to Solution Explorer’s browsing focus. Along the development of the feature, we instrumented it and analyzed the telemetry from our internal users to measure the efficiency of the feature. Here are a couple of examples that guided our design.

Auto Brace Complete

The Auto brace complete feature automatically adds closing parenthesis, quotes, braces and brackets for you as you type code in the Editor. This was one of the popular Productivity Power Tools Features that we decided to include in the Visual Studio 2013.


No comments:

Post a Comment