逻辑运算符 #

一、逻辑运算符概述 #

C#提供了以下逻辑运算符:

运算符 名称 说明
&& 逻辑与 两个都为true时返回true
|| 逻辑或 至少一个为true时返回true
! 逻辑非 取反
& 位与/逻辑与 不短路
| 位或/逻辑或 不短路
^ 异或 相同为false,不同为true

二、逻辑与运算 #

2.1 && 运算符 #

csharp
bool a = true;
bool b = true;
bool result = a && b;

bool c = true;
bool d = false;
bool result2 = c && d;

真值表:

A B A && B
true true true
true false false
false true false
false false false

2.2 短路求值 #

csharp
int x = 10;
bool result = x > 5 && x++ < 20;

int y = 10;
bool result2 = y > 20 && y++ < 30;

2.3 应用场景 #

csharp
string name = null;

if (name != null && name.Length > 0)
{
    Console.WriteLine(name);
}

int? age = null;
if (age.HasValue && age.Value >= 18)
{
    Console.WriteLine("成年人");
}

三、逻辑或运算 #

3.1 || 运算符 #

csharp
bool a = true;
bool b = false;
bool result = a || b;

bool c = false;
bool d = false;
bool result2 = c || d;

真值表:

A B A || B
true true true
true false true
false true true
false false false

3.2 短路求值 #

csharp
int x = 10;
bool result = x < 5 || x++ > 5;

int y = 10;
bool result2 = y < 20 || y++ > 20;

3.3 应用场景 #

csharp
int age = 16;
bool hasPermission = false;

if (age >= 18 || hasPermission)
{
    Console.WriteLine("允许进入");
}

string role = "admin";
if (role == "admin" || role == "manager")
{
    Console.WriteLine("有管理权限");
}

四、逻辑非运算 #

4.1 ! 运算符 #

csharp
bool a = true;
bool notA = !a;

bool b = false;
bool notB = !b;

真值表:

A !A
true false
false true

4.2 应用场景 #

csharp
bool isLoggedIn = false;

if (!isLoggedIn)
{
    Console.WriteLine("请先登录");
}

string name = "";
if (string.IsNullOrEmpty(name))
{
    Console.WriteLine("名称不能为空");
}

if (!string.IsNullOrEmpty(name))
{
    Console.WriteLine($"欢迎,{name}");
}

五、异或运算 #

5.1 ^ 运算符 #

csharp
bool a = true;
bool b = false;
bool result = a ^ b;

bool c = true;
bool d = true;
bool result2 = c ^ d;

真值表:

A B A ^ B
true true false
true false true
false true true
false false false

5.2 应用场景 #

csharp
bool isWeekend = true;
bool isHoliday = false;

if (isWeekend ^ isHoliday)
{
    Console.WriteLine("只有一个是休息日");
}

bool a = true;
a = !a;
a = a ^ true;

六、非短路运算符 #

6.1 & 和 | 运算符 #

csharp
bool Method1()
{
    Console.WriteLine("Method1 called");
    return true;
}

bool Method2()
{
    Console.WriteLine("Method2 called");
    return false;
}

bool result1 = Method1() && Method2();
bool result2 = Method1() & Method2();

bool result3 = Method1() || Method2();
bool result4 = Method1() | Method2();

6.2 使用场景 #

csharp
bool flag1 = false;
bool flag2 = false;

if (flag1 & flag2)
{
}

if (flag1 | flag2)
{
}

七、复合条件表达式 #

7.1 多条件组合 #

csharp
int age = 25;
bool hasLicense = true;
bool hasInsurance = true;

if (age >= 18 && hasLicense && hasInsurance)
{
    Console.WriteLine("可以驾驶");
}

int score = 85;
int attendance = 90;

if (score >= 60 || attendance >= 80)
{
    Console.WriteLine("通过考核");
}

7.2 混合使用 #

csharp
int age = 25;
bool isMember = true;
bool hasCoupon = false;

if ((age >= 18 && age <= 65) && (isMember || hasCoupon))
{
    Console.WriteLine("享受优惠");
}

bool a = true;
bool b = false;
bool c = true;

bool result = a && (b || c);

7.3 德摩根定律 #

csharp
bool a = true;
bool b = false;

bool result1 = !(a && b);
bool result2 = !a || !b;

bool result3 = !(a || b);
bool result4 = !a && !b;

八、可空布尔运算 #

8.1 bool? 类型 #

csharp
bool? a = true;
bool? b = false;
bool? c = null;

bool? andResult = a & b;
bool? orResult = a | b;

三值逻辑真值表:

A B A & B A | B
true true true true
true false false true
true null null true
false true false true
false false false false
false null false null
null true null true
null false false null
null null null null

九、实战示例 #

9.1 用户验证 #

csharp
public static bool ValidateUser(string username, string password, int age)
{
    return !string.IsNullOrEmpty(username) 
        && username.Length >= 3 
        && !string.IsNullOrEmpty(password) 
        && password.Length >= 6 
        && age >= 18;
}

9.2 权限检查 #

csharp
public static bool HasAccess(User user, string resource)
{
    return user != null 
        && user.IsActive 
        && (user.Role == "Admin" || user.Permissions.Contains(resource));
}

9.3 数值范围验证 #

csharp
public static bool IsValidRange(int value, int? min = null, int? max = null)
{
    return (!min.HasValue || value >= min.Value) 
        && (!max.HasValue || value <= max.Value);
}

9.4 字符串验证 #

csharp
public static bool IsValidInput(string input)
{
    return !string.IsNullOrWhiteSpace(input) 
        && input.Length >= 2 
        && input.Length <= 50;
}

十、最佳实践 #

10.1 使用短路运算符 #

csharp
if (obj != null && obj.IsValid)
{
}

if (string.IsNullOrEmpty(name) || name.Length > 100)
{
}

10.2 简化条件表达式 #

csharp
if (isValid == true)
{
}

if (isValid)
{
}

if (isValid == false)
{
}

if (!isValid)
{
}

10.3 提取复杂条件 #

csharp
bool isAdult = age >= 18;
bool hasValidId = id != null && id.Length > 0;
bool hasPaymentMethod = creditCard != null || paypal != null;

if (isAdult && hasValidId && hasPaymentMethod)
{
    ProcessOrder();
}

十一、总结 #

逻辑运算符要点:

运算符 说明
&& 短路与
|| 短路或
! 逻辑非
^ 异或
& | 非短路运算

下一步,让我们学习位运算符!

最后更新:2026-03-26