Skip to content

Instantly share code, notes, and snippets.

@abanoub-asaad
Last active February 4, 2022 10:21
Show Gist options
  • Select an option

  • Save abanoub-asaad/e337a55bce7dd5be34643f16afbd2633 to your computer and use it in GitHub Desktop.

Select an option

Save abanoub-asaad/e337a55bce7dd5be34643f16afbd2633 to your computer and use it in GitHub Desktop.
Lampda Expressions
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