开发者

What is this ReSharper snippet 'convert to method group' actually doing?

What is this ReSharper snippet 'convert to method group' actually doing?

Code before the changes:

List<ProductBrandModel> model = brands.Select(item => Mapper.Map<ProductBrand, ProductBrandModel>(item)).ToList();

Code after the improvement:

List<ProductBrandModel> model = brands.Select(Mapper.Map<ProductBrand, ProductBrandModel>).ToList();

What 开发者_如何学Pythonis this doing? Is it implicitly running that mapping on every item in the brands collection?


Since you're directly passing the parameter of the lambda expression to the Mapper.Map method, it is exactly equivalent to specifying this method directly as the projection for Select. The signature of Mapper.Map is compatible with the Func<TSource, TResult> delegate, so R# suggests to use the method group directly rather than a lambda expression.


The first line creates a method that immediately calls the Mapper.Map function. This is unnecessary since the Mapper.Map method matches the expected definition of Select and can call Mapper.Map directly. Resharper changes it so that only 1 method is called and the extra method is not generated by the compiler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