C# 6.0 Features Series
- How to try C# 6.0 and Rosyln?
- Getter-only (Read Only) Auto Properties in C# 6.0
- Lambda and Getter Only Auto-Properties in C# 6.0
- Initializers for Read-Only Auto Properties in C# 6.0
- Initializers via Expression Auto Properties in C# 6.0
- C# 6.0 – A field initializer cannot reference the non-static field, method, or property
- Lambda Expression for Function Members in C# 6.0
- Dictionary Initializers (Index Initializers) in C# 6.0
- Expression Bodies on Methods returning void in C# 6.0
- using keyword for static class in C# 6.0
- Unused namespaces in Different Color in Visual Studio 2015
- Null-Conditional Operator in C# 6.0
- Null-Conditional Operator and Delegates
- nameof Operator in C# 6.0
- Contextual Keywords in C#
- String Interpolation in C# 6.0
- Exception Filters in C# 6.0
- Await in Catch and finally block in C# 6.0
In one of the previous blog post , we explored one of the feature of C# called Lambda Expression for Function Members in C# 6.0 where the example method used there had a return type of integer. In this blog post , let see the usage of the expression bodies on the methods returning void.
Expression Bodies on Methods returning void in C# 6.0
The Expression bodies can still be used on the methods returning void or Task (async methods) . In this case , it is necessary that expression immediately after the lambda expression syntax (arrow) to be a statement expression.
Below is a sample code snippet demonstrating the usage of the Expression Bodies on Methods returning void in C# 6.0
using System; using System.Collections.Generic; using System.Linq; namespace MobileOSGeekApp { class Program { public static void Display() => Console.WriteLine("Welcome to developerpublish.com .NET Tutorials Section"); static void Main(string[] args) { Display(); Console.ReadLine(); } } }