Last active
February 4, 2022 10:21
-
-
Save abanoub-asaad/e337a55bce7dd5be34643f16afbd2633 to your computer and use it in GitHub Desktop.
Lampda Expressions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Lampda | |
| { | |
| /* | |
| You will see two classes have two code snippets to see how lampda is working here. | |
| Notes: | |
| Consider you have an array a = {1, 2, 3, 4} | |
| - So Reverse will change the order of the array and it will be like that {4, 3, 2, 1} | |
| - And Aggregate is working by this way: | |
| Aggregate((previousValue, next) => do whatever) | |
| - '<<' is bit operation which is left shift. | |
| */ | |
| } | |
| public class Lampda1 | |
| { | |
| public static void Main(string[] args) | |
| { | |
| // First Code Snippet | |
| int[] a = {1, 2, 3, 4}; | |
| int output = a.Reverse().Aggregate((t, n) => (t << 8) + n); | |
| Console.WriteLine(output); | |
| } | |
| } | |
| public class Lampda2 | |
| { | |
| public static void Main(string[] args) | |
| { | |
| // Second Code Snippet | |
| int[] a = {1, 2, 3, 4}; | |
| Array.Reverse(a); | |
| for(int i = 0; i < a.Length; i++) | |
| Console.Write(a[i] + " "); | |
| Console.WriteLine(); | |
| int total = a[0]; | |
| for(int i = 1; i < a.Length; i++) | |
| { | |
| total = (total << 8) + a[i]; | |
| } | |
| Console.WriteLine(total); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment