Local functions or nested functions

C# 7.0, which comes with Visual Studio 2017, allows you to write local functions or nested functions. The local function provides you with the ability to declare methods/functions inside an already defined methods body. It has the same capability as normal methods, but it will be scoped to the method block where they are declared.

In earlier versions of C#, we needed to write the method bodies separately and then we needed to call one method from the other. Here is the old way to write a method called by another method:

 public static Person GetBloggerDetails() { return new Person { FirstName = "Kunal", LastName = "Chowdhury", Blogs = GetBlogs() }; } private static List<string> GetBlogs() { return new List<string> ...

Get Mastering Visual Studio 2017 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.