compiler bug?

This is a discussion on compiler bug? within the C# Programming forums, part of the General Programming Boards category; Why doesnt the following compile? the c in prices.Where() is not a declared variable in this scope right? Sounds like ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    compiler bug?

    Why doesnt the following compile? the c in prices.Where() is not a declared variable in this scope right? Sounds like a compiler bug to me or am I missing something?

    Code:
    int c = 0;
    
    List<Price> list = prices.Where(c => c.Product_id == product_id).ToList();
    
    c++;

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    It is in scope. Lambda expressions allow you to reference variables that are in scope where you define them.

    http://msdn.microsoft.com/en-us/library/bb397687.aspx
    Lambdas can refer to outer variables that are in scope in the enclosing method or type in which the lambda is defined. Variables that are captured in this manner are stored for use in the lambda expression even if variables would otherwise go out of scope and be garbage collected. An outer variable must be definitely assigned before it can be consumed in a lambda expression.
    Last edited by itsme86; 04-20-2012 at 09:06 AM.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-13-2010, 09:02 PM
  2. Replies: 4
    Last Post: 09-12-2009, 01:10 PM
  3. Replies: 2
    Last Post: 02-04-2008, 01:34 AM
  4. Compiler
    By anirban in forum C Programming
    Replies: 10
    Last Post: 03-24-2007, 01:32 AM
  5. hp iPAQ 6300 && C compiler || C# compiler
    By xddxogm3 in forum Tech Board
    Replies: 2
    Last Post: 12-07-2004, 06:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21