原委
由于一种很漂亮的 switch 写法,就是这种:
string result = word switch{ "多喝热水" => 分手(), "你多高" => (身高 + 20).ToString() + "cm", _ => word, };
于是后面遇到什么分支就想试试,但是似乎只在 .net Core 里有……
然后……就开始研究怎么写出和上面那种类似的格式。群里有人说可以用 lambda 就写出了下面这玩意儿……
float rotation = new Func<float>(() =>{ switch (direction){ case 1: return 0; case 2: return -(float)Math.PI / 3; …… } })();
然后又被提醒了……这个 case 如果只是 1~6 ,返回值还是常数的话……
那么为什么不直接用数组呢?字典都免了。
private float[] rotations = new float[6] { 0, -(float)Math.PI / 3, -(float)Math.PI * 2 / 3, -(float)Math.PI, (float)Math.PI * 2 / 3, (float)Math.PI / 3 };
行吧,今日份犯傻。