「Unity/Csharp/switch」の版間の差分

提供: 初心者エンジニアの簡易メモ
ナビゲーションに移動 検索に移動
ページの作成:「==Switch文サンプル== <pre> using System; class Program { void Main() { string color = "red"; string message; switch (color) {...」
 
編集の要約なし
 
1行目: 1行目:
==Switch文サンプル==
==Switch文サンプル(C# 7.0まで)==
<pre>
<pre>
using System;
using System;
using UnityEngine;
class Program
class Program
{
{
23行目: 24行目:
                 break;
                 break;
         }  
         }  
        Debug.Log(message);
    }
}
</pre>
==Switch文サンプル(C# 8.0まで)==
<pre>
using System;
using UnityEngine;
class Program
{
    void Main()
    {
        string color = "red";
        string message = color switch
        {
            "red" => "赤は情熱の色です",
            "green" => "緑は自然の色です",
            "blue" => "青は空と海の色です",
            _ => "その色についての情報がありません" // デフォルトケース
        };
         Debug.Log(message);
         Debug.Log(message);
     }
     }
}
}
</pre>
</pre>

2025年5月8日 (木) 07:51時点における最新版

Switch文サンプル(C# 7.0まで)

using System;
using UnityEngine;
class Program
{
    void Main()
    {
        string color = "red";
        string message;
        switch (color)
        {
            case "red":
                message = "赤は情熱の色です";
                break;
            case "green":
                message = "緑は自然の色です";
                break;
            case "blue":
                message = "青は空と海の色です";
                break;
            default:
                message = "その色についての情報がありません";
                break;
        } 
        Debug.Log(message);
    }
}

Switch文サンプル(C# 8.0まで)

using System;
using UnityEngine;
class Program
{
    void Main()
    {
        string color = "red";
        string message = color switch
        {
            "red" => "赤は情熱の色です",
            "green" => "緑は自然の色です",
            "blue" => "青は空と海の色です",
            _ => "その色についての情報がありません" // デフォルトケース
        };
        Debug.Log(message);
    }
}