New features added to C# 5.0

New features added to C# 5.0

05 Oct 2024
Beginner
150K Views
8 min read
Learn with an interactive course and practical hands-on labs

Free C# Foundation Course: Learn C# In 21 Days

C# 5.0: An Overview

We know the new features of C# 3.0 and 4.0. We now have C# 6.0, but we’ll regress to tour C# 5.0’s new features since a lot of our programmer wants to know more about C# 5.0.C #'s most recent version 5.0 was released on August 15, 2012, with .NET Framework 4.5 and Visual Studio 2012.  In this C# Tutorial, we will explore more about features of C# 5.0. The top two features added in the C# 5.0 version are as follows: 

  • Async Programming 
  • Caller Information. 

C# 5.0 feature,C# version evolution

Let's understand both of these features in detail as given below.

Async Feature (Asynchronous Methods)

C# 5.0 The Async feature introduces two keywords async and await which allows you to write asynchronous code more easily and intuitively than synchronous code. Before C# 5.0, for writing an asynchronous code, you need to define callbacks (also known as continuations) to capture what happens after an asynchronous process finishes. This makes your code and other routine task such as exception handling complicated.

Both keywords are used in combination with each other. Hence, an await operator is applied to one or more than one expression of an async method. An async method returns a Task or Task<TResult> that represents the ongoing work of the method. The task contains information that the caller of the asynchronous method can use, such as the status of the task, its unique ID, and the method's result. Let's elaborate on this in the C# compiler.

Example:

public async Task<IEnumerable<Product>> GetProductList()
{
 HttpClient client = new HttpClient();
 Uri address = new Uri("http://scholarhat.com/");
 client.BaseAddress = address;

 HttpResponseMessage response = await client.GetAsync("myservice/product/ProductList");

 if (response.IsSuccessStatusCode)
 {
 var list = await response.Content.ReadAsAsync<IEnumerable<Product>>();
 return list;
 }
 else
 {
 return null;
 }
}
 

Read More - C# Interview Questions For Freshers

Caller Information (Caller info attributes)

Caller Information can help you in tracing, debugging, and creating diagnosis tools. It will help you to avoid duplicate codes which are generally invoked in many methods for the same purpose, such as logging and tracing. You could get the following information on the caller method.

1.CallerFilePathAttribute

The full path of the source file that contains the caller. This is the file path at compile time.

2.CallerLineNumberAttribute

Line number in the source file at which the method is called.

3.CallerMemberNameAttribute

Method or property name of the caller.

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Example
{
 static void Main(string[] args)
 {
 Console.WriteLine("Main method Start");
 InsertLog("Main");
 MyMethodB();
 MyMethodA();
 Console.WriteLine("Main method End!");
 Console.ReadLine(); // hold on result
 }

 static void MyMethodA()
 {
 InsertLog("MyMethodA");
 MyMethodB();
 }

 static void MyMethodB()
 {
 // some code here. 
 }

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

Output:

 
 Main method Start
 Main called MyMethodB at 11/17/2013 11:12:24 PM
 MyMethodA called MyMethodB at 11/17/2013 11:12:24 PM
 Main method End!
 

In both Main and MyMethodA, the method InsertLog is invoked for logging. Now we can change the above code as follows.

 
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

class Example
{
 static void Main(string[] args)
 {
 Console.WriteLine("Main method Start");
 MyMethodB();
 MyMethodA();
 Console.WriteLine("Main method End!");
 Console.ReadLine();
 }

 static void MyMethodA()
 {
 MyMethodB();
 }

 static void MyMethodB([CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
 {
 InsertLog(memberName);
 }

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

Output:

 
 Main method Start
 Main called MyMethodB at 11/17/2013 10:30:11 PM
 MyMethodA called MyMethodB at 11/17/2013 10:30:11 PM
 Main method End!

Read More - C# Roadmap For Beginners

Conclusion:

I hope you will enjoy these new features of C# 5.0 while programming. I would like to have feedback from my blog readers. Your valuable feedback, questions, or comments about this article are always welcome. Also, Consider the C# Programming course for a better understanding of C# concepts.

Take our Csharp skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET FREE CHALLENGE

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 9th time in a row (2016-2024). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this