Using C# 7 Local Function

Printer-friendly version

A handy new feature introduced with C# 7 is Local Functions. As an example, in the LINQ query below, the select method is bit hard to read and can be simplified using a helper function.

void Main()

{

    AccountSet

    .Where(a => a.Name.StartsWith("Locator Test"))

    .Select(a => a.Name + " " + a.Address2_Line1 + " " + a.Address2_City + " " 

        + a.Address2_StateOrProvince + " " + a.Address2_PostalCode)

    .Dump();

}

A separate helper function can be created to replace the lambda in the select method of the Linq query in the Main function like this:

string FormatAddress(Account a)

{

    return a.Name + " " + a.Address2_Line1 + " " + a.Address2_City + ", " 

         + a.Address2_StateOrProvince + " " + a.Address2_PostalCode;

}

With the helper function in place, you can replace the select method in the Linq query as shown below:  

void Main()

{

    AccountSet

    .Where(a => a.Name.StartsWith("Locator Test"))

    .Select(FormatAddress)

    .Dump();

}

This works and produces the same results, but the downside is that we now have an additional function that's only used to support our main function. It would be nice to declare the helper function inline instead of separately, which we can do and is supported in C# 6 using lambdas like this:

Func<Account, string> FormatAddress = (a => a.Name + " " + a.Address2_Line1 + " " 

    + a.Address2_City + ", " + a.Address2_StateOrProvince + " " + a.Address2_PostalCode);

With C# 7 local functions, the syntax is a bit simpler, and instead of having to declare a Func, you simply create another function within the current one as follows, basically moving our original helper function into the main function:

string FormatAddress(Account a) { return a.Name + " " + a.Address2_Line1 + " " + a.Address2_City 

    + ", " + a.Address2_StateOrProvince + " " + a.Address2_PostalCode; }

The local function can also be written as a lambda:

string FormatAddress(Account a) => a.Name + " " + a.Address2_Line1 + " " + a.Address2_City 

    + ", " + a.Address2_StateOrProvince + " " + a.Address2_PostalCode;

The results are all the same, but the local function is a nice improvement. It keeps the new function close to where it's being used and has access to the variables in the main function as well.

Be sure to check out What’s New in C# 7.0 for more information as well as C# 7 in depth: Exploring local functions, a nice blog on the subject by Joydip Kanjilal that goes into more detail.

 

About the Author:

TopLine Strategies delivers the complete integration and development of sales, marketing and customer service technologies that enable corporate clientele to improve revenue streams and strengthen customer interactions. Our project management and consulting is designed to achieve timely delivery, 100 percent user adoption of the technologies we implement and deliver measurable returns on investments for our clients.

Comments (0)

Related Blogs

TheReact Native Open Source roadmap was announced in Q4 2018 after they decided to invest more in the React Native open source community.

October is not just about pumpkins, fall foliage, and cooler temps anymore. October 2018 also means the exciting introduction of Microsoft Dynamics 365 for Customer Engagement.

Back in 2016, Microsoft introduced its intentions to refresh its CRM and ERP strategy with Dynamics 365. At the heart of its services was the Common Data Model (CDM).