0

Can someone explain this code to me ?

I found this code somewhere in sololearn and I just wanted to know how it works. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { string input = Console.ReadLine(); string output = new String(input.Where(c => Char.IsWhiteSpace(c) || Char.IsLetterOrDigit(c)).ToArray()); Console.Write(output); } } }

23rd Apr 2020, 7:33 PM
Joseph Oritseweyinmi
Joseph Oritseweyinmi - avatar
1 Answer
+ 1
This is using LINQ and lambda expressions. Basically this is filtering the data. It’s like SQL for collections. c is just a variable name representing one item in the collection. In this case the colletion is an array of chars. Simliar to how you’d do this in a foreach statement. foreach(var c in input) {...} https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.where?view=netframework-4.8
23rd Apr 2020, 11:53 PM
Chris Persichetti
Chris Persichetti - avatar