C# - 杂项运算符
什么是 C# 杂项运算符?
C# 杂项运算符 是不属于算术、逻辑、关系或位运算类别的特殊运算符。这些运算符主要用于类型检查、空值处理和内存引用。
C# 杂项运算符列表
还有其他一些重要的运算符,包括 sizeof 和 ? : 由 C# 支持。
运算符 | 描述 | 示例 |
---|---|---|
sizeof() | 返回数据类型的大小。 | sizeof(int) 返回 4。 |
typeof() | 返回类的类型。 | typeof(StreamReader); |
& | 返回变量的地址。 | &a; 返回变量的实际地址。 |
* | 指向变量的指针。 | *a;创建指向变量"a"的指针。 |
? : | 条件表达式 | 如果条件为真?则值为 X :否则值为 Y |
is | 判断对象是否属于特定类型。 | If( Ford is Car) // 检查 Ford 是否是 Car 类的对象。 |
as | 如果转换失败,则进行类型转换,不引发异常。 | Object obj = new StringReader("Hello");
StringReader r = obj as StringReader; |
其他运算符示例
using System; namespace OperatorsAppl { class Program { static void Main(string[] args) { /* sizeof 运算符的示例 */ Console.WriteLine("The size of int is {0}", sizeof(int)); Console.WriteLine("The size of short is {0}", sizeof(short)); Console.WriteLine("The size of double is {0}", sizeof(double)); /* 三元运算符示例 */ int a, b; a = 10; b = (a == 1) ? 20 : 30; Console.WriteLine("Value of b is {0}", b); b = (a == 10) ? 20 : 30; Console.WriteLine("Value of b is {0}", b); Console.ReadLine(); } } }
当编译并执行上述代码时,它会产生以下结果 -
The size of int is 4 The size of short is 2 The size of double is 8 Value of b is 30 Value of b is 20
更多杂项运算符示例
示例 1:三元运算符 (?:)
三元运算符用于在一行中替换简单的 if-else 条件。
在下面的示例中,我们使用三元运算符检查年龄是否为 18 岁或以上,并相应地返回"成人"或"未成年人"。
using System; class Program { static void Main() { int age = 20; string result = (age >= 18) ? "Adult" : "Minor"; Console.WriteLine(result); } }
当编译并执行上述代码时,它会产生以下结果 -
Adult
示例 2:空值合并运算符 (??)
空值合并运算符 (??) 在处理具有空值的变量时非常有用。如果变量为空,此运算符会直接赋值默认值。
在下面的示例中,我们使用空值合并运算符,将 100 赋值给为空的变量。
using System; class Program { static void Main() { int? number = null; int result = number ?? 100; // 如果"number"为空,则赋值为 100 Console.WriteLine(result); } }
当编译并执行上述代码时,它会产生以下结果 -
100
示例 3:空合并赋值 (??=)
??= 运算符仅当变量为空时才赋值。
在下面的示例中,我们使用空合并赋值运算符,当变量为空时,分配一条默认消息。
using System; class Program { static void Main() { string message = null; message ??= "Default Message"; // 仅当消息为空时才分配值 Console.WriteLine(message); } }
当编译并执行上述代码时,它会产生以下结果 -
Default Message
示例 4:类型检查运算符 (is)
is 运算符检查对象是否属于特定类型。
在下面的示例中,我们使用"is"运算符检查给定对象是否为字符串类型。
using System; class Program { static void Main() { object obj = "Hello, World!"; if (obj is string) { Console.WriteLine("obj is a string."); } } }
当编译并执行上述代码时,它会产生以下结果 -
obj is a string.
示例 5:安全类型转换 (as)
as 运算符可以安全地执行类型转换,且不会引发异常。
在下面的示例中,我们使用"as"运算符将对象安全地转换为字符串。
using System; class Program { static void Main() { object obj = "Hello, C#"; string str = obj as string; if (str != null) { Console.WriteLine("Safe casting successful: " + str); } } }
当编译并执行上述代码时,它会产生以下结果 -
Safe casting successful: Hello, C#
示例 6:使用 sizeof 获取数据类型的大小
sizeof 运算符返回数据类型的大小(以字节为单位)。
在下面的示例中,我们使用 sizeof 运算符来确定整数类型的大小(以字节为单位)。
using System; class Program { static unsafe void Main() { Console.WriteLine("Size of int: " + sizeof(int) + " bytes"); } }
当编译并执行上述代码时,它会产生以下结果 -
Size of int: 4 bytes
用例
以下示例演示了 C# 中各种运算符的实际用例:
示例 7:在 Web 表单中使用 ?? 作为默认值
在以下示例中,如果用户输入为空,我们将使用空合并运算符来分配默认用户名。
using System; class Program { static void Main() { string userInput = null; string finalInput = userInput ?? "Default Username"; Console.WriteLine("User: " + finalInput); } }
当编译并执行上述代码时,它会产生以下结果 -
User: Default Username
示例 8:使用 is 检查对象类型
在下面的示例中,我们使用"is"运算符检查给定对象是否为整数,如果是,则检索其值。
using System; class Program { static void Main() { object obj = 42; if (obj is int number) { Console.WriteLine("obj is an integer: " + number); } } }
当编译并执行上述代码时,它会产生以下结果 -
obj is an integer: 42
最佳实践
以下是使用杂项运算符和类型运算符的最佳实践。
- 对于简洁的条件表达式,应使用 ?:。
- 对于安全的空值,应使用 ?? 和 ??=。
- 对于运行时类型检查和安全的类型转换,应使用 is 和 as。
- 必须避免对引用类型使用 sizeof。
- 为了更清晰起见,应在三元表达式中使用括号。