.Net Framework 4.5 came up with lots of great features. The new features of the core framework .Net 4.5 are difficult to read and run through because it does not fit depending on what are you working currently on. Many developers skip all the features and only focus on what they need in current development. Writing about all features will not be possible but some of them are:
async and await
"async" and "await" are keywords in C#. "Async" is used with a method to indicate that the method will have await keyword. In other words, we can call this function asynchronously. "Await" is used to mark the position of code from where the task should resume after a successful completion of the thread. In other words, the return type of an asynchronous function is Task. When it finishes its execution it will complete a Task.
Each and every asynchronous method can return three types of values i.e. Void (nothing to return), Task(one operation will perform) and Task<T> (return a task with a T type parameter) where Task is simply an implementation to IAsynchResult. In short, basic concepts are:
1. No Main method defined the asynchronous method.
2. No Main method cannot be call by the await keyword.
3. If any asynchronous method does not have await keyword then it will behave like a
synchronous method.
4. Properties should not be defined as asynchronous.
5. The await keyword can not be used inside lock section.
6. No asynchronous methods call from the try/catch block or finally block.
Zip compression:
Earlier .Net did not have any built-in support for implementing zip compression. Many developers were using third party code-packet/assembly/component say "DotNetZip". Now, the zip feature is available in the core framework .NET 4.5.
First, you need to import following framework assemblies (Right click on Reference in Solution Explorer then select Framework under Assemblies from left menu and select the following name)
CreateFromDirectory function is used to zip files from a folder as shown in the below code line:
Regex timeout
"Regex" is used to validate the data and it is commonly the preferred way of doing validations. As the length increases a regex takes more time to evaluate. In other words, the time taken to evaluate rises exponentially with the number of characters.
Due to a long time is taken while evaluating, it can be exploited by hackers. A hacker can make application hang forever by putting a really long malicious string. In the core framework .NET 4.5, there is timeout property in regex which can be used to not go application in a loop forever.
The signature is
public Regex(string pattern, RegexOptions options, TimeSpan matchTimeout);
Improved start-up performance using profile optimization
When a .NET applications run first time, it runs slow because JIT(Just In Time) compiler translates half compiled IL code to native machine code. To slow down the start-up time, the “profile optimization” has been introduced by .NET 4.5.
Profile is a file which has the list of methods that will need during application start-up. So when the application starts, JIT runs and start translating methods reading from "profile" file into machine/native code.
Note: Profile Optimization is enabled by default for ASP.NET 4.5 and Silverlight 5 applications.
Garbage collector (GC background cleanup)
A real heavy task in a .NET an application is garbage collector. when the garbage collector runs for cleanup, all the application threads are suspended. To thread suspended problem, server GC was introduced. This thread works in the background and keeps cleaning.
Due to double GC threads running, the main application threads are less suspended. To enable server GC, we need to use the gcServer XML tag and enable it to true.
0 Comment(s)