473,508 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Confused about the lamda expression

I want to get my hands dirty with Linq, so I watched a few videos from
www.asp.net, and then turned to

http://msdn2.microsoft.com/en-us/library/bb397687.aspx

for some introduction about the new lamda feature of C#.

In the article linked above, there is an example reproduced here:

int[] numbers = {5,4,1,3,9,8,6,7,2,0 };
int oddNumbers = numbers.Count(n =n % 2 == 1);

I gave it a test, and oddNumbers has a value of 5.

I am confused by the 2nd line, i.e.,

int oddNumbers = numbers.Count(n =n % 2 == 1);

The Count method, as the MSDN doc says, returns the number of elements
in a sequence. So, does

n =n % 2 == 1

return a sequence? How does this happen?

Please shed some light. Thank you.

Mar 18 '08 #1
3 1319
For info, the relevant MSDN page:

http://msdn2.microsoft.com/en-us/library/bb535181.aspx
Mar 18 '08 #2

"gnewsgroup" <gn********@gmail.comwrote in message
news:5d**********************************@e60g2000 hsh.googlegroups.com...
>I want to get my hands dirty with Linq, so I watched a few videos from
www.asp.net, and then turned to

http://msdn2.microsoft.com/en-us/library/bb397687.aspx

for some introduction about the new lamda feature of C#.

In the article linked above, there is an example reproduced here:

int[] numbers = {5,4,1,3,9,8,6,7,2,0 };
int oddNumbers = numbers.Count(n =n % 2 == 1);

I gave it a test, and oddNumbers has a value of 5.

I am confused by the 2nd line, i.e.,

int oddNumbers = numbers.Count(n =n % 2 == 1);

The Count method, as the MSDN doc says, returns the number of elements
in a sequence. So, does

n =n % 2 == 1

return a sequence? How does this happen?

Please shed some light. Thank you.

Mar 18 '08 #3
int[] numbers = {5,4,1,3,9,8,6,7,2,0 };
int oddNumbers = numbers.Count(n =n % 2 == 1);

I gave it a test, and oddNumbers has a value of 5.

I am confused by the 2nd line, i.e.,

int oddNumbers = numbers.Count(n =n % 2 == 1);

The Count method, as the MSDN doc says, returns the number of elements
in a sequence. So, does

n =n % 2 == 1

return a sequence? How does this happen?
No, it returns true or false depending on whether the number is odd.

A lambda expression is a way of creating a function without giving it a
name. Suppose you defined:

bool blah(n) { return n%2 == 1; }

That's a function named blah.

(n =n%2 == 1)

is the same function without a name.

The Count method takes a function that tells it which elements to count.
I'm not sure if

numbers.Count(blah)

is valid syntax, but it ought to be; it means "count the ones that return
'true' when given as the argument of 'blah'".

numbers.Count(n =n%2 == 1)

is the same thing without defining it separately giving it a name.

Lambda expressions are the stuff of which Lisp is made. They are a basic
tool of advanced, abstract computer programming.


Mar 18 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1203
by: EAS | last post by:
In theory, the following code should ask for the user to enter a value for h until he/she enters hello or goodbye. h = "hi" while h != "hello" or "goodbye": h = raw_input("Value for h:") But...
11
1289
by: sathyashrayan | last post by:
Please look at the following code: static char *text = {"th", "st", "nd", "rd"}; char *ordinal_text(int number) { if (((number %= 100) > 9 && number < 20) || (number %= 10) > 3) number =...
9
2214
by: vijay | last post by:
Hello, I am new to C Programming and just started reading K&R. I was about to finish the pointers chapter but got very confused with: 1. int arr; >From what I have read, arr is a pointer to...
6
1527
by: rahul8143 | last post by:
hello, I am really confused over following for code snippets. No problem that they are working codes but how? how they are evaluated by compiler? It will be my pleasure if you explain me all...
3
2186
by: randomtalk | last post by:
hello everyone! Well, recently i've been trying to pick up c and see what is pointer all about (been programming in lisp/python for the better part of my last two years).. mmm.. I'm currently...
13
1821
by: Marvin | last post by:
Hi: I have been programming with OOP and C++ for over 10 years but I am new to javascript. I am very confused about one aspect and would appreciate it if someone could explain the differences...
16
1546
by: nephish | last post by:
Hey there, i have been learning python for the past few months, but i can seem to get what exactly a lamda is for. What would i use a lamda for that i could not or would not use a def for ? Is...
2
1492
by: bochengnever | last post by:
( ) and -are left to right in the same order . eg: struct foo { int a ; void * p; } main() { struct foo* A= malloc(sizeof(struct foo));
5
2312
by: Just_a_fan | last post by:
I tried to put an "on error" statement in a routine and got the message that I cannot user "on error" and a lamda or query expression in the same routine. Help does not list anything useful for...
0
7125
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7049
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7499
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4709
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3199
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
422
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.