Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

enter image description here

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 is this doing? Is it implicitly running that mapping on every item in the brands collection?

like image 946
Only Bolivian Here Avatar asked Aug 24 '11 20:08

Only Bolivian Here


2 Answers

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.

like image 164
Thomas Levesque Avatar answered Sep 29 '22 05:09

Thomas Levesque


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.

like image 38
NotDan Avatar answered Sep 29 '22 04:09

NotDan