C# 使用模式匹配以及 is 和 as 运算符安全地进行强制转换

C# 使用模式匹配以及 is 和 as 运算符安全地进行强制转换

is 运算符

用于检查一个对象是否是特定类型。C# 7 引入了 is 运算符的模式匹配功能,可以在检查类型的同时进行类型转换

csharp 复制代码
object obj = "Hello, world!";

if (obj is string str)
{
    Console.WriteLine($"String length: {str.Length}");
}

is 运算符不仅检查 obj 是否是 string 类型,还将其转换为 str 变量,并在条件块中使用。这样可以避免不必要的显式转换,提高代码的安全性和简洁性。

as 运算符

运算符用于尝试将一个对象转换为特定类型。如果转换成功,则返回转换后的对象;如果转换失败,则返回 null

csharp 复制代码
object obj = "Hello, world!";
string str = obj as string;

if (str != null)
{
    Console.WriteLine($"String length: {str.Length}");
}
else
{
    Console.WriteLine("obj is not a string.");
}

这里 obj 被尝试转换为 string 类型,并赋值给 str。随后可以通过 str != null 来检查转换是否成功,从而避免不安全的显式类型转换。

模式匹配

C# 7 引入了模式匹配特性,使得你可以在 switch 语句和 is 运算符中结合模式进行类型检查和转换

csharp 复制代码
object obj = 42;

switch (obj)
{
    case int i:
        Console.WriteLine($"Integer: {i}");
        break;
    case string s:
        Console.WriteLine($"String: {s}");
        break;
    default:
        Console.WriteLine("Unknown type.");
        break;
}

这里 switch 语句通过模式匹配直接检查 obj 的类型,并将其转换为对应的类型变量 i 或 s。这种方式不仅简化了类型检查的代码,还确保了类型转换的安全性。

使用场景

  • 类型检查:在处理未知类型的对象时,使用 is 或模式匹配可以进行类型检查并转换,避免了不安全的显式转换。
  • 多态性:在继承关系中,is 和 as 可以用于检查和转换对象为基类或子类,这可以在运行时决定对象的具体类型。
  • 代码简洁性:模式匹配可以结合 switch 语句或 is 来简化类型检查和转换的代码逻辑,使其更易读。

拓展

  • is 和 as 运算符在类型检查和转换时,会考虑对象的实际类型以及继承关系
csharp 复制代码
class Animal {}
class Dog : Animal {}

object obj = new Dog();

if (obj is Animal animal) {
    Console.WriteLine("It's an Animal.");
}

Animal animal = obj as Animal;

if (animal != null) {
    Console.WriteLine("It's an Animal.");
}

obj 是 Dog 的实例,但 Dog 继承自 Animal,所以 is Animal 检查会成功,并将 obj 转换为 animal

父类和子类的检查

is 和 as 运算符都可以识别并转换父类和子类之间的关系。

  • 向上转换(Upcasting):将子类转换为父类是安全的,也可以直接使用 is 或 as 来实现。
  • 向下转换(Downcasting):如果需要将父类转换为子类,你需要确保类型匹配,否则转换会失败。
csharp 复制代码
Animal animal = new Dog();
Dog dog = animal as Dog;

if (dog != null) {
    Console.WriteLine("It's a Dog.");
}

在这里,我们将 animal 向下转换为 Dog。如果 animal 实际上不是 Dog 的实例,则转换会失败,dog 会是 null。

  • 继承链的完整性:is 和 as 会识别整个继承链。因此,你可以使用它们来检查父类、子类甚至兄弟类之间的关系。

校验与对象类型

is 和 as 在类型检查和转换时,并不一定要求目标类型是 object。

is 和 as 运算符在类型检查和转换时,也可以用于泛型类型。

csharp 复制代码
class Box<T> {}

object obj = new Box<int>();

if (obj is Box<int> boxInt) {
    Console.WriteLine("It's a Box<int>.");
}
csharp 复制代码
Box<string> boxString = new Box<string>();
object obj = boxString;

Box<string> convertedBox = obj as Box<string>;

if (convertedBox != null) {
    Console.WriteLine("Successfully converted to Box<string>.");
}
相关推荐
iCxhust35 分钟前
使用 Visual Studio 创建安装包的完整指南
ide·单片机·嵌入式硬件·microsoft·c#·visual studio
蓝胖子不会敲代码1 小时前
跟着AI学习C# Day12
学习·microsoft·c#
编程乐趣1 小时前
C#实现图片文字识别
开发语言·c#
小马+4 小时前
内存优化有哪些方法?
c#
蓝胖子不会敲代码4 小时前
跟着AI学习C# Day14
开发语言·学习·c#
爱吃煎蛋的小新5 小时前
WPF入门 #1 WPF布局基础
笔记·学习·c#·wpf
夜空晚星灿烂17 小时前
C# 关于LINQ语法和类型的使用
windows·c#·linq
code bean17 小时前
【C#】 C#中 nameof 和 ToString () 的用法与区别详解
android·java·c#
普通网友1 天前
C# 中委托和事件的深度剖析与应用场景
java·算法·c#
钢铁男儿1 天前
C#结构体性能暴击指南:从内存陷阱到零损耗实战
开发语言·c#