What’s New In C# 5.0

When we talk about what new features c# 5.0 has then, suddenly the two key features strike in mind. Microsoft Visual Studio 2012 with C# 5.0 provides following key features,

  1. Async Programming
  2. Caller Information

Async Programming: In c# 5.0 with .NET Framework 4.5, we can easily achieve asynchronous programming by using two new key-words,

  1. async
  2. await

async: It is a modifier which is used to create an asynchronous method.

await: It is an operator.

In traditional synchronization programming the user could not interact with UI until the process gets completed.

For example, if an application is trying to access a web resource then the whole application must wait until the request gets completed, which results into a slower interaction with UI.


Writing an async method is very simple,

async Task<int> doWorkAsync()


int someValue;


return someValue;


Task<int> returnVal= doWorkAsync();

int result=await returnVal;

or in one line,

int result = await doWorkAsync();



  1. async: async is a modifier which tells compiler that it is an async method.
  2. Task<datatype>: Task<datatype> specifies the return type of async method,
    1. Task<int>: Specifies that method will return an Integer.
    2. Task: Specifies that method will not return anything.
    3. void: It is used when you write a handler for async event.
    4. await: It specifies the suspending point for async method, which means that method will not respond further until the awaited process gets completed. It suspends the execution of async method and transfers the control back to the method’s caller then rest of program executes.


Caller Information: Caller information is very useful while performing tasks like tracing, debugging, logging etc.

For this purpose Caller Information uses three attributes,

  1. CallerFilePathAttribute: It gets the full path (at compile time) of file which has caller. It is of String type.
  2. CallerLineNumberAttribute: It gets the source file’s line number from where function is being called. It is of Integer type.
  3. CallerMemberNameAttribute: It gets the method/property name of caller. It is of String type.



public void A()


public void B()


public void doWorkAndLog(

[CallerMemberName] string memberName = “”,
[CallerFilePath] string sourceFilePath = “”,
[CallerLineNumber] int sourceLineNumber = 0)


Console.WriteLine(“Method Name: {0}”,memberName);

Console.WriteLine(“File Path: {0}”,sourceFilePath);

Console.WriteLine(“Line Number: {0}”,sourceLineNumber);



Note: Caller information attributes must be used as optional parameters, else Caller information will not work.

Now each time when doWorkAndLog method would be called, it would automatically log the information of caller.

About the Author



Related Posts