C#では型でもswitch文で分岐できるらしい
最近、型でもswitch文で分岐できるということを知った。
例えば以下のような感じ。
using UnityEngine; public interface TestInterface { } public class TypeA : TestInterface { } public class TypeB : TestInterface { } public class TypeC : TestInterface { } public class TestTypeSwitch : MonoBehaviour { private TestInterface testType = new TypeC(); void Start() { switch (testType) { case TypeA: print("This is Type A."); break; case TypeB: print("This is Type B."); break; case TypeC://testTypeはTypeC型なのでここを通る print("This is Type C."); break; } } }
あまり使うシチュエーションは無い気がするけど。