I have a question on something I've never seen before in C#. In the service provider in the new asp.net dependency injection, there is a method with return _ => null;
https://github.com/aspnet/DependencyInjection/blob/dev/src/Microsoft.Framework.DependencyInjection/ServiceProvider.cs Lines 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? Searching around for return _
doesn't return anything useful, unless you want naming conventions.
If you are not using the parameter in a lambda, people use _
as a convention for indicating that.
In your code, it is the catchall case for if serviceType
isn't resolved to a call site. Since you don't care about the serviceType
to return null, _
is used for that parameter.
This is probably a duplicate of this post which has lots of info:
C# style: Lambdas, _ => or x =>?
_ is a valid C# identifier, so _ => null
is the same as myServiceProvider => 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With