简体   繁体   English

C#中的return _是什么

[英]What is the return _ in C#

I have a question on something I've never seen before in C#. 我对C#中从未见过的事情有疑问。 In the service provider in the new asp.net dependency injection, there is a method with return _ => null; 在新的asp.net依赖注入的服务提供者中,有一个方法, return _ => null;

https://github.com/aspnet/DependencyInjection/blob/dev/src/Microsoft.Framework.DependencyInjection/ServiceProvider.cs Lines 63-72. https://github.com/aspnet/DependencyInjection/blob/dev/src/Microsoft.Framework.DependencyInjection/ServiceProvider.cs第63-72行。

The method in question: 问题的方法:

private Func<MyServiceProvider, object> CreateServiceAccessor(Type serviceType)
{
    var callSite = GetServiceCallSite(serviceType, new HashSet<Type>());
    if (callSite != null)
    {
        return RealizeService(_table, serviceType, callSite);
    }
    return _ => null;
}

What is the _ ? 什么是_ Is it new in C# 6? 它是C#6中的新功能吗? Searching around for return _ doesn't return anything useful, unless you want naming conventions. 除非您需要命名约定,否则搜索return _不会返回任何有用的内容。

If you are not using the parameter in a lambda, people use _ as a convention for indicating that. 如果你没有在lambda中使用参数,人们使用_作为约定来表示。

In your code, it is the catchall case for if serviceType isn't resolved to a call site. 在您的代码中,如果未将serviceType解析为调用站点,则会出现这种情况。 Since you don't care about the serviceType to return null, _ is used for that parameter. 由于您不关心serviceType返回null,因此_用于该参数。

This is probably a duplicate of this post which has lots of info: 这可能是这篇帖子的重复,有很多信息:

C# style: Lambdas, _ => or x =>? C#风格:Lambdas,_ =>或x =>?

_ is a valid C# identifier, so _ => null is the same as myServiceProvider => null _是有效的C#标识符,因此_ => nullmyServiceProvider => null相同

Defining what is a valid identifier is not as simple as checking the characters for a white list of allowed characters, but it's documented here 定义什么是有效的标识符并不像检查允许字符的白名单的字符那么简单,但是这里记录

For C#7 onwards, the language (or rather the compiler) recognizes the convention that a single _ denotes an unimportant value: https://docs.microsoft.com/en-us/dotnet/csharp/discards 对于C#7以上,语言(或编译器)认识到单个_表示不重要的值的约定: https//docs.microsoft.com/en-us/dotnet/csharp/discards

Starting with C# 7.0, C# supports discards, which are temporary, dummy variables that are intentionally unused in application code. 从C#7.0开始,C#支持丢弃,这是在应用程序代码中故意不使用的临时虚拟变量。 Discards are equivalent to unassigned variables; 丢弃等同于未分配的变量; they do not have a value. 他们没有价值。 Because there is only a single discard variable, and that variable may not even be allocated storage, discards can reduce memory allocations. 因为只有一个discard变量,并且该变量甚至可能不被分配存储,所以丢弃可以减少内存分配。 Because they make the intent of your code clear, they enhance its readability and maintainability. 因为它们使代码的意图变得清晰,所以它们增强了可读性和可维护性。

You indicate that a variable is a discard by assigning it the underscore (_) as its name. 通过为下划线(_)指定变量,可以指示变量是丢弃的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM