关系运算符 #
一、关系运算符概述 #
关系运算符用于比较两个值,返回布尔结果(true或false)。
| 运算符 | 名称 | 示例 |
|---|---|---|
| == | 等于 | a == b |
| != | 不等于 | a != b |
| > | 大于 | a > b |
| < | 小于 | a < b |
| >= | 大于等于 | a >= b |
| <= | 小于等于 | a <= b |
二、基本比较运算 #
2.1 等于运算符 #
csharp
int a = 10;
int b = 10;
bool isEqual = a == b;
string s1 = "Hello";
string s2 = "Hello";
bool sameString = s1 == s2;
bool sameReference = object.ReferenceEquals(s1, s2);
2.2 不等于运算符 #
csharp
int a = 10;
int b = 20;
bool notEqual = a != b;
string s1 = "Hello";
string s2 = "World";
bool different = s1 != s2;
2.3 大于小于运算符 #
csharp
int a = 10;
int b = 20;
bool greater = a > b;
bool less = a < b;
bool greaterOrEqual = a >= 10;
bool lessOrEqual = b <= 20;
三、数值比较 #
3.1 整数比较 #
csharp
int age = 25;
if (age >= 18)
{
Console.WriteLine("成年人");
}
if (age < 60)
{
Console.WriteLine("未退休");
}
3.2 浮点数比较 #
csharp
double a = 0.1 + 0.2;
double b = 0.3;
bool equal = a == b;
double epsilon = 1e-10;
bool approximatelyEqual = Math.Abs(a - b) < epsilon;
3.3 Decimal比较 #
csharp
decimal price1 = 99.99m;
decimal price2 = 99.99m;
bool samePrice = price1 == price2;
四、字符串比较 #
4.1 == 运算符 #
csharp
string s1 = "Hello";
string s2 = "Hello";
string s3 = "hello";
bool equal1 = s1 == s2;
bool equal2 = s1 == s3;
4.2 Equals方法 #
csharp
string s1 = "Hello";
string s2 = "hello";
bool equal = s1.Equals(s2);
bool equalIgnoreCase = s1.Equals(s2, StringComparison.OrdinalIgnoreCase);
4.3 Compare方法 #
csharp
string s1 = "apple";
string s2 = "banana";
int result = string.Compare(s1, s2);
if (result < 0)
Console.WriteLine("s1 < s2");
else if (result > 0)
Console.WriteLine("s1 > s2");
else
Console.WriteLine("s1 == s2");
4.4 StringComparison #
csharp
string s1 = "Hello";
string s2 = "hello";
bool equal1 = string.Equals(s1, s2, StringComparison.Ordinal);
bool equal2 = string.Equals(s1, s2, StringComparison.OrdinalIgnoreCase);
bool equal3 = string.Equals(s1, s2, StringComparison.CurrentCulture);
五、引用类型比较 #
5.1 引用相等 #
csharp
public class Person
{
public string Name { get; set; }
}
var p1 = new Person { Name = "张三" };
var p2 = new Person { Name = "张三" };
var p3 = p1;
bool sameRef1 = p1 == p2;
bool sameRef2 = p1 == p3;
bool sameRef3 = object.ReferenceEquals(p1, p2);
5.2 重写Equals #
csharp
public class Person : IEquatable<Person>
{
public string Name { get; set; }
public int Age { get; set; }
public override bool Equals(object obj)
{
return Equals(obj as Person);
}
public bool Equals(Person other)
{
if (other is null) return false;
return Name == other.Name && Age == other.Age;
}
public override int GetHashCode()
{
return HashCode.Combine(Name, Age);
}
public static bool operator ==(Person left, Person right)
{
if (left is null) return right is null;
return left.Equals(right);
}
public static bool operator !=(Person left, Person right)
{
return !(left == right);
}
}
六、可空类型比较 #
6.1 可空值类型 #
csharp
int? a = 10;
int? b = 10;
int? c = null;
bool equal1 = a == b;
bool equal2 = a == c;
bool equal3 = c == null;
6.2 可空引用类型 #
csharp
string? s1 = null;
string? s2 = null;
bool equal = s1 == s2;
七、is运算符 #
7.1 类型检查 #
csharp
object obj = "Hello";
if (obj is string)
{
Console.WriteLine("是字符串");
}
if (obj is int)
{
Console.WriteLine("是整数");
}
7.2 模式匹配 #
csharp
object obj = 42;
if (obj is int number)
{
Console.WriteLine($"整数:{number}");
}
if (obj is string text)
{
Console.WriteLine($"字符串:{text}");
}
7.3 类型模式 #
csharp
object value = 42;
string result = value switch
{
int i => $"整数:{i}",
double d => $"浮点数:{d}",
string s => $"字符串:{s}",
null => "null",
_ => "其他类型"
};
八、条件表达式中的使用 #
8.1 if语句 #
csharp
int score = 85;
if (score >= 90)
{
Console.WriteLine("优秀");
}
else if (score >= 80)
{
Console.WriteLine("良好");
}
else if (score >= 60)
{
Console.WriteLine("及格");
}
else
{
Console.WriteLine("不及格");
}
8.2 三元运算符 #
csharp
int age = 20;
string status = age >= 18 ? "成年" : "未成年";
int number = -5;
int abs = number >= 0 ? number : -number;
8.3 while循环 #
csharp
int count = 0;
while (count < 10)
{
Console.WriteLine(count);
count++;
}
九、实战示例 #
9.1 范围检查 #
csharp
public static bool IsInRange(int value, int min, int max)
{
return value >= min && value <= max;
}
public static bool IsValidAge(int age)
{
return age >= 0 && age <= 150;
}
public static bool IsValidScore(int score)
{
return score >= 0 && score <= 100;
}
9.2 字符串验证 #
csharp
public static bool IsValidEmail(string email)
{
return email != null && email.Contains("@") && email.Contains(".");
}
public static bool IsValidPhoneNumber(string phone)
{
return phone != null && phone.Length == 11 && phone.StartsWith("1");
}
9.3 对象比较 #
csharp
public class Range
{
public int Min { get; }
public int Max { get; }
public Range(int min, int max)
{
Min = min;
Max = max;
}
public bool Contains(int value)
{
return value >= Min && value <= Max;
}
public bool Overlaps(Range other)
{
return Min <= other.Max && Max >= other.Min;
}
}
十、最佳实践 #
10.1 浮点数比较 #
csharp
public static bool ApproximatelyEqual(double a, double b, double epsilon = 1e-10)
{
return Math.Abs(a - b) < epsilon;
}
10.2 字符串比较 #
csharp
public static bool EqualsIgnoreCase(string a, string b)
{
return string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
}
10.3 空值检查 #
csharp
public static bool IsNullOrEmpty(string value)
{
return string.IsNullOrEmpty(value);
}
public static bool IsNullOrWhiteSpace(string value)
{
return string.IsNullOrWhiteSpace(value);
}
十一、总结 #
关系运算符要点:
| 运算符 | 说明 |
|---|---|
| == != | 相等比较 |
| > < | 大于小于 |
| >= <= | 大于等于、小于等于 |
| is | 类型检查 |
下一步,让我们学习逻辑运算符!
最后更新:2026-03-26