ºÇ½ª¹¹¿·¡§ orika_ex_miyako 2023ǯ06·î05Æü(·î) 11:41:46ÍúÎò
¡¡Switchʸ¤Ï¡¢¥×¥í¥°¥é¥ß¥ó¥°¸À¸ì¤Ë¤ª¤¤¤Æ¿´ô¤Ë¤ï¤¿¤ë¾ò·ïʬ´ô¤ò¼Â¸½¤¹¤ë¤¿¤á¤ÎÀ©¸æʸ¤Î°ì¤Ä¤Ç¤¹¡£
Æäˡ¢¤¢¤ëÊÑ¿ô¤ÎÃͤˤè¤Ã¤Æ¡¢°Û¤Ê¤ë½èÍý¤ò¹Ô¤¤¤¿¤¤¾ì¹ç¤ËÊØÍø¤Ç¤¹¡£
¡¡if-elseʸ¤Ç¤âƱ¤¸¤è¤¦¤Ê¤³¤È¤Ï²Äǽ¤Ç¤¹¤¬¡¢¾ò·ï¤¬Â¿¤¯¤Ê¤ë¤È²ÄÆÉÀ¤¬²¼¤¬¤ë¤¿¤á¡¢¤½¤Î¤è¤¦¤Ê¾ì¹ç¤Ë¤ÏSwitchʸ¤ò»ÈÍѤ¹¤ë¤È¸ú²ÌŪ¤Ç¤¹¡£
¡¡¿§¡¹¤ÊµË¡¤òÃΤäƤª¤¡¢»È¤¤¤³¤Ê¤¹¥¹¥¥ë¤ò¿È¤Ë¤Ä¤±¤Æ¤¤¤¯¤³¤È¤Ç¡¢¥×¥í¥°¥é¥à¤Î¼ÂÁõÎϤ¬¸þ¾å¤·¤Þ¤¹¡£
¡¡Switchʸ¤Î´ðËÜŪ¤Ê½ñ¤Êý¤Ï°Ê²¼¤ÎÄ̤ê¤Ç¤¹¡£
switch (ÊÑ¿ô) { case ÃÍ1: // ÃÍ1¤Î»þ¤Î½èÍý break; case ÃÍ2: // ÃÍ2¤Î»þ¤Î½èÍý break; default: // ¾åµ°Ê³°¤ÎÃͤλþ¤Î½èÍý break; }
¡¡³Æ¥¡¼¥ï¡¼¥É¤ÎÀâÌÀ¤Ç¤¹¡£
¡¡switch¡§switchʸ¤ò»Ï¤á¤Þ¤¹¡£¤³¤ì¤Ë³¤±¤Æ³ç¸Ì()Æâ¤Ë¡¢Ê¬´ô¤Î¸µ¤È¤Ê¤ëÊÑ¿ô¤ò½ñ¤¤Þ¤¹¡£
¡¡case¡§ÊÑ¿ô¤¬ÆÃÄê¤ÎÃͤò»ý¤Ä¾ì¹ç¤Î½èÍý¤òµ½Ò¤·¤Þ¤¹¡£¤³¤ì¤Ë³¤±¤Æ¥³¥í¥ó:¤ò½ñ¤¡¢¤½¤Î¸å¤Ë³ºÅö¤¹¤ëÃͤ¬ÊÑ¿ô¤Ë¸ºß¤¹¤ë¾ì¹ç¤Î½èÍý¤òµ½Ò¤·¤Þ¤¹¡£
¡¡break¡§¤½¤ì¤¾¤ì¤Îcase¥Ö¥í¥Ã¥¯¤ÎºÇ¸å¤Ë¤Ïbreak¤ò½ñ¤¤Þ¤¹¡£¤³¤ì¤Ï¡¢¤½¤Î¥Ö¥í¥Ã¥¯¤Î½èÍý¤¬½ª¤ï¤Ã¤¿¤éswitchʸ¤«¤é½Ð¤ë¤¿¤á¤Î¥¡¼¥ï¡¼¥É¤Ç¤¹¡£¥á¥½¥Ã¥É¼«ÂΤò½ªÎ»¤·¤¿¤¤¾ì¹ç¤Ë¤Ï return ¤Ç¤âÈ´¤±½Ð¤»¤Þ¤¹¡£
¡¡default¡§ÊÑ¿ô¤¬¤É¤Îcase¤Ë¤âÅö¤Æ¤Ï¤Þ¤é¤Ê¤¤¾ì¹ç¤Î½èÍý¤òµ½Ò¤·¤Þ¤¹¡£¤³¤ì¤Ï¥ª¥×¥·¥ç¥ó¤Ç¡¢É¬¤º¤·¤âµ½Ò¤¹¤ëɬÍפϤ¢¤ê¤Þ¤»¤ó¡£
¡ã¥µ¥ó¥×¥ë¥³¡¼¥É¡ä
string menu = "¥Ï¥ó¥Ð¡¼¥¬¡¼"; switch (menu) { case "¥Ï¥ó¥Ð¡¼¥¬¡¼": // ¥Ï¥ó¥Ð¡¼¥¬¡¼¤òºî¤ë½èÍý break; case "¥Õ¥ì¥ó¥Á¥Õ¥é¥¤": // ¥Õ¥ì¥ó¥Á¥Õ¥é¥¤¤òºî¤ë½èÍý break; case "¥·¥§¥¤¥¯": // ¥·¥§¥¤¥¯¤òºî¤ë½èÍý break; default: // ¤½¤ì°Ê³°¤Î¥á¥Ë¥å¡¼¤Î¾ì¹ç¤Î½èÍý break; }
¡¡case ¤È break ¤Î´Ö¤Î²þ¹Ô¤ÏÉÔÍפǤ¹¡£
¡¡¤¿¤À¤·¡¢²ÄÆÉÀ¤ò¹â¤á¤ë¤¿¤á¤Ë¤â¡¢ºÇ½é¤ÎÆâ¤Ï¾åµ¤Î¤è¤¦¤Ë²þ¹Ô¤·¤Æ¤ª¤¤¤¿Êý¤¬Æɤߤ䤹¤¤¤Ç¤·¤ç¤¦¡£
¡¡
¡¡Enum¡ÊÎóµó·¿¡Ë¤È¤Ï¡¢Ê£¿ô¤ÎÄê¿ô¤ò°ì¤Ä¤Ë¤Þ¤È¤á¤Æ̾Á°¤ò¤Ä¤±¤Æ´ÉÍý¤¹¤ë¤¿¤á¤Î·¿¤Ç¤¹¡£
Switchʸ¤ÈEnum¤ÏÈó¾ï¤ËÁêÀ¤¬Îɤ¯¡¢Enum¤ÎÃͤ˱þ¤¸¤Æ°Û¤Ê¤ë½èÍý¤ò¹Ô¤¤¤¿¤¤¾ì¹ç¤Ë¤è¤¯»ÈÍѤµ¤ì¤Þ¤¹¡£
¡¡Î㤨¤Ð¡¢¥²¡¼¥à¤Î¥¥ã¥é¥¯¥¿¡¼¤Î¾õÂÖ¡ÊΩ¤Ã¤Æ¤¤¤ë¡¢Áö¤Ã¤Æ¤¤¤ë¡¢¥¸¥ã¥ó¥×¤·¤Æ¤¤¤ë¤Ê¤É¡Ë¤òEnum¤Ç´ÉÍý¤·¡¢
¤½¤Î¾õÂ֤˱þ¤¸¤Æ°Û¤Ê¤ë¥¢¥Ë¥á¡¼¥·¥ç¥ó¤òºÆÀ¸¤¹¤ë¡¢¤È¤¤¤Ã¤¿¤³¤È¤¬²Äǽ¤Ç¤¹¡£
public enum CharacterState { Standing, Running, Jumping } CharacterState state = CharacterState.Running; switch (state) { case CharacterState.Standing: // Ω¤Ã¤Æ¤¤¤ë»þ¤Î½èÍý break; case CharacterState.Running: // Áö¤Ã¤Æ¤¤¤ë»þ¤Î½èÍý break; case CharacterState.Jumping: // ¥¸¥ã¥ó¥×¤·¤Æ¤¤¤ë»þ¤Î½èÍý break; default: break; }
¡¡¤³¤Î¤è¤¦¤Ë¡¢Switchʸ¤ÈEnum¤òÁȤ߹ç¤ï¤»¤ë¤³¤È¤Ç¥³¡¼¥É¤¬Æɤߤ䤹¤¯¤Ê¤ê¡¢¾õÂ֤˱þ¤¸¤¿½èÍý¤ò´Êñ¤Ë´ÉÍý¤¹¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
¡¡C# 8.0¤«¤é¡¢Switchʸ¤ÏÆÃÄê¤Î·¿¤ä¾ò·ï¤Ë°ìÃפ¹¤ë¤«¤É¤¦¤«¤òȽÄê¤Ç¤¤ë¤è¤¦¤Ë¤Ê¤ê¤Þ¤·¤¿¡£
¥Ñ¥¿¡¼¥ó¥Þ¥Ã¥Á¥ó¥°¤È¸Æ¤Ð¤ì¤ëµ¡Ç½¤Î°ì´Ä¤Ç¤¢¤ê¡¢¤³¤ì¤Ë¤è¤ê¡¢Switchʸ¤Ï¤è¤ê½ÀÆð¤Êʬ´ô½èÍý¤òµ½Ò¤¹¤ë¤³¤È¤¬²Äǽ¤Ë¤Ê¤ê¤Þ¤·¤¿¡£
object obj = "Hello, World!"; switch (obj) { case string s: Console.WriteLine($"It's a string with length {s.Length}"); break; case int i: Console.WriteLine($"It's an integer with value {i}"); break; default: Console.WriteLine("It's something else."); break; }
¡¡¤³¤ÎÎã¤Ç¤Ï¡¢obj¤¬string·¿¤«int·¿¤«¡¢¤½¤ì°Ê³°¤Î·¿¤«¤òȽÄꤷ¤Æ¤¤¤Þ¤¹¡£
case string s:¤ÎÉôʬ¤Ç¤Ï¡¢obj¤¬string·¿¤Ç¤¢¤ì¤Ð¡¢¤½¤ÎÃͤòs¤È¤¤¤¦ÊÑ¿ô¤ËÂåÆþ¤·¡¢¤½¤Î¸å¤Î½èÍý¤Ç»ÈÍѤǤ¤ë¤è¤¦¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡£
¡¡¤Ç¤Ï¡¢¶ñÂÎŪ¤Ê¥á¥½¥Ã¥É¤ÎÃæ¤ÇSwitchʸ¤ÈEnum¤ò»È¤Ã¤¿Îã¤ò¸«¤Æ¤ß¤Þ¤·¤ç¤¦¡£
°Ê²¼¤Ï¡¢¥¥ã¥é¥¯¥¿¡¼¤Î¾õÂ֤˱þ¤¸¤Æ°Û¤Ê¤ë¥¢¥Ë¥á¡¼¥·¥ç¥ó¤òºÆÀ¸¤¹¤ë¥á¥½¥Ã¥É¤ÎÎã¤Ç¤¹¡£
public enum CharacterState { Standing, Running, Jumping } public void PlayAnimation(CharacterState state) { switch (state) { case CharacterState.Standing: // Ω¤Ã¤Æ¤¤¤ë»þ¤Î¥¢¥Ë¥á¡¼¥·¥ç¥ó¤òºÆÀ¸ Console.WriteLine("Standing animation"); break; case CharacterState.Running: // Áö¤Ã¤Æ¤¤¤ë»þ¤Î¥¢¥Ë¥á¡¼¥·¥ç¥ó¤òºÆÀ¸ Console.WriteLine("Running animation"); break; case CharacterState.Jumping: // ¥¸¥ã¥ó¥×¤·¤Æ¤¤¤ë»þ¤Î¥¢¥Ë¥á¡¼¥·¥ç¥ó¤òºÆÀ¸ Console.WriteLine("Jumping animation"); break; default: // ͽ´ü¤·¤Ê¤¤¾õÂÖ Console.WriteLine("Invalid state"); break; } }
¡¡¤³¤Î¥á¥½¥Ã¥ÉPlayAnimation¤Ç¤Ï¡¢°ú¿ô¤È¤·¤ÆCharacterState·¿¤Îstate¤ò¼õ¤±¼è¤ê¡¢¤½¤Îstate¤ÎÃͤ˱þ¤¸¤ÆSwitchʸ¤Çʬ´ô¤·¤Æ¤¤¤Þ¤¹¡£
¤½¤ì¤¾¤ì¤Îcase¤Ç¡¢Âбþ¤¹¤ë¾õÂ֤Υ¢¥Ë¥á¡¼¥·¥ç¥ó¤òºÆÀ¸¤¹¤ë½èÍý¡Ê¤³¤³¤Ç¤ÏConsole.WriteLine¤ÇÂåÍѡˤòµ½Ò¤·¤Þ¤¹¡£
¡¡C# 8.0°Ê¹ß¤Ç¤Ï¡¢¤è¤ê´Ê·é¤ËSwitchʸ¤ò½ñ¤¯¤³¤È¤¬²Äǽ¤È¤Ê¤ê¤Þ¤·¤¿¡£
Æäˡ¢Switch¼°¤È¸Æ¤Ð¤ì¤ë·Á¼°¤Ç¤Ï¡¢Ìá¤êÃͤò»ý¤ÄSwitchʸ¤ò°ì¹Ô¤Ç½ñ¤¯¤³¤È¤¬²Äǽ¤Ç¤¹¡£
¤³¤ì¤Ë¤è¤ê¡¢¥³¡¼¥É¤¬¤è¤ê¥·¥ó¥×¥ë¤Ë¤Ê¤ê¡¢Æɤߤ䤹¤µ¤¬¸þ¾å¤·¤Þ¤¹¡£
¡¡°Ê²¼¤Ë¡¢Switch¼°¤ò»È¤Ã¤¿Îã¤ò¼¨¤·¤Þ¤¹¡£
public enum Size { Small, Medium, Large } public int GetSizeValue(Size size) { return size switch { Size.Small => 1, Size.Medium => 2, Size.Large => 3, _ => 0 // ¤½¤ì°Ê³°¤ÎÃÍ }; }
¡¡¤³¤ÎGetSizeValue¥á¥½¥Ã¥É¤Ç¤Ï¡¢°ú¿ô¤È¤·¤ÆSize·¿¤Îsize¤ò¼õ¤±¼è¤ê¡¢¤½¤ì¤ËÂбþ¤¹¤ëÀ°¿ôÃͤòÌá¤êÃͤȤ·¤ÆÊÖ¤·¤Æ¤¤¤Þ¤¹¡£
Switch¼°¤Ç¤Ï¡¢returnʸ¤Î¸å¤ËÊÑ¿ô switch¤È½ñ¤¡¢¤½¤Î¸å¤Ë{ }Æâ¤Ç³Æcase¤òµ½Ò¤·¤Þ¤¹¡£³Æcase¤Ç¤Ï¡¢ÃÍ => ·ë²Ì¤Î·Á¼°¤ò¼è¤ê¤Þ¤¹¡£
¡¡¤Þ¤¿¡¢_¤Ï¤¹¤Ù¤Æ¤ÎÃͤòɽ¤¹¥ï¥¤¥ë¥É¥«¡¼¥É¤Ç¡¢¤³¤³¤Ç¤Ï¤½¤ì°Ê³°¤ÎÃͤËÂФ¹¤ë¥Ç¥Õ¥©¥ë¥È¤Î½èÍý¤òµ½Ò¤·¤Þ¤¹¡£
- ¥«¥Æ¥´¥ê¡§
- ¿Ê³Ø/¥¹¥¯¡¼¥ë
- ¥×¥í¥°¥é¥ß¥ó¥°
¥³¥á¥ó¥È¤ò¤«¤¯