object obj = "Hello World"; // 传统用法 if (obj is string) { Console.WriteLine("是字符串"); } // 模式匹配用法 (推荐) if (obj is string str) { // 如果转换成功,str 变量直接可用 Console.WriteLine($"字符串长度: {str.Length}"); }