gpt4 book ai didi

c# - 本地功能的摘要注释和引用不起作用

转载 作者:行者123 更新时间:2023-12-04 10:27:49 30 4
gpt4 key购买 nike

如您所知,在 C# 7.0 中添加了一些 new features其中之一是本地功能。我查看了一些使用本地函数的示例和用例,发现使用它们的两个原因:

1) 隐藏函数或方法。原因是:如果该函数不是本地的,其他成员可能会不小心直接使用

2) 使用“Parent”函数的变量

在调试重构代码期间,我无法在 Visual Studio 中找到对本地函数的引用。有对私有(private)函数的引用:

private function

它在我调试或重构代码时很有帮助。在本地函数中我找不到它们:

local function

那么,第一个问题是为什么局部函数不显示摘要注释和引用文献?

有些程序员喜欢使用局部函数,但有些则不喜欢。这是一个示例(来自 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; }
}
}
}

在这种情况下,使用局部函数的原因是:

如果 IteratorFilter 旁边的私有(private)方法,其他成员可能会不小心直接使用它(没有参数检查).此外,它需要采用与 Filter 相同的所有参数,而不是让它们只在范围内

第二个问题,为什么要使用局部函数?在这种情况下,我们可以删除本地方法,因为它只使用了一次。如果我们害怕代码大小或代码责任,我们可以使用 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
}

最佳答案

根据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

我测试了所有 3 种插入评论的方法,但它们都不适用于本地功能。

  1. 如果您尝试插入“///”,IDE 不会生成摘要节点。
  2. 如果您尝试通过编辑菜单或右键单击上下文菜单使用内部评论,您将在主要功能上添加摘要,而不是本地功能。

开发环境:VS2015

但是文档中没有提到“不支持本地功能”的免责声明。

关于c# - 本地功能的摘要注释和引用不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60558194/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com