Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
2.1 Basic types: Lists and Linq beauty
//2.1 Basic types: Lists and Linq beauty using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { //Linq - language integrated query, functional programming elements in C# var list = new List<MyNumber>() //var saves us typing. Compiler is able to determine the type anyway. { new MyNumber() {Num = 5}, new MyNumber() {Num = 10}, new MyNumber() {Num = 15} }; list = list.OrderBy(f => f.Num).ToList(); //so in practice linq is these handy methods on lists (or generally IEnumerable) //that allow you transform your data without procedural for loops //This is very simillar to sql. //As a parameter they take functions, f => f.Num in this case. //This means a function that takes a parameter of type MyNumber (called f) and returns int (f.Num) list = list.Where(f => f.Num > 5).ToList(); //f - function that takes MyNumber and returns bool list.Where(f => f.Num > 5) .Select(f => new AnotherNumber //f - a function that takes MyNumber and returns AnotherNumber { DoubleNum = 2*f.Num }) //.Select(f => new //f - a function that takes MyNumber and returns anonymous type, this would have worked also //{ //Anonymous type - type without a name and definition - compiler will define it itself at compile time. // DoubleNum = 2*f.Num //Especially handy with multiple .Select clauses //}) //could insert as many .Select as needed to gradually transform my data to suit my needs .ToList() .ForEach(f => Console.Write(f.DoubleNum+" ")); //f - a function that takes AnotherNumber and returns nothing, but does some side effecting stuff (printing in this case) } } public class MyNumber { public int Num { get; set; } } public class AnotherNumber { public int DoubleNum { get; set; } } }
run
|
edit
|
history
|
help
0
Using Yield Return to mass copy a Class instance
P.I. Works
Check if string is palindrome
Homework4
hello
testing_editor
Program
Polygon Util
MostFrequent
Ki