Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 奇数返回语法语句_C#_.net_C# 7.0 - Fatal编程技术网

C# 奇数返回语法语句

C# 奇数返回语法语句,c#,.net,c#-7.0,C#,.net,C# 7.0,我知道这听起来很奇怪,但我甚至不知道如何在互联网上搜索这个语法,而且我也不知道确切的意思 所以我看了一些MoreLINQ代码,然后我注意到了这个方法 public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityCompar

我知道这听起来很奇怪,但我甚至不知道如何在互联网上搜索这个语法,而且我也不知道确切的意思

所以我看了一些MoreLINQ代码,然后我注意到了这个方法

public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source,
        Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
{
    if (source == null) throw new ArgumentNullException(nameof(source));
    if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));

    return _(); IEnumerable<TSource> _()
    {
        var knownKeys = new HashSet<TKey>(comparer);
        foreach (var element in source)
        {
            if (knownKeys.Add(keySelector(element)))
                yield return element;
        }
    }
}
公共静态IEnumerable DistinctBy(此IEnumerable源,
Func键选择器,IEqualityComparer(比较器)
{
如果(source==null)抛出新的ArgumentNullException(nameof(source));
如果(keySelector==null)抛出新的ArgumentNullException(nameof(keySelector));
返回();IEnumerable())
{
var knownKeys=新哈希集(比较器);
foreach(源中的var元素)
{
if(knownKeys.Add(键选择器(元素)))
收益-收益要素;
}
}
}
这个奇怪的返回语句是什么<代码>返回函数()

这是支持本地功能的C#7.0

public static IEnumerable<TSource> DistinctBy<TSource, TKey>(
       this IEnumerable<TSource> source,
        Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
    {
        if (source == null) throw new 
           ArgumentNullException(nameof(source));
        if (keySelector == null) throw 
             new ArgumentNullException(nameof(keySelector));

        // This is basically executing _LocalFunction()
        return _LocalFunction(); 

        // This is a new inline method, 
        // return within this is only within scope of
        // this method
        IEnumerable<TSource> _LocalFunction()
        {
            var knownKeys = new HashSet<TKey>(comparer);
            foreach (var element in source)
            {
                if (knownKeys.Add(keySelector(element)))
                    yield return element;
            }
        }
    }
我可以优化这个与

  public void ValidateCustomer(Customer customer){

      void _validate(string value, string error){
           if(!string.IsNullOrWhitespace(value)){

              // i can easily reference customer here
              customer.ValidationErrors.Add(error);

              ErrorLogger.Log(error);
              throw new ValidationError(error);                   
           }
      }

      _validate(customer.FirstName, "Firstname cannot be empty");
      _validate(customer.LastName, "Lastname cannot be empty");
      ... on  and on... 
  }

考虑一个更简单的例子

void Main()
{
    Console.WriteLine(Foo()); // Prints 5
}

public static int Foo()
{
    return _();

    // declare the body of _()
    int _()
    {
        return 5;
    }
}

\(
是在包含return语句的方法中声明的本地函数

或者你的意思是:
return uz();IEnumerable()
?@Steve,我想知道OP是否更多地指的是
返回();IEnumerable()比
收益率收益率
?我想他指的是这行
收益率();IEnumerable()
。他可能会被它看起来的样子而不是实际的返回声明弄糊涂。@AkashKava OP说有一个奇怪的返回声明。不幸的是,代码包含两个返回语句。因此,如果人们对他/她指的是什么感到困惑,这是可以理解的。编辑了这个问题,并再次为困惑感到抱歉。@zoharpeld Well。。发布的代码确实显示了该函数的用途:)@其中一个好处是匿名函数可以很容易地从它的“主机”访问变量。你确定在C#语言中这实际上被称为匿名函数吗?它似乎有一个名称,即
\u匿名函数
或只是
\u
,而我希望真正的匿名函数类似于
(x,y)=>x+y
。我会称之为局部函数,但我不习惯C#术语。明确地说,正如没有人指出的那样,此代码段使用局部函数,因为它是一个迭代器(注意产量),因此执行起来很慢。如果没有本地函数,您需要接受输入验证发生在首次使用时,或者有一个只会被另一个方法调用的方法,原因很少。@ColinM kuksmen发布的示例实际上是最终实现该方法的主要原因之一-当您使用
生成一个函数时,在实际枚举可枚举项之前,不会执行任何代码。这是不可取的,因为您希望(例如)立即验证参数。在C#中实现这一点的唯一方法是将方法分为两种方法——一种是使用
收益率
s,另一种是不使用。内联方法允许您在内部使用方法声明
yield
,避免混乱和潜在的方法误用,该方法严格属于其父级内部且不可重用。是的,我知道关于本地函数,正是格式欺骗了我。。。希望这不会成为标准。你是说从同一行开始的函数声明吗?如果是这样,我同意,那太可怕了!是的,这就是我的意思。除了把它命名为下划线很可怕well@AkashKava:问题不在于它是否合法,而在于这样格式化时代码是否易于理解(因此易于维护和阅读)。个人喜好起了一定作用,但我倾向于同意斯图尔特的观点。
  public void ValidateCustomer(Customer customer){

      void _validate(string value, string error){
           if(!string.IsNullOrWhitespace(value)){

              // i can easily reference customer here
              customer.ValidationErrors.Add(error);

              ErrorLogger.Log(error);
              throw new ValidationError(error);                   
           }
      }

      _validate(customer.FirstName, "Firstname cannot be empty");
      _validate(customer.LastName, "Lastname cannot be empty");
      ... on  and on... 
  }
void Main()
{
    Console.WriteLine(Foo()); // Prints 5
}

public static int Foo()
{
    return _();

    // declare the body of _()
    int _()
    {
        return 5;
    }
}