首页  >  文章  >  后端开发  >  C# 三元运算符

C# 三元运算符

PHPz
PHPz原创
2024-09-03 15:09:04697浏览

用于代替条件语句 if 和 else 进行决策的运算符在 C# 中被称为三元运算符,它由三个参数组成,其中第一个参数用于比较,如果比较的结果为 true,则返回第二个参数,如果比较结果为 false,则返回第三个参数,这个运算符可以被认为是使用 if-else 语句的优化方式。

语法:

Boolean Expression ? First statement : Second statement

上面的语法代表三元运算符。它由三部分组成。语法的第一部分位于 ? 之前。它返回一个布尔值 true 或 false,因为它是一个条件表达式。语法的第二部分位于“?”之前和“:”之后,如果第一部分的条件表达式的结果为 true,则执行该部分。如果第一部分的条件表达式的结果为 false,则执行“:”语句之后的语法的第三部分。

C# 三元运算符的工作原理

以下是三元运算符的不同示例。

1.使用 If Else

比较两个值

考虑下面的 C# 程序,使用 if-else 语句比较两个值。

代码:

using System;
namespace Project
{
class MainClass
{
public static void Main(string[] args)
{
int a = 30;
int b = 40;
if (a < b)
{
Console.WriteLine("a's value is less than b");
}
else
{
Console.WriteLine("b's value is less than a");
}
}
}
}

输出:

C# 三元运算符

在上面的程序中,定义了两个变量a和b并分配了一些值。使用 if-else 条件语句将它们的值相互比较,找出较大的值。上面的程序由二十行代码组成,用于相互比较两个值。使用三元运算符可以将相同的代码优化为十四行,如下代码中使用的:

2.使用三元运算符比较两个值

考虑下面的 C# 程序,使用三元运算符比较两个值。

代码:

using System;
namespace Project
{
class MainClass
{
public static void Main(string[] args)
{
int a = 40;
int b = 30;
Console.WriteLine((a < b) ? "a's value is more than b" : "b's value is less than a");
}
}
}

输出:

C# 三元运算符

在上面的程序中,定义了两个变量a和b并分配了一些值。使用三元运算符将它们的值相互比较,找出较大的值。执行条件语句,并将语句的结果赋给变量 res。如果条件语句的结果为真,则打印‘?’之后的第二条语句,即a的值大于b;否则,打印‘:’之后的第三条语句 b 的值小于 a。

3.嵌套三元运算符

当“?”或“:”后面的第二个参数或第三个参数又是条件语句时,该运算符称为嵌套三元运算符。例如,考虑以下程序:

代码:

using System;
namespace Project
{
public class MainClass
{
public static void Main(string[] args)
{
int a = 10;
int b = 8;
Console.WriteLine(a> b ? "a's value is more than b" : a < b ? "a's value is less than b" : a == b ? "C" : "No result");
}
}
}

输出:

C# 三元运算符

在上面的程序中,定义了两个变量a和b并分配了一些值。使用 C# 中的三元运算符将它们的值相互比较,以找出哪个更大或者它们是否相等。执行条件语句,并将语句的结果赋给变量 res。如果条件语句的结果为 true,则执行“?”之后的第二条语句,这也是一个条件语句 a 注意:三元运算符不能执行语句。它仅返回第二部分或第三部分中存在的表达式或值,具体取决于第一部分中条件语句的结果。

C# 三元运算符示例

以下是 C# 中三元运算符的不同示例。

示例#1

使用嵌套三元运算符找出给定数字中最大的 C# 程序。

代码:

using System;
using System.IO;
using System.Text;
//Define Namespace
namespace program
{
//Define class
public class large
{
//Define main method
public static void Main(string[] args)
{
//Define three variables to take the input
int x;
int y;
int z;
//Get the input from the users
Console.Write("First number must be entered : ");
x = Convert.ToInt32(Console.ReadLine());
Console.Write("Second number must be entered: ");
y = Convert.ToInt32(Console.ReadLine());
Console.Write("Third number must be entered : ");
z = Convert.ToInt32(Console.ReadLine());
//largest number is found out by using nested ternary operator
int large = (x>y)?((x>z)?x:z):(y>z?y:z);
//display the largest number
Console.WriteLine("{0} is the largest number", large);
Console.ReadLine();
}
}
}

输出:

C# 三元运算符

示例#2

使用三元运算符判断给定数字是否为偶数的 C# 程序。

代码:

using System;
// Define Namespace
namespace program
{
// Define class
public class check
{
// Define main method
public static void Main(string[] args)
{
//Assign the number which need to be checked if it is even or not
int number = 8;
bool ifitisEven;
//Logic to check if ithe given number is even or not
ifitisEven = (number % 2 == 0) ? true : false ;
Console.WriteLine(ifitisEven);
}
}
}

输出:

C# 三元运算符

结论

在本教程中,我们通过定义了解 C# 中三元运算符的概念,然后了解 C# 中三元运算符的工作原理。然后我们了解使用嵌套三元运算符和简单三元运算符的不同 C# 程序以及它们的工作使用程序及其输出快照,其中包含程序的结果。

以上是C# 三元运算符的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn