c# - 将 null 类型转换为变量的数据类型来初始化变量

标签 c# types casting null

我遇到过这样的代码:

List<string> list = (List<string>)null;

程序员没有通过以下方式初始化的原因是什么:

List<string> list = null;

两者有区别吗?

这是从另一种编程语言迁移过来的习惯吗?也许是 C、C++ 或 Java?

最佳答案

Is there a difference between the two?

不,没有区别。

ILSpy , 此行 List<string> list = (List<string>)null;变为 List<string> list = null;

Is this a habit that migrated from another programming language?

不能说。可能是,早些时候有一些不同于 null 的东西然后改为null .

List<string> list = (List<string>) Session["List"];

关于c# - 将 null 类型转换为变量的数据类型来初始化变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26594024/

相关文章:

c# - 为高对比度主题重新设计 AvalonDock?

c - 是否可以将 C 数据类型存储在变量中?

c++ - 如何将结构转换为 char[] 数组?

java - 字节数组中 char 的大小

java - == Java中对象的运算符

c# - 在文本 block 文本 WPF 中使用 &

c# - 单声道 2.6.7 : Array Initializer Bug?

Java注释处理器: javax.validation.constraints.过去的注释更改了字段类型

单个语句中的 C# 类型分配和方法调用

javascript - 是否可以将对象转换为 Flow 中的精确类型?