5.0
C# 5.0, released with .NET Framework 4.5 in August 2012, focused primarily on asynchronous programming and improving the performance of applications that perform I/O-bound or CPU-bound operations. Here are the key features introduced in C# 5.0, along with examples and comparisons to earlier versions of C#:
1. Async/Await
The async
and await
keywords were introduced to simplify writing asynchronous code, making it more readable and maintainable by avoiding callback "pyramids of doom" and making asynchronous code appear more like its synchronous counterpart.
C# 5.0
public async Task<string> GetWebPageAsync(string url)
{
using (HttpClient client = new HttpClient())
{
string content = await client.GetStringAsync(url);
return content;
}
}
await
keyword is used to await the completion of the asynchronous operation without blocking the calling thread.
C# < 5.0
Before C# 5.0, asynchronous programming involved more complex patterns, such as the use of callbacks, the IAsyncResult
pattern, or the Event-based Asynchronous Pattern (EAP).
public void GetWebPage(string url, Action<string> callback)
{
WebClient client = new WebClient();
client.DownloadStringCompleted += (sender, e) =>
{
if (e.Error == null)
{
callback(e.Result);
}
};
client.DownloadStringAsync(new Uri(url));
}
2. Caller Information Attributes
C# 5.0 introduced Caller Information attributes (CallerFilePath
, CallerLineNumber
, CallerMemberName
) that allow you to obtain information about the caller of a method, which is particularly useful for diagnostic purposes.
C# 5.0
public void LogMessage(string message,
[CallerMemberName] string memberName = "",
[CallerFilePath] string filePath = "",
[CallerLineNumber] int lineNumber = 0)
{
Console.WriteLine($"{filePath}({lineNumber}) {memberName}: {message}");
}
C# < 5.0 In earlier versions, obtaining caller information required manual input, such as passing the file name, line number, and member name as parameters, making logging and diagnostics more tedious and error-prone.