FreeTrainの進化系を目指す
C#6.0新機能の備忘録的デモ
@@ -0,0 +1,103 @@ | ||
1 | +using System; | |
2 | +using System.Collections.Generic; | |
3 | +using System.Linq; | |
4 | +using System.Text; | |
5 | +using nft.framework; | |
6 | +using System.IO; | |
7 | +using System.Diagnostics; | |
8 | +using System.Reflection; | |
9 | +using System.Drawing; | |
10 | +using System.Drawing.Imaging; | |
11 | +using System.Threading.Tasks; | |
12 | +using System.Threading; | |
13 | +using System.Reactive.Linq; | |
14 | +using System.Reactive.Concurrency; | |
15 | +using System.Windows.Forms; | |
16 | +using static System.Diagnostics.Debug; // 'Debug.WliteLine()' の 'Debug.' を省略できるぞ | |
17 | + | |
18 | +namespace nft.test.test | |
19 | +{ | |
20 | + /// C# 6.0で知っておくべき12の新機能 | |
21 | + /// http://www.buildinsider.net/language/csharplang/0600 | |
22 | + /// | |
23 | + class DotNetFeatureTest | |
24 | + { | |
25 | + public int AutoPropertyNum { get; set; } = 999; //初期値を指定できる | |
26 | + public string AutoPropertyStr { get; } = "自動プロパティ"; //初期値を指定できる | |
27 | + | |
28 | + private Dictionary<int, string> textarray = new Dictionary<int, string>(); | |
29 | + public string this[int id] | |
30 | + { | |
31 | + get { return textarray[id]; } | |
32 | + set { textarray[id] = value; } | |
33 | + } | |
34 | + private string desc; | |
35 | + | |
36 | + [TestEntry("C#6.0各種新機能を試す#1")] | |
37 | + static private bool TestCS6Features1() | |
38 | + { | |
39 | + DotNetFeatureTest test = new DotNetFeatureTest | |
40 | + { // インデクサを持つオブジェクトの新しい初期化方法 | |
41 | + desc = "通常のプロパティ初期化", | |
42 | + [1] = "これが新しい初期化", | |
43 | + [2] = "インデクサを使って初期化できる" | |
44 | + }; | |
45 | + WriteLine(test.AutoPropertyNum, nameof(test.AutoPropertyNum)); // nameof 演算子 & using static の効果 | |
46 | + test.AutoPropertyNum *= 10; | |
47 | + WriteLine(test.AutoPropertyNum, nameof(test.AutoPropertyNum)); | |
48 | + | |
49 | + // String.Formatに代わる、新しい文字列内変数置換 | |
50 | + WriteLine($"{test.AutoPropertyStr} & [1]={test[2]}, num={test.AutoPropertyNum:#,0.#} Time={DateTime.Now:f}"); | |
51 | + | |
52 | + return true; | |
53 | + } | |
54 | + | |
55 | + // 重い処理のエミュレート兼Null条件演算子のテスト | |
56 | + static private void HeavyJob(Func<int, bool> callback, string name=null, int? tick=null, int? times=null ) | |
57 | + { | |
58 | + int _tick = tick ?? 100; // ?? 演算子は左辺がnullの時、代わりに右辺を返す | |
59 | + int totalTick = _tick * (times ?? 1); | |
60 | + string _name = name?.ToUpper() ?? "<匿名>"; // ?. の前がnullなら ?? の右辺を返す | |
61 | + WriteLine($"Start Heavy Job, It will took {totalTick} sec.", _name); | |
62 | + for (int i = 0; i < times; i++) | |
63 | + { | |
64 | + WriteLine($"Callback #{i}", _name); | |
65 | + if (!callback(i)) break; | |
66 | + Thread.Sleep(_tick); | |
67 | + } | |
68 | + WriteLine("Heavy Job End.", _name); | |
69 | + } | |
70 | + | |
71 | + | |
72 | + [TestEntry("C#6.0各種新機能を試す#2")] | |
73 | + static private async Task TestCS6Features2() | |
74 | + { | |
75 | + try | |
76 | + { | |
77 | + await Task.Run(() => HeavyJob( i => { | |
78 | + if (i == 2) throw new Exception("テスト用例外発生!"); | |
79 | + return true; | |
80 | + }, "Job#1", 100, 3 )); | |
81 | + } | |
82 | + catch (Exception ex) when (ex.Message == "テスト用例外発生!") // when 例外フィルター | |
83 | + { | |
84 | + // 例えばリトライとか | |
85 | + await Task.Run(() => HeavyJob(i => { | |
86 | + return true; | |
87 | + }, "job#2", 100 /*, times = null */)); | |
88 | + WriteLine("catch block end."); | |
89 | + } | |
90 | + catch (Exception ex) { | |
91 | + throw ex; // 予期せぬ例外 | |
92 | + } finally { | |
93 | + // 例えば解放処理などの後始末 | |
94 | + await Task.Run(() => HeavyJob(i => { | |
95 | + return true; | |
96 | + } /*, name = null, tick = null, times = null */)); | |
97 | + WriteLine("finally block end."); | |
98 | + } | |
99 | + } | |
100 | + | |
101 | + } | |
102 | + | |
103 | +} |
@@ -6,47 +6,7 @@ | ||
6 | 6 | using nft.framework; |
7 | 7 | |
8 | 8 | namespace nft.core.geometry { |
9 | - /// <summary> | |
10 | - /// 16 directions (Int16). | |
11 | - /// </summary> | |
12 | - public enum Direction16 : short { | |
13 | - NORTH, WEST, SOUTH, EAST, | |
14 | - NORTHEAST, NORTHWEST, SOUTHWEST, SOUTHEAST, | |
15 | - NORTHNORTHEAST, NORTHNORTHWEST, WESTNORTHWEST, WESTSOUTHWEST, | |
16 | - SOUTHSOUTHWEST, SOUTHSOUTHEAST, EASTSOUTHEAST, EASTNORTHEAST, | |
17 | - //INVALID = byte.MaxValue | |
18 | - } | |
19 | - | |
20 | - /// <summary> | |
21 | - /// 8 directions (Int16). | |
22 | - /// Cardinal and inter-cardinal directions. | |
23 | - /// </summary> | |
24 | - public enum Direction8 : short { | |
25 | - NORTH, WEST, SOUTH, EAST, | |
26 | - NORTHEAST, NORTHWEST, SOUTHWEST, SOUTHEAST, | |
27 | - //INVALID = short.MaxValue | |
28 | - } | |
29 | - | |
30 | - /// <summary> | |
31 | - /// 4 cardinal directions (Int16). | |
32 | - /// </summary> | |
33 | - [Serializable] | |
34 | - public enum Direction4 : short { | |
35 | - NORTH, WEST, SOUTH, EAST, | |
36 | - //INVALID = byte.MaxValue | |
37 | - } | |
38 | - | |
39 | - /// <summary> | |
40 | - /// 4 inter cardinal directions (Int16). | |
41 | - /// </summary> | |
42 | - [Serializable] | |
43 | - public enum InterCardinalDirection : short { | |
44 | - // 4の倍数で始まる連続した値であること(そういう前提の実装がある) | |
45 | - NORTHEAST = 4, NORTHWEST, SOUTHWEST, SOUTHEAST, | |
46 | - //INVALID = byte.MaxValue | |
47 | - } | |
48 | - | |
49 | - /// <summary> | |
9 | + /// <summary> | |
50 | 10 | /// direction expression and direction related utilities. |
51 | 11 | /// </summary> |
52 | 12 | [Serializable] |
@@ -0,0 +1,48 @@ | ||
1 | +using System; | |
2 | +using System.Diagnostics; | |
3 | +using System.Drawing; | |
4 | +using System.Runtime.Serialization; | |
5 | +using System.Collections.Generic; | |
6 | +using nft.framework; | |
7 | + | |
8 | +namespace nft.core.geometry { | |
9 | + /// <summary> | |
10 | + /// 16 directions (Int16). | |
11 | + /// </summary> | |
12 | + public enum Direction16 : short { | |
13 | + NORTH, WEST, SOUTH, EAST, | |
14 | + NORTHEAST, NORTHWEST, SOUTHWEST, SOUTHEAST, | |
15 | + NORTHNORTHEAST, NORTHNORTHWEST, WESTNORTHWEST, WESTSOUTHWEST, | |
16 | + SOUTHSOUTHWEST, SOUTHSOUTHEAST, EASTSOUTHEAST, EASTNORTHEAST, | |
17 | + //INVALID = byte.MaxValue | |
18 | + } | |
19 | + | |
20 | + /// <summary> | |
21 | + /// 8 directions (Int16). | |
22 | + /// Cardinal and inter-cardinal directions. | |
23 | + /// </summary> | |
24 | + public enum Direction8 : short { | |
25 | + NORTH, WEST, SOUTH, EAST, | |
26 | + NORTHEAST, NORTHWEST, SOUTHWEST, SOUTHEAST, | |
27 | + //INVALID = short.MaxValue | |
28 | + } | |
29 | + | |
30 | + /// <summary> | |
31 | + /// 4 cardinal directions (Int16). | |
32 | + /// </summary> | |
33 | + [Serializable] | |
34 | + public enum Direction4 : short { | |
35 | + NORTH, WEST, SOUTH, EAST, | |
36 | + //INVALID = byte.MaxValue | |
37 | + } | |
38 | + | |
39 | + /// <summary> | |
40 | + /// 4 inter cardinal directions (Int16). | |
41 | + /// </summary> | |
42 | + [Serializable] | |
43 | + public enum InterCardinalDirection : short { | |
44 | + // 4の倍数で始まる連続した値であること(そういう前提の実装がある) | |
45 | + NORTHEAST = 4, NORTHWEST, SOUTHWEST, SOUTHEAST, | |
46 | + //INVALID = byte.MaxValue | |
47 | + } | |
48 | +} |