4

As you know in C# 7.0 added some new features One of them is Local functions. I looked at some examples and use cases of using local functions and found two reasons to use them:

1) Hide function or method. The reason is: if the function had been not a local, it would have been available for other members to accidentally use directly

2) To use variables of "Parent" function

During debugging in refactoring code I couldn't find references to the local functions in Visual Studio. There are references to private function:

private function

It helps when I am debugging or refactoring code. In local functions I couldn't find them:

local function

So, the first question is why local functions don't show summary comments and references?

Some programmers liked using the local functions, but some of them don't. Here is an example (from What's New in C# 7.0 | .NET Blog):

 public IEnumerable<T> Filter<T>(IEnumerable<T> source, Func<T, bool> filter)
    {
        if (source == null) throw new ArgumentNullException(nameof(source));
        if (filter == null) throw new ArgumentNullException(nameof(filter));

        return Iterator();

        IEnumerable<T> Iterator()
        {
            foreach (var element in source)
            {
                if (filter(element)) { yield return element; }
            }
        }
    }

In this case, the reason for using a local function is :

If Iterator had been a private method next to Filter, it would have been available for other members to accidentally use directly (without argument checking). Also, it would have needed to take all the same arguments as Filter instead of having them just be in scope

The second question is why we should use local functions? In this case, we can just remove the local method, because it used only once. if we are afraid of code size or responsibility of code, we can use region:

    public IEnumerable<T> Filter<T>(IEnumerable<T> source, Func<T, bool> filter)
    {
        if (source == null) throw new ArgumentNullException(nameof(source));
        if (filter == null) throw new ArgumentNullException(nameof(filter));

        #region Iterating

        foreach (var element in source)
        {
            if (filter(element)) { yield return element; }
        }

        #endregion
    }
Dilshod K
  • 2,924
  • 1
  • 13
  • 46
  • For second question, if one function `A` uses a local function `B` multiple times and `B` is not used by other function, declaring `B` as a local function seems acceptable. – Louis Go Mar 06 '20 at 05:44
  • For first question. I tested to key in 3 slashes on top of the local function in vs2019, and ide doesn't generate summary text. I assume ide doesn't support summary on local function since you could just add one line comment one it. – Louis Go Mar 06 '20 at 05:52
  • @LouisGo What about references? – Dilshod K Mar 06 '20 at 06:22
  • I can't find a clear statement about "not supporting it", but none of the way for adding summary in msdn is working with local function. I post it as an answer for readability. – Louis Go Mar 06 '20 at 06:35

1 Answers1

2

According to msdn

To insert XML comments for a code element

  1. Place your text cursor above the element you want to document, for example, a method.

  2. Do one of the following:

    • Type /// in C#, or ''' in Visual Basic

    • From the Edit menu, choose IntelliSense > Insert Comment

    • From the right-click or context menu on or just above the code element, choose Snippet > Insert Comment

I tested all of 3 ways to insert comment, but none of it works on local funciton.

  1. If you try to insert "///", IDE doesn't generate summary node.
  2. If you try to use inter comment either by edit menu or right click context menu, you will add summary on the main function, not local function.

IDE: VS2015

However there is not disclaimer mentioned in document about "not supporting local function".

Louis Go
  • 2,213
  • 2
  • 16
  • 29