New Changes waiting
This commit is contained in:
31
Demos/DemoAction.cs
Normal file
31
Demos/DemoAction.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using System;
|
||||
|
||||
public class DemoAction
|
||||
{
|
||||
private Action justAnAction;
|
||||
|
||||
private Action<T> AsAction<T>()
|
||||
{
|
||||
var name = typeof(T).Name;
|
||||
return v => Console.WriteLine(name);
|
||||
}
|
||||
|
||||
private void ContrivedMethod()
|
||||
{
|
||||
var willTakeString = AsAction<string>();
|
||||
|
||||
willTakeString("Hello world");
|
||||
// outputs: String
|
||||
|
||||
//type safety - squiggly
|
||||
willTakeString("1");
|
||||
}
|
||||
|
||||
Action<string> takesString; // and does stuff with it
|
||||
Action<int, string> takesIntAndString; // and does stuff with it
|
||||
|
||||
Action<object> takesAnyObject;
|
||||
|
||||
Action<int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int> takesMaximum;
|
||||
// I've never used so many - so don't worry
|
||||
}
|
||||
52
Demos/DemoFunc.cs
Normal file
52
Demos/DemoFunc.cs
Normal file
@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public class DemoFunc
|
||||
{
|
||||
private Func<int> intFunc;
|
||||
|
||||
private Func<T> AsFunc<T>(T input)
|
||||
{
|
||||
return () => input;
|
||||
}
|
||||
|
||||
private T Exec<T>(Func<T> func)
|
||||
{
|
||||
return func();
|
||||
}
|
||||
|
||||
|
||||
public void ContrivedMethod()
|
||||
{
|
||||
Func<string> helloWorldFunc = AsFunc("Hello World!");
|
||||
var helloWorld = Exec(helloWorldFunc);
|
||||
// helloWorld == "Hello World!"
|
||||
|
||||
intFunc = AsFunc(1);
|
||||
var one = Exec(intFunc);
|
||||
// one == 1);
|
||||
}
|
||||
|
||||
|
||||
public void SomewhatMoreRealisticMethod()
|
||||
{
|
||||
// cant use var - because we need to tell the compiler what we'll describe
|
||||
Func<IEnumerable<BooksByAuthor>, string, bool> authorExists =
|
||||
(booksByAuthor, authorName) => booksByAuthor.Any(ba => ba.Author == authorName);
|
||||
|
||||
|
||||
Func<IEnumerable<BooksByAuthor>, string, BooksByAuthor> getGataloguedAuthor =
|
||||
(booksByAuthor, authorName) => {
|
||||
// can do complex stuff or help in debugging
|
||||
return booksByAuthor.FirstOrDefault(ba => ba.Author == authorName);
|
||||
};
|
||||
|
||||
|
||||
var bas = Enumerable.Empty<BooksByAuthor>();// for conciceness
|
||||
|
||||
var exits = authorExists(bas, "Homer");
|
||||
|
||||
var homersBooks = getGataloguedAuthor(bas, "Homer");
|
||||
}
|
||||
}
|
||||
12
Demos/Demos.csproj
Normal file
12
Demos/Demos.csproj
Normal file
@ -0,0 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Books.ConsoleApp\Books.ConsoleApp.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
12
Demos/Program.cs
Normal file
12
Demos/Program.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace Demos
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello World!");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user