Nov 18, 2015

Asynchronous Programming with Async and Await (MSDN) + Extra

https://msdn.microsoft.com/library/hh191443.aspx

https://code.msdn.microsoft.com/Async-Sample-Example-from-9b9f505c


http://www.abhisheksur.com/2010/10/c-50-asynchronous-made-easy.html




(Excerpted from http://www.abhisheksur.com/2010/10/c-50-asynchronous-made-easy.html)



What is await ? 

According to Whitepaper published on the CTP, await keyword lets you to wait your program and lets the program to be declared as a continuation of all the existing awaits so that the compiler automatically waits for all other awaits to be finished until the program continues its execution. 
In other words, say you have invoked 3 asynchronous methods from your code an asynchronous block from your code, you would just write the way you would have written this synchronously with a single keyword await before the call. The call lets the compiler wait until the execution successfully completes. Just as shown below : 
async Task GetStringsFromUrlAsync(string url)
{
     WebClient client = new WebClient();
     return await client.DownloadStringTaskAsync(new Uri(url));
}

This involves the call to DownloadStringTaskAsync which is already implemented for WebClient. Now as you can see I have specified the await just before the call to Asyncrhonous method DownloadStringTaskAsync, it will keep the call on wait. 

Even though if I say use :

async Task GetStringsFromUrlAsync(string url)
 {
      WebClient client = new WebClient();
      Task<string> modifieduri = client.DownloadStringTaskAsync(new Uri(url));
      string result = await modifieduri;
      return await client.DownloadStringTaskAsync(new Uri(result));
}

It means the second Task depends on the first one. So in this case the First DownloadStringTaskAsync will be called. This call actually returns Task(Task for any other operation) which you can keep on wait until it is finished using await keyword. So when you execute, the first call will block until the result is fetched and then it will take the string result and call the next one. 

Thus you can see how you can use await on Task variable to wait the object for completion without any implementation of custom callbacks, global objects etc.


What is async? 

Now, how to implement the asynchronous method? Hmm, async keyword can be placed just before the function call to make the method asynchronous. 

By specifying the async keyword against your method call, the compiler will automatically select your method to be asynchronous, which means it will move to the next line before returning the result in parallel until it finds an await. In the example I have provided theDownloadStringTaskAsync is actually an extension which specifies to be called with async.

Async method can return void, Task or Task based on what you need in your environment. But it is mostly preferable to return at least a Task object so that the caller can better handle the call. 

For any async method, you should put await before fetching the result, otherwise you might end up with blank/null result. await works with async to stop the further execution before finishing the other operations in context.

C# 6.0: Language features and its internals Part 1

Learn about the enhancements in both the CLR and IL in C# 6.0:
---------------------------------------------------------------------------------------
(excerpted from http://www.abhisheksur.com/2015/03/c-60-language-features-and-its.html)

The feature set which have been added / modified as part of C# 6.0 are:

1. Auto Property Initializer
2. Expression bodied Function
3. Static Class Uses
4. String Interpolation
5. Null Conditional operators
6. Exception filters
7. nameof operator
8. Dictionary initializers
9. await in try / finally.
10. Parameterless constructor for struct
------------------------------------------------------------------------------------


Feature 1 :  Auto Property Initializer

Auto properties are not new to C#. It was there since C# 3.0. The auto properties does not require a backing field to be defined which was done automatically during compilation. So as a coder, you can get a ready-made property without writing much of code. As auto properties does not have backing field exposed, you cannot initialize the backing field during object initialization, rather you have to initialize explicitly in constructors.




    
public class AutoPropertyInitialier
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public string FullName { get; set; }

        public AutoPropertyInitialier()
        {
            this.FirstName = "Abhishek";
            this.LastName = "Sur";

            this.FullName = this.FirstName + " " + this.LastName;
        }
    }
You can see in the above code block, the FirstName and LastName were Auto - Implemented properties, and both of them were initialized during constructor call, as the backing fields are not exposed for them. Think of a situation when your class is pretty big and you want to introduce a new property, you need to again initialize it inside constructor. This is the pain of the developer.

With C# 6.0, the initialization of auto-implemented properties is damn easy. Take a look into the code below :



    public class AutoPropertyInitializer
    {
        public string FirstName { get; set; } =  "Abhishek";
        public string LastName { get; } = "Sur";

        //public string FullName { get; } = this.FirstName + this.LastName;

        public string FullName { get; }

        public AutoPropertyInitializer()
        {
            this.FullName = this.FirstName + " " + this.LastName;
        }
    }

Here the properties FirstName and LastName were initialized directly after the declaration. You can notice the LastName were intentionally made readonly by not mentioning the setter. The backing field produced for a readonly property is also a readonly variable.

The FullName though is initialized inside constructor as the object initialization works before the constructor call, and hence initialization cannot evaluate "this".


In the above image, the C# representation of actual IL is shown which clearly indicates that the properties do have their backing fields. You can also notice the backing fields for property LastName is defined as readonly. The auto-property initialization is handled very cautiously during compilation and is abstract to you and relieves some of the pain points on writing un-necessary code. 

Feature 2 : Expression bodies functions 
Similar to Auto property initialization, C# 6.0 also comes up with expression bodies functions a long awaited feature. Expression bodied function is also a syntactic sugar implemented in the language which adds the flexibility on how you define member functions. 

Lambda expressions are not new to the language, but you might already know you cannot assign a lambda to a member function if the member function is not defined with a delegate. But if you read my internals to Lamba expression, you already know that for each lambda declaration the compiler creates a backing function body, but that function body cannot be exposed to class level. C# 6.0 enhances the language to allow you to define a member function with lambda expressions. 




    public class ExpressionBodiedFunction
    {
       
        public string WelcomeMsg(string name) => string.Format("Welcome {0}", name);

        public string HelloMsg => "Abhishek";

        public void Print() => Console.WriteLine(this.HelloMsg);

    }
In the above code block the WelcomeMsg is a member function that takes a string argument and returns a string. As per lambda rules, a lambda which has a return statement does not require a return keyword, hence the WelcomeMsg returns the expression with string.Format.

The second property is HelloMsg which is a property like method where the getter of the property HelloMsg will return the string "Abhishek". Remember, as property is also auto-implemented, the get keyword is also automatically evaluated by the compiler.

The last method "Print" is a void method and hence require a non-returning expression. The Console.WriteLine calls the property HelloMsg to print "Abhishek".

Now how does the compiler handles the lambda now without an explicit declaration of delegates ? Well, the answer is simple. The compiler re-writes the lambda into a full functional method body.


Here the WelcomeMsg, HelloMsg and Print after compilation is just a class level methods. The compiler entirely re-written the class into a strongly typed method bodies.



Feature 3 : Static class uses
The C# language teams identified the developers pain points very well, and implemented the Static class uses in the language. If you are using Static types it is always a nice to have feature in the language such that we can omit the repetitive static method call.

For instance, say I want to print a number of lines on screen, using Console.WriteLine. How about if we don't need to mention Console each line when we call WriteLine ? Static class uses does the same thing. Static class uses also recognize extension methods, hence if say a static class defines extension methods, if you add it, it will automatically recognize the extension methods as well.



    using static System.Console;
    using static System.Linq.Enumerable;

    public class UseStatic
    {
        public void CallMe()
        {

            WriteLine("Hi");

            var range = Range(10, 10);
            var check = range.Where(e => e > 13);
            // Static extension method can 
            // only be called with fulll representation
        }
    }


In the above code the WriteLine is automatically detected as we added the head using static statement. The Range is also declared inside System.Linq.Enumerable which is automatically determined and Where is an extension function. The static uses automatically recognized the extension methods as well.

If you look at the internals of the static uses, the compiler rewrites the static type calls on every occurrences of its method call.


In the actual IL, you can see, the WriteLine is padded with its appropriate type Console and Range is also associated with Enumerable. The re-writing is done by the compiler.


Feature 4 : String Interpolation 

String interpolation is another interesting feature introduced with recent version of C# 6.0. Interpolation is not new with strings. We can do string interpolation using string.Format before, but the problem is the code looks really ugly when using interpolation constructs.

For instance,


string myString = "FullName :" + p.First + " " + p.Last;
string myString = string.Format("FullName : {0} {1}", p.First, p.Last);
Here the first instance, is normal string concatenation. This creates an issue with multiple memory allocation if not string interned. The second statement uses string.Format to format a given string and replaces the {} arguments with the proper arguments as parameter to the method. Now while dealing with multiple occurrences of these kind of string placeholders might look very ugly and un-maintainable and error prone.

C# 6.0 comes with a new technique where the string interpolation can take place in between the strings, or rather than putting numeric placeholders, you can use the actual values.



string myString = $"FullName : {p.First, 5} {p.Last, 20}";
Console.WriteLine(myString);

string my2ndString = $"FullName from EBF = {this.EBF.FullName}";
Console.WriteLine(my2ndString);

Here Feature 4 :the string need to be started with a $ sign which indicates that the new kind of string interpolation to occur. The {p.First, 5} will be replaced with the value of p.First aligned to 5 characters.

Now what does the compiler do when these kind of interpolation is encountered. Well, you might be surprised to see, that the string is actually replaced by a call of String.Format during compilation.


So here you can see the actual statement after compilation is nothing but a string.Format call. So string interpolation is also a compiler trick. 

Feature 5 : Null conditional operators

Getting into NullReferenceException or "Object reference not set to instance of an object" is one of the most common exception that every developer must see while working with the langauge. It has been a common problem where developers need to give extra stress to check the null value before calling any member functions. Null for compiler is unknown, and hence if you call a method on null, it will point nowhere and hence throws NullReferenceException and exists the stack.

To handle nulls Microsoft had introduced few language constructs already like Null coalesce operator (??) or Conditional operators. But with the new feature introduced in C# 6.0, the language team has introduced another operator which checks for null.

Null Conditional operator is a new construct that gives away a shortcut to condition the null check before calling its members.



string zipcode = (address == null? null : address.zipcode);

string zipcode = (person == null? null : (person.address == null ? null : person.address.zipcode))

Here in the above two lines you can see how the complexity rises when there are multiple number of null checking on an expression. In the first statement the address is checked with null and when it has address, it will only then evaluate the zipcode. On the second statement, the person object is checked with null, then its address and then finally the zipcode is evaluated.

With null conditional operator the code would look like :



string zipcode = address?.zipcode;

string zipcode = person?.address?.zipcode;

Here the code is reduced greatly even though the actual expression logic remains the same. The ? mark is used for null conditioning. Null conditional operators does not exists as well, it is the same representation as that of the previous conditional operators. If you try to look into reflector, you will see it like :



You can see the null conditional operators are replaced by the compiler with normal conditional statements.


Feature 6 : Exception Filters

Now after so many syntactic sugar introduced with latest version of C#, this is the only new feature that directly relates to IL (Intermediate Language). From the very inception, the exception filters were there in CLR but was not exposed to the C# language. With C# 6.0, the exception filters finally came into existence and finally as a C# developer you can make use of it.

In C# the exception block contains try/catch/finally with multiple catch blocks supported. Based on type of the exception that has been encountered during runtime, the runtime chooses the appropriate catch block to execute. Exception filters allows you to filter out the catch blocks to ensure which catch block can handle the exception. With exception filters in place, there could be multiple catch blocks with same type where the type determines which filter it will call first.

Let us take a look at the code :



public class ExceptionFilters
    {
        public static bool LogException(Exception ex)
        {
            //Log
            return false;
        }
        public static bool LogAgainException(Exception ex)
        {
            //Log
            return true;
        }
        public void CallMe()
        {
            try
            {
                throw new ArgumentException("Exception");
            }
            catch (Exception ex) when (LogException(ex))
            {
                //Keep exception stack intact
            }
            catch(Exception ex) when (LogAgainException(ex))
            {
                // Calling the catch
            }
            
        }
    }

Here the ArgumentException is thrown when CallMe is invoked. The Exception filters are specified using when keyword just like in VB.NET. So the execution is like, it will call LogException method, executes the code and see whether the return statement is true or false. If it is true, it executes the catch block and exit, or otherwise it move and call LogAgainException method.

The exception filter expects a boolean value, hence the call to LogException should be a method returning bool or otherwise we need a comparison operator to always evaluate the when statement to boolean. Exception filters allows the developer to run arbitary code between the exception and the catch block execution without interfering the original stack trace of the exception object.

I have already told you that exception filters is a CLR concept. The IL is laid out as :


 .try --- to --- filter --- handler --- to ---

This tells the compiler to execute try block from line no --- to line ----. It will then call filter line and then call the handler which executes some bunch of lines. Cool ? Yes.

One small addition that I would want to add, if an exception filter encounters an exception and is left unhandled, the compiler handles it for you and automatically skip the exception block and moves to the next catch block. For instance if LogException in the code above throws an exception, it will call LogAgainException without modifying the Stack for ex.


Feature 7 : nameof Operator

nameof is a new addition to operator list supported by C#. This new operator is nothing but a compiler trick, where the compiler determines the name of a Property, Function or an Extension method and writes it directly in compiled output. For instance,



string name1 = nameof(this.MyProp); // evaluates MyProp
string name2 = nameof(DateTime.Now);//evaluates Now
string name3 = nameof(Console.WriteLine); //evaluates WriteLine
string name4 = nameof(new List<string>().FirstOrDefault); // Evaluates FirstOrDefault
string name5 = nameof(name4);//Evaluates name4

The nameof operator works on any type where the type has a name. In case you specify a variable, the nameof operator will evaluate to the name of the variable.


Feature 8 : Dictionary initializers

Dictionary is one of the common array object which used often to send data between objects. Unlike arrays, Dictionaries form an array of KeyValuePair. In modern days, Dictionary is used interchangeably with arrays because of its flexibility of defining key value set.



var list = new List<string>();
list.Add("Abhishek");
list.Add("Abhijit");

//Access
var first = list[0];
var second = list[1];

Here you can see a list is used to store string data, where index giving you the reference to individual objects. If I write the same with dictionary, it will look like :



var dict = new Dictionary<int,string>();
dict.Add(0, "Abhishek");
dict.Add(1, "Abhijit");

//Access
var first = dict[0];
var second = dict[1];

Even though the basics remain the same, the developers are increasingly inclined to use the later as it gives a flexibility to assign any index. You can define "Abhishek" to 10 and "Abhijit" to 20 without the limitation of fixed incremental index usage of dictionary.

As more and more people are going to Dictionary, language team thought about giving easier constructs to define Dictionary. Even though we can go without it, but it is a nice to have feature. In C# 6.0, you can define dictionary using the following syntax :

var dict = new Dictionary<int,string>
{
   [10] = "Abhishek",
   [20] = "Abhijit"
};

//Access
var first = dict[10];
var second = dict[20];

Or even if the key is declared as string, you can also use like:


var dict = new Dictionary<string,string>
{
   ["first"] = "Abhishek",
   ["second"] = "Abhijit"
};

//Access
var first = dict["first"];
var second = dict["second"];

This way it is very easier to declare and use dictionaries. The last two syntaxes were introduced in C# 6.0.

Now if you think of the actual implementation, there is nothing as such. If you try to sneak peek on the compiled code, you will be seeing the same code with object being initialized inside the constructor.



Here even though the new construct is used, the dictionary object is actually initialized in constructor itself. The code is re-written by the compiler.


Feature 9: Await in catch and finally

If you are not using async / await stuffs too often you might have already overlooked that you cannot specify await in catch or finally block before the latest version of C#. Microsoft has initially thought that it wont be possible to allow awaits in catch/finally because the compiler creates a state machine which slices each and every await statements into a sequence of calls which can pause and resume the function upon request. But as try/catch/finally are handled by CLR, allowing such thing will indicate every errors in the state machine is properly handled and also each of try/catch/finally would need their individual state machines each.

Based on the complexity, microsoft haven't provided the feature in previous version, but as everything is handled by the compiler itself, it is a very important feature to have as a programmers perspective hence in latest version of C# 6.0, the feature have been revised and await in catch/finally is allowed.



 public async Task<string> DownloadUrl(string url)
        {
            WebClient client = new WebClient();
            client.BaseAddress = "www.abhisheksur.com";
            try
            {
                return await client.DownloadStringTaskAsync(url);
            }
            catch
            {
                return await client.DownloadStringTaskAsync("mvp");
            }
            finally
            {
                //Await is also allowed.
            }
        }

Hence you can successfully compile the code above where the await works during the catch execution.

If you try looking at the code in reflector it will look very ugly with so many of StateMachine declarations and so many of goto statements to properly navigate to lines inside the state machine on failure. It would be nice if you can look into it personally, but if there is any problem understanding the implementation, feel free to ask me in discussions.

But as a benefit, you get this feature ready.


Feature 10: Parameterless constructor for struct

If you are unaware, there was no parameterless constructors in struct.  This is because of the fact that ValueTypes has a unique behavior where it will be automatically assigned a default memory when declared.

For instance,


int x;

Here int is a struct (System.Int32) and the value of x would be automatically 0. You can even declare array of a struct. For instance,



struct MyStruct
{
  public int Property1{get;set;}
  public string Property2 {get; set;}
}

MyStruct[] array = new MyStruct[10];

Now the Struct MyStruct will automatically get the default(MyStruct) in the array even though its not explicitly constructed. If you want to know more why struct does not support default constructor, feel free to read the article I posted .

In C# 6.0 the parameterless constructor is allowed. This is also a trick made by the compiler. It actually replaces the default constructor with the parameterless constructor passed in when the new operator is used to construct the object.

So if you say,


MyStruct s = new MyStruct();

The .NET will call the parameterless constructor for you.