Skip to content
This repository has been archived by the owner on Aug 18, 2023. It is now read-only.

Latest commit

 

History

History
346 lines (235 loc) · 4.4 KB

slides.md

File metadata and controls

346 lines (235 loc) · 4.4 KB
marp author theme class
true
Rasmus Lystrøm
default
invert

Survey during the academic quarter

https://www.menti.com/ code: 5942 1139


Questions

  • How to setup tests?
  • Testing private methods?
  • How to use fluent assertions?

C♯ 03: Lambdas and LINQ

bg right:50%

Rasmus Lystrøm Associate Professor ITU


Topics

bg right:60%

Delegates Anonymous methods Lambda expressions Local functions Anonymous types Tuples Records Extension methods LINQ


bg

Delegates














Delegates – Building block for Higher-order functions

public delegate int BinaryOperation(int x, int y);

var add = new BinaryOperation(
    delegate (int x, int y)
    {
        return x + y;
    }
);

Delegates

Demo


Lambda Expressions

bg right:50%


Lambda Expressions

Action<string> write = => Console.WriteLine(s);

Func<int, int> square = => a * a;
Predicate<City> b = => c.Name.StartsWith("B");

Converter<double, double> ftoC = => c * 9.0 / 5.0 + 32.0;

...

Local Functions

static void Main(string[] args)
{
    int square(int a) { return a * a; };

    Console.WriteLine(square(16));
}

Local Functions

Demo


bg

Prerequisites














Anonymous types

var question = new
{
    Title = "The answer...?",
    Answer = 42
};

(Tuples)

var s = Tuple.Create("Clark Kent", "Superman");

var b = ("Bruce Wayne", "Batman");

var f = (name: "Barry Allen", alterEgo: "The Flash");

var random = new Random();

IEnumerable<(float x, float y)> GenerateCoordinates()
{
    yield return (random.NextSingle() * 100, random.NextSingle() * 100);
}

Records

public record Superhero(string Name, string AlterEgo, DateTime FirstAppearance);

Data: Collection Initializer

IEnumerable<City> cities = new[]
{
    new City(1, "Berlin"),
    new City(2, "Hamburg"),
    new City(3, "Frankfurt")
};

Data: Collection + Object Initializer

IEnumerable<City> cities = new[]
{
    new City { Id = 1, Name = "Berlin" },
    new City { Id = 2, Name = "Hamburg" },
    new City { Id = 3, Name = "Frankfurt" }
};

bg

Extension Methods














Built-in Extension methods

var count = cities.Count();

var sort = cities.OrderBy(=> c.Name);

var filter = cities.Where(=> c.Name.Contains("i"));

var pick = cities.FirstOrDefault(=> c.Id == 2);

var all = cities.All(=> c.Name.Length < 10);

var any = cities.Any(=> c.Name.StartsWith("B"));

var project = cities.Select(=> c.Name);

Create your own extension method

public static class Extensions
{
    public static int WordCount(this string str) =>
        str.Split(new[] { ' ', '.', '?' },
                  StringSplitOptions.RemoveEmptyEntries)
           .Length;
}

Extension methods

Demo


bg













LINQ - Language INtegrated Query


LINQ

var sorted = from c in cities
             where c.Name.Contains("i")
             orderby c.Name descending
             select new { Name = c.Name };

Extension Methods Version

var sorted = cities.Where(=> c.Name.Contains("i"))
                   .OrderByDescending(=> c.Name)
                   .Select(=> new { Name = c.Name });

LINQ

Demo


bg

Thank you