marp | author | theme | class |
---|---|---|---|
true |
Rasmus Lystrøm |
default |
invert |
https://www.menti.com/ code: 5942 1139
- How to setup tests?
- Testing private methods?
- How to use fluent assertions?
Rasmus Lystrøm Associate Professor ITU
Delegates Anonymous methods Lambda expressions Local functions Anonymous types Tuples Records Extension methods LINQ
public delegate int BinaryOperation(int x, int y);
var add = new BinaryOperation(
delegate (int x, int y)
{
return x + y;
}
);
Action<string> write = s => Console.WriteLine(s);
Func<int, int> square = a => a * a;
Predicate<City> b = c => c.Name.StartsWith("B");
Converter<double, double> ftoC = c => c * 9.0 / 5.0 + 32.0;
...
static void Main(string[] args)
{
int square(int a) { return a * a; };
Console.WriteLine(square(16));
}
var question = new
{
Title = "The answer...?",
Answer = 42
};
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);
}
public record Superhero(string Name, string AlterEgo, DateTime FirstAppearance);
IEnumerable<City> cities = new[]
{
new City(1, "Berlin"),
new City(2, "Hamburg"),
new City(3, "Frankfurt")
};
IEnumerable<City> cities = new[]
{
new City { Id = 1, Name = "Berlin" },
new City { Id = 2, Name = "Hamburg" },
new City { Id = 3, Name = "Frankfurt" }
};
var count = cities.Count();
var sort = cities.OrderBy(c => c.Name);
var filter = cities.Where(c => c.Name.Contains("i"));
var pick = cities.FirstOrDefault(c => c.Id == 2);
var all = cities.All(c => c.Name.Length < 10);
var any = cities.Any(c => c.Name.StartsWith("B"));
var project = cities.Select(c => c.Name);
public static class Extensions
{
public static int WordCount(this string str) =>
str.Split(new[] { ' ', '.', '?' },
StringSplitOptions.RemoveEmptyEntries)
.Length;
}
var sorted = from c in cities
where c.Name.Contains("i")
orderby c.Name descending
select new { Name = c.Name };
var sorted = cities.Where(c => c.Name.Contains("i"))
.OrderByDescending(c => c.Name)
.Select(c => new { Name = c.Name });