[C#]?:陳述句

  • 6255
  • 0
  • C#
  • 2012-10-11

[C#]?:陳述句

?:,它需要三個運算元,是 C# 中唯一的三元運算子。

條件式

x ?: y : z

如果 x 為 true 則評估 y;如果 x 為 false 則評估 z

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 60;
            //原式
            if (i >= 60)
            {
                Console.WriteLine("及格");
            }
            else 
            {
                Console.WriteLine("不及格");
            }
            Console.WriteLine(i>=60?"及格":"不及格");         
            Console.Read(); 
        } 
    }
}

如有錯誤 歡迎指正