Roslyn CTP

What is Roslyn?

Microsoft compilers have been always acting as black boxes. You provide the source text in and out the other end, you receive an assembly. All of that rich knowledge and information that the compiler produces is thrown away and unavailable for anyone else to use.

Visual Studio team has recently been working on a project called Roslyn with a goal to rewrite the C# and VB compilers and language services in managed code. This means that Microsoft is opening up the C# and Visual Basic compilers and exposing all that rich information and code analysis to be available for the software developers. Microsoft exposes a public API surface and provide extension points in the C# and VB language services. This opens up new opportunities for VS extenders to write powerful refactorings and language analysis tools, as well as allow anyone to incorporate Microsoft parsers, semantic engines, code generators and scripting in their own applications.
The Community Technological Preview (CTP) version can be downloaded from Roslyn CTP

Getting Started

After installing the Roslyn CTP, the best place to start is to open Start Menu -> Microsoft Codename Roslyn CTP -> Getting Started.
The CTP ships with quite a few samples for Visual Studio Extensions, compiler API, code issues, refactorings and so on. Most of the samples are provided for both C# and Visual Basic. You can open the sample source code from the Getting Started page.
In addition, several new project templates are available in the New Project dialog:

You can run scripts using the new rcsi.exe, which installs into %ProgramFiles(x86)%\Microsoft Codename Roslyn CTP\Binaries\rcsi.exe. You can add rcsi.exe to the path and then type rcsi .csx.
You can also copy chunks of code from a script file and send them to the C# Interactive Window (using the right-click context menu or a keyboard shortcut).

WinRT versus Win32 on Windows 8

The Windows Runtime API (WinRT) replaces the old Win32 libraries for accessing operating system functions. The WinRT API is object oriented, largely asynchronous, and callable easily by a wide variety of programming languages. Win32 continues to be supported by Windows 8, and it is what enables all existing Windows 7 applications to function in the Windows 8 desktop environment. The following figure shows how existing technologies map to the Windows 8 development platform.









Windows 8 Development Strategy

When considering the impact of Windows 8 on future software development, the following broad strategies should be evaluated:

1- Continue to use existing technologies, and run the application in the desktop environment.

2- Create a WinRT/Metro style smart client application that takes full advantage of the new WinRT and Windows 8 features.

3- Create a browser-based web application that relies on no plug-ins, so it can run in the browser in both the WinRT and desktop environments.

In summary, the new WinRT API and Metro style applications may represent the future of smart client development on the Windows operating system. Existing applications will continue to run in the Windows 8 desktop environment. Additionally, existing web applications that avoid the use of plug-ins will run in the WinRT web browser.
Existing developer skills in XAML, C#, VB, .NET, and Silverlight carry forward to WinRT development. The same is true for HTML5, CSS, and JavaScript developer skills.

Random Number Generator

Random Numbers

The generation of random numbers is important in many applications like simulations, cryptography, sampling and mostly in statistics. A sequence of numbers is random when it does not have a recognizable pattern in it or in other words if it is non-deterministic. Although non-deterministic random numbers are ideal, the computer generated, deterministic random numbers can be statistically “random enough”. These random numbers are named as pseudo-random numbers and can have easily identifiable patterns if the algorithm is not chosen wisely. Sometimes, you need to generate a cryptographically secure random number, for example, for creating a random password. For this scenario, you should use a class derived from the System.Security.Cryptography.RandomNumberGenerator. For example, you can use System.Security.Cryptography.RNGCryptoServiceProvider. CryptoRandom brings with it a performance cost. While Random’s implementation does not produce cryptographically strong random numbers, it is extremely fast when compared to RNGCryptoServiceProvider. Roughly, random is over 100 times faster than CryptoRandom.

Performance of PLINQ Queries

