Question about counting through an Array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Question about counting through an Array

int[] myArray = new int[7] {1,3,4,6,7,8,9}; Console.WriteLine("Enter a number"); int x = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Number of "+x+"in array is:"); Console.WriteLine(myArray.Count(n => n == x)); This code is find out how many times a number appears inside an array. The only part I dont understand is the last line, particularly "n => n == x". Why could I have not just put 'x'? Thanks

6th Sep 2017, 3:06 PM
eric
3 Answers
+ 3
Hi This is a major topic in c# I think you should study about Linq and lambda expressions. Read this https://www.codeproject.com/Articles/33769/Basics-of-LINQ-Lamda-Expressions
6th Sep 2017, 3:21 PM
Ali Vahidi
+ 3
Thank you, was just confused where the 'n' came from.
6th Sep 2017, 4:06 PM
eric
+ 1
n=>n==x equals to "foreach (n in array) where n==x" sorrounded with the count function, in simple words give me the count of times where foreach n in array is equal to x
6th Sep 2017, 3:44 PM
Loai Hazima
Loai Hazima - avatar