C#知识整理-运算符
运算符
C# 提供了许多运算符。 其中许多都受到内置类型的支持,可用于对这些类型的值执行基本操作。 这些运算符包括以下组:
- 算术运算符
- 比较运算符
- 布尔逻辑运算符
- 位运算符和移位运算符
- 相等运算符
算数运算符
以下运算符堆数值类型进行算数运算:
++(增量)、--(减量)、+(加)、-(减)、*(乘)、/(除)、%(余数)
int i = 10; Console.WriteLine($"i={i}"); // i=10 int j = i++; Console.WriteLine($"i={i}; j={j}");// i=11; j=10 int k = ++j; Console.WriteLine($"i={i}; j={j}; k={k}"); // i=11; j=11; k=11 // -- 的用法和++相同 j = -i; Console.WriteLine($"i={i}; j={j}"); // i=11; j=-11 //% Console.WriteLine(5 % 4);//output : 1 int x = 1; int y = 2; x += y;//和x=x+y 相同 Console.WriteLine(x);// output: 3
比较运算符/相等运算符
比较运算符包括:
<、>、<=、>=、
相等运算符包括:
==、!=
int a = 5; int b = 3; var c1 = 'a'; var c2 = 'A'; Console.WriteLine(a == b);//False Console.WriteLine(a != b);//True Console.WriteLine(c1 == c2);//False Console.WriteLine(c1 == char.ToLower(c2));//True Console.WriteLine(a > 4);//True Console.WriteLine(a > 5);//False Console.WriteLine(a >= 5);//True Console.WriteLine(a < 6);//True Console.WriteLine(a < 5);//False Console.WriteLine(a <= 5);//True
布尔逻辑运算符 - AND\OR\NOT\XOR
运算符包括:
!(逻辑非)、&(逻辑与)、|(逻辑或)、^(逻辑异或)、&&(条件逻辑与)、||(条件逻辑或)
bool a = true; bool b = false; Console.WriteLine($"{!a}|{!b}");//False|True Console.WriteLine($"{a & a}|{a & b}|{b & b}");//True|False|False Console.WriteLine($"{a | a}|{a | b}|{b | b}");//True|True|False Console.WriteLine($"{a ^ a}|{a ^ b}|{b ^ b}");//False|True|False Console.WriteLine($"{a && a}|{a && b}|{b && b}");//True|False|False Console.WriteLine($"{a || a}|{a || b}|{b || b}");//True|True|False
位运算符和移位运算符
位运算符和移位运算符包括一元位补、二进制左移和右移、无符号右移、二进制逻辑AND、OR 和异或运算符。
位运算符包括:
~(按位求补)、<<(左移)、>>(右移)、>>>(无符号右移)、&(AND)、 |(OR)、 ^(异或)
int a = 10;//0000 1010 int b = 6;//0000 0110 Console.WriteLine($"a = {a}");//a = 10 Console.WriteLine($"b = {b}");//b = 6 Console.WriteLine($"~a = {~a}");//~a = -11 Console.WriteLine($"a<<1 = {a << 1}"); //a << 1 = 20 Console.WriteLine($"a<<2 = {a << 2}"); //a << 2 = 40 Console.WriteLine($"a<<3 = {a << 3}"); //a << 3 = 80 Console.WriteLine($"b>>1 = {b << 1}"); //b >> 1 = 12 Console.WriteLine($"a & b = {a & b}"); //a & b = 2 Console.WriteLine($"a | b = {a | b}"); //a | b = 14 Console.WriteLine($"a ^ b = {a ^ b}"); //a ^ b = 12
二进制的运算我不太常用,了解以下就好
运算符重载
用户定义的类型可重载预定义的 C# 运算符。 也就是说,当一个或两个操作数都是某类型时,此类型可提供操作的自定义实现。
运算符重载通过operator关键字进行声明,运算符生命需要符合:
- 同时包含public和static关键字
- 一元运算符有一个输入参数。 二元运算符有两个输入参数。 在每种情况下,都至少有一个参数必须具有类型T或T?,其中T是包含运算符声明的类型。
public readonly struct Fraction { private readonly int num; private readonly int den; public Fraction(int numerator, int denominator) { if (denominator == 0) { throw new ArgumentException("Denominator cannot be zero.", nameof(denominator)); } num = numerator; den = denominator; } public static Fraction operator +(Fraction a) => a; public static Fraction operator -(Fraction a) => new Fraction(-a.num, a.den); public static Fraction operator +(Fraction a, Fraction b) => new Fraction(a.num * b.den + b.num * a.den, a.den * b.den); public static Fraction operator -(Fraction a, Fraction b) => a + (-b); public static Fraction operator *(Fraction a, Fraction b) => new Fraction(a.num * b.num, a.den * b.den); public static Fraction operator /(Fraction a, Fraction b) { if (b.num == 0) { throw new DivideByZeroException(); } return new Fraction(a.num * b.den, a.den * b.num); } public override string ToString() => $"{num}/{den}"; } var a = new Fraction(5, 4); var b = new Fraction(1, 2); Console.WriteLine(-a); // output: -5 / 4 Console.WriteLine(a + b); // output: 14 / 8 Console.WriteLine(a - b); // output: 6 / 8 Console.WriteLine(a * b); // output: 5 / 8 Console.WriteLine(a / b); // output: 10 / 4
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了