Parallel LINQ (PLINQ)
The main goal of the Parallel LINQ, or PLINQ is to execute LINQ to Objects queries in parallel, realizing the benefits of multithreading. Using PLINQ is simple, if you have to perform the same task on each element in a sequence, and those tasks are independent. If you need the result of one calculation step in order to find the next, PLINQ is not for you but many CPU intensive tasks can in fact be done in parallel. To tell the compiler to use PLINQ, you just need to call AsParallel and let PLINQ handle the threading.
The following samples demonstrate the performance of PLINQ queries for different scenarios:




































The result is shown below:

Without the AsParallel call, we would only use a single thread. Please note that except you specify that you want the results in the same order as the original sequence, PLINQ will assume you don’t mind getting results as soon as they’re available, even if results from earlier elements haven’t been returned yet. You can prevent this by using AsParallel().AsOrdered()

When to Use PLINQ

It’s tempting to search your existing applications for LINQ queries and experiment with parallelizing them. This is usually unproductive, because most problems for which LINQ is obviously the best solution tend to execute very quickly and so don’t benefit from parallelization. A better approach is to find a CPU-intensive bottleneck and then consider, “Can this be expressed as a LINQ query?”
PLINQ is well suited to embarrassingly parallel problems. It also works well for structured blocking tasks, such as calling several web services at once. PLINQ can be a poor choice for imaging, because collating millions of pixels into an output sequence creates a bottleneck. Instead, it’s better to write pixels directly to an array or unmanaged memory block and use the Parallel class or task parallelism to manage the multi-threading.

Reading Gmail Atom Feed in C#




What is the Atom?

Atom is a system which makes it easy for you to receive, in one place, regular updates from news websites, blogs, and/or Gmail. You can use Atom with an aggregator (also known as a newsreader, feed reader, or RSS/Atom reader) to receive new message alerts. Note: This feed is only available for Gmail accounts on Google Apps domains.
To use Gmail with Atom, you must already have an aggregator. Then enter in the URL field, and enter your Gmail address and password in the fields provided. This will allow the aggregator to look for your new messages.
If you’d like to have a label view in your aggregator, just add the label name to the end of the feed URL. So, to view your ‘work’ label as a feed, it’s
Please keep in mind that Gmail messages will appear in your aggregator only if there are unread messages in your inbox.

The following code shows how to authenticate the Gmail account and how to read/save the atom feed of that account:

Async CTP and Performance

Asynchify or Synchify?

Asynchronous methods are a powerful productivity tool, enabling you to write scalable and responsive libraries and applications. It should be considered that asynchronicity is not a performance optimization for an individual operation. Taking a synchronous operation and making it asynchronous will invariably degrade the performance of that one operation, as it still needs to accomplish everything that the synchronous operation did, but now with additional constraints and considerations.

How the Overall System Performs when Everything is Written Asynchronously?

The performance of your application will be increased by using asynchronicity in the aggregate because you can overlap I/O and achieve better system utilization by consuming valuable resources only when they are actually needed for execution.

Async CTP Optimization

The asynchronous method implementation provided by the .NET Framework is well-optimized, and often ends up providing as good or better performance than well-written asynchronous implementations using existing patterns and volumes more code. Any time you are planning to develop asynchronous code in the .NET Framework from now on, asynchronous methods should be your tool of choice. Still, it’s good for you as a developer to be aware of everything the Framework is doing on your behalf in these asynchronous methods, so you can ensure the end result is as good as it can possibly be.

What is new in C# 5?

Async CTP
When your user interface is unresponsive or your server doesn’t scale, chances are you need your code to be more asynchronous. With today’s .NET Framework and language features, though, that is easier said than done.
The Microsoft Visual Studio Async CTP proposes a new language feature in C# 5, and a new framework pattern to go with it, that will make asynchronous programming similar to – and about as straightforward as –synchronous programming.
The goal is to bring the asynchronous development experience as close to the synchronous paradigm as possible, without letting go of the ability to handle the asynchrony-specific situations. Asynchrony should be explicit and non-transparent, but in a very lightweight and non-disruptive manner. Composability, abstraction and control structures should all work as simply and intuitively as with synchronous code.

Copyright © All Rights Reserved - C# Learners