0

I have read some about lambda. But I have problems with the code below, Who can explain to me?

public class FakeProductRepository : IProductRepository 
{
    public IEnumerable<Product> Products => new List<Product> 
    {
        new Product { Name = "Football", Price = 25 },
        new Product { Name = "Surf board", Price = 179 },
        new Product { Name = "Running shoes", Price = 95 }
    };
}

how public IEnumerable<Product> Products => new List<Product> work?? Why when I use "=" instead of "=>" error occurs?:

Error CS0535 'FakeProductRepository' does not implement interface member 'IProductRepository.Products'

Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
Parsicode
  • 1
  • 2
  • 1
    This is an expression-bodied function member, see https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6#expression-bodied-function-members – Stephan Bauer Mar 08 '17 at 13:58
  • The difference is that using `=>` means `Products` is a property. Using `=` means it's a field. Note however that is the equivalent of `public IEnumerable Products { get { return new List{...};}}` so it's going to create the list every time the property getter is called. – juharr Mar 08 '17 at 13:59
  • (maybe not the best dupe target, happy to unhammer if someone finds a better one!) – DavidG Mar 08 '17 at 13:59
  • @DavidG looks like a good dupe target to me. – juharr Mar 08 '17 at 14:01

0 Answers0