C# - 关系运算符
C# 中的关系运算符是什么?
C# 关系运算符比较两个操作数并返回布尔值(true 或 false)。这些运算符可用于决策、循环和数据验证。
C# 关系运算符列表
下表列出了 C# 支持的所有关系运算符。假设变量 A 为 10,变量 B 为 20,则 −
| 运算符 | 符号 | 说明 | 示例 |
|---|---|---|---|
| 等于 | == | 检查两个操作数的值是否相等,如果相等,则条件成立。 | (A == B) 不成立。 |
| 不等于 | != | 检查两个操作数的值是否相等,如果相等,则条件成立。值不相等,则条件成立。 | (A != B) 为真。 |
| 大于 | > | 检查左操作数的值是否大于右操作数的值,如果是,则条件成立。 | (A > B) 不为真。 |
| 小于 | < | 检查左操作数的值是否小于右操作数的值,如果是,则条件成立。 | (A < B) 为真。 |
| 大于或等于 | >= | 检查左操作数的值是否大于或等于右操作数的值,如果是,则条件成立。 | (A >= B) 不为真。 |
| 小于或等于 | <= | 检查左操作数的值是否小于或等于右操作数的值,如果是,则条件成立。 | (A <= B) 为真。 |
关系运算符示例
以下示例演示了 C# 中所有可用的关系运算符 -
using System;
class Program {
static void Main(string[] args) {
int a = 21;
int b = 10;
if (a == b) {
Console.WriteLine("Line 1 - a is equal to b");
} else {
Console.WriteLine("Line 1 - a is not equal to b");
}
if (a < b) {
Console.WriteLine("Line 2 - a is less than b");
} else {
Console.WriteLine("Line 2 - a is not less than b");
}
if (a > b) {
Console.WriteLine("Line 3 - a is greater than b");
} else {
Console.WriteLine("Line 3 - a is not greater than b");
}
/* 改变 a 和 b 的值 */
a = 5;
b = 20;
if (a <= b) {
Console.WriteLine("Line 4 - a is either less than or equal to b");
}
if (b >= a) {
Console.WriteLine("Line 5-b is either greater than or equal to b");
}
}
}
当编译并执行上述代码时,它会产生以下结果 -
Line 1 - a is not equal to b Line 2 - a is not less than b Line 3 - a is greater than b Line 4 - a is either less than or equal to b Line 5 - b is either greater than or equal to b
关系运算符与条件语句
您可以在条件语句中使用关系运算符来比较两个操作数。
示例
在下面的示例中,我们使用关系运算符比较两个整数值并打印结果。
using System;
class Program
{
static void Main()
{
int a = 10, b = 5;
Console.WriteLine("a == b: " + (a == b));
Console.WriteLine("a != b: " + (a != b));
Console.WriteLine("a > b: " + (a > b));
Console.WriteLine("a < b: " + (a < b));
Console.WriteLine("a >= b: " + (a >= b));
Console.WriteLine("a <= b: " + (a <= b));
}
}
当编译并执行上述代码时,它会产生以下结果 -
a == b: False a != b: True a > b: True a < b: False a >= b: True a <= b: False
示例
在下面的示例中,我们在 if-else 语句中使用关系运算符来确定温度条件。
using System;
class Program
{
static void Main()
{
int temperature = 25;
if (temperature > 30)
{
Console.WriteLine("It's a hot day!");
}
else
{
Console.WriteLine("The weather is pleasant.");
}
}
}
当编译并执行上述代码时,它会产生以下结果 -
The weather is pleasant.
for 循环中的关系运算符
您可以在循环中使用关系运算符来控制循环的执行。
示例
在下面的示例中,我们在 for 循环中使用关系运算符来控制迭代次数。
using System;
class Program
{
static void Main()
{
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("Iteration: " + i);
}
}
}
当编译并执行上述代码时,它会产生以下结果 -
Iteration: 1 Iteration: 2 Iteration: 3 Iteration: 4 Iteration: 5
用例
以下是关系运算符的一些实际用例:
示例:验证用户年龄
在以下示例中,我们使用关系运算符来验证用户是否有资格投票。
using System;
class Program
{
static void Main()
{
int age = 17;
if (age >= 18)
{
Console.WriteLine("You are eligible to vote.");
}
else
{
Console.WriteLine("You are not eligible to vote.");
}
}
}
当编译并执行上述代码时,它会产生以下结果 -
You are not eligible to vote.
示例:检查账户余额以进行提款
在下面的示例中,我们使用关系运算符来检查提款金额是否有效。
using System;
class Program
{
static void Main()
{
double balance = 5000;
double withdrawal = 3000;
if (withdrawal <= balance)
{
balance -= withdrawal;
Console.WriteLine("Withdrawal successful! Remaining balance: " + balance);
}
else
{
Console.WriteLine("Insufficient funds.");
}
}
}
当编译并执行上述代码时,它会产生以下结果 -
Withdrawal successful! Remaining balance: 2000
示例:检查学生是否通过
在下面的示例中,我们使用关系运算符来确定学生是否通过了考试。
using System;
class Program
{
static void Main()
{
int marks = 45;
if (marks >= 50)
{
Console.WriteLine("You passed the exam.");
}
else
{
Console.WriteLine("You failed. Better luck next time.");
}
}
}
当编译并执行上述代码时,它会产生以下结果 -
You failed. Better luck next time.
最佳实践
以下是使用比较运算符的最佳实践:
- 必须使用 == 进行比较,而不是 =(赋值)。
- 应使用 && 和 || 来代替无效的链式比较,例如 5 < x < 10。
- 应使用 string.IsNullOrEmpty(x) 而不是 x.Length > 0 来避免异常。
- 应避免冗余比较;使用 if (x) 而不是 if (x == true)。

