参考サイト
so-zou.jp
genesis-tech.jp
genesis-tech.jp
work-note32.com
sasaki816.hatenablog.com
Action
Action a1 = new Action(delegate () { Console.WriteLine("action a1"); });
a1();
Action a2 = delegate () { Console.WriteLine("action a2"); };
a2();
Action a3 = new Action(() => { Console.WriteLine("action a3"); });
a3();
Action a4 = () => { Console.WriteLine("action - ramda - a4"); };
a4();
Action<int, string> a5 = (int v1, string s1) => { Console.WriteLine("action - ramda - a5 - p:{0},{1}", v1, s1); };
a5(100, "hello");
var a6 = (int v1, int v2) => { Console.WriteLine("action - ramda - a6 - p:{0},{1}", v1, v2); };
a6(100, 200);
Func<T,TResult>
Func<string, bool> f1 = (string name) => {
return string.IsNullOrEmpty(name);
};
Console.WriteLine("Function - f1:" + f1("aoi"));
Func<string, string, string> f2 = (string type, string name) => {
return $"{type}-{name}";
};
Console.WriteLine("Function - f2:" + f2("attaker", "aoi"));
var f3 = (string type) => {
return string.IsNullOrEmpty(type);
};
Console.WriteLine("Function - f3:" + f3("aoi"));
Console.WriteLine("Function - f3:" + f3(""));
Task
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console1
{
public class MyTask
{
public MyTask()
{
}
public void Execute1()
{
Console.WriteLine("Execute1 - start");
Task.Run(() => {
Console.WriteLine("task - start");
Thread.Sleep(2000);
Console.WriteLine("task - end");
});
Console.WriteLine("Execute1 - end");
}
public void Execute2()
{
Console.WriteLine("Execute2 - start");
var t1 = Task.Run(() => { DoHeayWork("aoi"); });
var t2 = Task.Run(() => { DoHeayWork("kii"); });
Task.WaitAll(t1, t2);
Console.WriteLine("Execute2 - all finish.");
Console.WriteLine("Execute2 - end");
}
public void DoHeayWork(string name)
{
Console.WriteLine($"{name} DoHeayWork - start");
Thread.Sleep(1000);
Console.WriteLine($"{name} is woking.");
Thread.Sleep(1000);
Console.WriteLine($"{name} DoHeayWork - end");
}
public void Execute3()
{
Console.WriteLine($"Execute3 - start");
var t1 = Task.Run(() => {
Console.WriteLine("hevy work - start");
Thread.Sleep(2000);
Console.WriteLine("hevy work - end");
return true;
});
Console.WriteLine($"Execute3 - Result:{t1.Result}");
Console.WriteLine($"Execute3 - finish");
}
public void Execute4()
{
Console.WriteLine($"Execute4 - start");
var tasks = new List<Task<Tuple<int, bool>>>();
for (int i = 0; i < 3; i++) {
var index = i;
var task = Task <Tuple<int, bool>>.Run(() => TaskProc(index));
tasks.Add(task);
}
Task.WaitAll([.. tasks]);
foreach (var t1 in tasks)
{
Console.WriteLine($"task.Result: {t1.Result.Item1},{t1.Result.Item2 }");
}
Console.WriteLine($"Execute4 - finish");
}
public Tuple<int, bool> TaskProc(int index) {
Console.WriteLine($"hevy work({index}) - start");
Thread.Sleep(2000);
Console.WriteLine($"$hevy work({index}) - end");
return new Tuple<int, bool>(index, true) ;
}
}
}