Caller Info In C# 5.0

Here we are with the second new feature in C# 5.0 “Caller Info“.

Lets start with C# evolution chart, then with an example shows How to use Caller Information.

C# Evolution Chart

Caller Information Example

Suppose we have DoSomething method that will do some important thing.

And we need to log some information about Who call it and When.
We can do the following…

    class Program
    {
        static void Main(string[] args)
        {
            InsertLog("Main");
            DoSomething();

            MethodA();

            MethodB();

            Console.ReadLine();
        }

        static void MethodA()
        {
            InsertLog("MethodA");
            DoSomething();
        }

        static void MethodB()
        {
            InsertLog("MethodB");
            DoSomething();
        }

        static void DoSomething()
        {
            // Some important thing will be here.
        }

        static void InsertLog(string methodName)
        {
            Console.WriteLine("{0} called DoSomething at {1}", methodName, DateTime.Now);
            Console.WriteLine("-----------------------------------------------------");
        }
    }

Each time before calling DoSomething we call InsertLog and pass the caller method name for logging.

Now note how we can do the same thing but using the Caller Info. It’s really simple 🙂

    using System.Runtime.CompilerServices;

    class Program
    {
        static void Main(string[] args)
        {
            DoSomething();

            MethodA();

            MethodB();

            Console.ReadLine();
        }

        static void MethodA()
        {
            DoSomething();
        }

        static void MethodB()
        {
            DoSomething();
        }

        static void DoSomething([CallerMemberName] string memberName = "",
                            [CallerFilePath] string sourceFilePath = "",
                            [CallerLineNumber] int sourceLineNumber = 0)
        {
            InsertLog(memberName, sourceFilePath, sourceLineNumber);
            // Some important thing will be here.
        }

        static void InsertLog(string methodName, string sourceFilePath, int sourceLineNumber)
        {
            Console.WriteLine("{0} called methodB at {1}", methodName, DateTime.Now);
            Console.WriteLine("Source file path: {0}", sourceFilePath);
            Console.WriteLine("Source line number: {0}", sourceLineNumber);
            Console.WriteLine("-----------------------------------------------------");
        }
    }

Instead of passing the caller method name we can use CallerMemberName.
Note that InsertLog now exists in only one place DoSomething and now we can know who call DoSomething, the source file path, and the source line number. All without need to do extra work. It’s really amazing and helpful 🙂

You can find here more information about Caller Info and how to use it.

Thank you,

Keep It Simple & Straightforward 🙂

~:H}{H:~

Async & Await in C# 5.0

Most software engineers are used to programming in a linear manner, at least that is how they are taught when they begin their careers.

Linear versus nonlinear code

The following figure assumes that we have an order system in place that will help us to fetch a collection of orders from somewhere.

The flow of an event-based system is such that it fires off a call somewhere and expects the result to be delivered through a raised event, can be visualized like the diagram in the following figure.

Introducing a new pattern

To simulate a small portion of a real order system, the OrderHandler and Order will look like the following:


class Order
{
    public string OrderNumber { get; set; }
    public decimal OrderTotal { get; set; }
    public string Reference { get; set; }
}

class OrderHandler
{
    private readonly IEnumerable _orders;
    public OrderHandler()
    {
        _orders = new[]
        {
            new Order {OrderNumber = “F1”,OrderTotal = 100,Reference = “Filip”},
            new Order {OrderNumber = “F1”,OrderTotal = 100,Reference = “Filip” }
        };
    }

    public IEnumerable GetAllOrders()
    {
        return _orders;
    }
}

As this is about asynchronous programming, we want to have something to ask for in an asynchronous manner. In order to simulate this, we can simply modify GetAllOrders() as follows:

public IEnumerable GetAllOrders()
{
    System.Threading.ManualResetEvent(false).WaitOne(2000);
    return _orders;
}

Now we will  create a new blank Windows 8 Store Application project, then add the following XAML to the MainPage.xaml:


   <Grid.RowDefinitions>

         <ProgressBar.RenderTransform>

   <button></button>

Before we can actually run the application, we need to add some things to the code file as well.

public MainPage()
{
    this.InitializeComponent();
    Information.Text = “No orders have been loaded yet.”;
}

private void LoadOrders_Click(object sender, RoutedEventArgs e)
{
    OrderLoadingProgress.Visibility = Visibility.Visible;
    var orderHandler = new OrderHandler();
    var orders = orderHandler.GetAllOrders();
    OrderLoadingProgress.Visibility = Visibility.Collapsed;
}

Now you can run the application you should see something like the following:

Now when you press the Load orders button, you will notice that you will not see any loading indicator and that the button is left in its pressed state for 2 seconds. This is because we are locking up the application.

In previous C# versions, we could have solved this by wrapping the code in a BackgroundWorker. That when finished would have raised an event in which we had to invoke a delegate in order for us to change the UI. This is a non-linear approach which tends to mess up the readability of the code. In an older application that is not WinRT, using a BackgroundWorker would have looked something like this:

public sealed partial class MainPage : Page
{
    private BackgroundWorker _worker = new BackgroundWorker();
    public MainPage()
    {
        InitializeComponent();
        _worker.RunWorkerCompleted += WorkerRunWorkerCompleted;
        _worker.DoWork += WorkerDoWork;
    }

    void WorkerDoWork(object sender, DoWorkEventArgs e)
    {
        var orderHandler = new OrderHandler();
        var orders = orderHandler.GetAllOrders();
    }

    private void LoadOrders_Click(object sender, RoutedEventArgs e)
    {
        OrderLoadingProgress.Visibility = Visibility.Visible;
        _worker.RunWorkerAsync();
    }

    void WorkerRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        Dispatcher.BeginInvoke(new Action(() =>
            {
                // Update the UI
                OrderLoadingProgress.Visibility = Visibility.Collapsed;
            }));
    }
}

The BackgroundWorker is what is known as event-based asynchronicity, the pattern is called event-based asynchronous pattern (EAP).

In WinRT however, there is no BackgroundWorker so we have to adapt to the new linear approach, which is only a good thing!
Our solution to this is to adapt to the new pattern introduced in .NET 4.5, async & await. When we use async & await, it is mandatory that we are using it together with the task parallel library (TPL).


private async void LoadOrders_Click(object sender, RoutedEventArgs e)
{
    OrderLoadingProgress.Visibility = Visibility.Visible;
    var orderHandler = new OrderHandler();
    var orderTask = Task<IEnumerable>.Factory.StartNew(() =>
        {
            return orderHandler.GetAllOrders();
        });
    var orders = await orderTask;
    Orders.Items.Clear();
    foreach (var order in orders)
        Orders.Items.Add(order);
    OrderLoadingProgress.Visibility = Visibility.Collapsed;
}

We can now run this application and load the orders without the UI locking up and then having a nice list of order numbers presented to us.

Thank you 🙂

Keep It Simple & Straightforward!

~:H}{H:~