Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. #if UNITY_EDITOR
  2. #if CSHARP_7_OR_LATER
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6.  
  7. public class CS7Test : MonoBehaviour
  8. {
  9. private void Start()
  10. {
  11. TestFunc001();
  12. TestFunc002();
  13. TestFunc003();
  14. }
  15.  
  16. static void TestFunc001()
  17. {
  18. #region タプル(Tupple)
  19. // タプルを使って2つの戻り値を返す
  20. (int count, int sum) Tally(IEnumerable<int> items)
  21. {
  22. var count = 0;
  23. var sum = 0;
  24. foreach (var x3 in items)
  25. {
  26. sum += x3;
  27. count++;
  28. }
  29. return (count, sum);
  30. }
  31. var data = new[] { 1, 2, 3, 4, 5 };
  32. var t = Tally(data);
  33. Debug.Log($"{t.sum}/{t.count}");
  34. // 15/5
  35. #endregion
  36.  
  37.  
  38. #region 型スイッチ
  39. object obj = 5;
  40. if (obj is string s)
  41. {
  42. Debug.Log("string #" + s.Length);
  43. }
  44. switch (obj)
  45. {
  46. case 7:
  47. Debug.Log("7の時だけここに来る");
  48. break;
  49. case int n when n > 0:
  50. Debug.Log("正の数の時にここに来る " + n);
  51. // 上から順に判定するので 7の時には来なくなる
  52. break;
  53. case int n:
  54. Debug.Log("整数の時にここに来る" + n);
  55. // 0 以下の時にしか来ない
  56. break;
  57. default:
  58. Debug.Log("その他");
  59. break;
  60. }
  61. // 正の数の時にここに来る 5
  62. #endregion
  63.  
  64. #region 数値リテラルの改善
  65. byte bitMask = 0b1100_0000;
  66. Debug.Log(bitMask);
  67. // 192
  68.  
  69. uint magicNumber = 0xDEAD_BEEF;
  70. Debug.Log(magicNumber);
  71. // 3735928559
  72. Debug.Log(magicNumber.ToString("X"));
  73. // DEADBEEF
  74. #endregion
  75.  
  76. #region 先頭区切り文字
  77. // C# 7.0 から書ける
  78. var b1 = 0b1111_0000;
  79. var x1 = 0x0001_F408;
  80. // C# 7.2 から書ける
  81. // b, x の直後に _ 入れてもOKに
  82. var b2 = 0b_1111_0000;
  83. var x2 = 0x_0001_F408;
  84. Debug.Log(b1);
  85. // 240
  86. Debug.Log(x1);
  87. // 128008
  88. Debug.Log(b2);
  89. // 240
  90. Debug.Log(x2);
  91. // 128008
  92. #endregion
  93.  
  94.  
  95. #region タプルの ==, != 比較 (c# 7.3)
  96. // c# 7.3
  97. (int a, (int x, int y) b) t2 = (2, (1, 2));
  98. Debug.Log(t2 == (1, (2, 3)));
  99. // False
  100. #endregion
  101.  
  102.  
  103. #region ref 再代入 (c# 7.3)
  104. // c# 7.3
  105. int x = 1;
  106. int y = 2;
  107. ref var r = ref x;
  108. r = 10;
  109. // x が 10になる
  110. r = ref y;
  111. r = 20;
  112. // y が 20 になる
  113. Debug.Log((x, y));
  114. // (10, 20)
  115. #endregion
  116.  
  117.  
  118. #region ref readonly
  119. int x5 = 0;
  120. ref readonly var readonlyX = ref x5;
  121. Debug.Log(readonlyX);
  122. // 0
  123. #endregion
  124.  
  125.  
  126. #region is 式
  127. int x7 = 3;
  128. if (x7 is int val)
  129. x7 += val;
  130. Debug.Log(x7);
  131. // 6
  132. #endregion
  133. }
  134.  
  135.  
  136.  
  137. #region 値の破棄
  138. static (int quotient, int remainder) DivRem(int dividend, int divisor)
  139. => (Math.DivRem(dividend, divisor, out var remainder), remainder);
  140. static void Deconstruct()
  141. {
  142. var (q, _) = DivRem(123, 11);
  143.  
  144. (_, var r) = DivRem(123, 11);
  145. }
  146. #endregion
  147.  
  148.  
  149.  
  150. #region 参照戻り値と参照ローカル変数
  151. static void TestFunc002()
  152. {
  153. var x = 10;
  154. var y = 20;
  155.  
  156. ref var m = ref Max(ref x, ref y);
  157. m = 0;
  158.  
  159. Debug.Log($"{x}, {y}");
  160. // 10, 0
  161. }
  162. static ref int Max(ref int x, ref int y)
  163. {
  164. if (x < y) return ref y;
  165. else return ref x;
  166. }
  167. #endregion
  168.  
  169.  
  170. #region ローカル関数
  171. static void TestFunc003()
  172. {
  173. // 関数の中で、ローカル関数 f を定義
  174. int f(int n) => n >= 1 ? n * f(n - 1) : 1;
  175.  
  176. Debug.Log(f(10));
  177. // 3628800
  178. }
  179. #endregion
  180.  
  181.  
  182. }
  183.  
  184.  
  185.  
  186. #region 参照渡しの拡張メソッド
  187. /// <summary>
  188. /// 参照渡しの拡張メソッド
  189. /// </summary>
  190. public static class QuarternionExtensions
  191. {
  192. /// <summary>
  193. ///
  194. /// </summary>
  195. /// <param name="q"></param>n
  196. public static void Conjugate(ref this Quaternion q)
  197. {
  198. var norm = q.w * q.w + q.x * q.x + q.y * q.y + q.z * q.z;
  199. q.w = q.w / norm;
  200. q.x = -q.x / norm;
  201. q.y = -q.y / norm;
  202. q.z = -q.z / norm;
  203. }
  204. /// <summary>
  205. /// コピーを避けたい場合に in 引数が使える
  206. /// </summary>
  207. /// <param name="p"></param>
  208. /// <param name="q"></param>
  209. /// <returns></returns>
  210. public static Quaternion Rotate(in this Quaternion p, in Quaternion q)
  211. {
  212. var qc = q;
  213. qc.Conjugate();
  214. return q * p * qc;
  215. }
  216. }
  217. #endregion
  218.  
  219. #endif
  220. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement