在 C# 中,如果您已经使用完一个对象变量,是否有必要将其分配给 null,即使它无论如何都会超出范围?
In C#, is it necessary to assign an object variable to null if you have finished using it, even when it will go out of scope anyway?
不,这实际上可能很危险且容易出错(考虑一下有人可能会在以后尝试使用它,而没有意识到它已被设置)为空).仅当存在将其设置为 null 的合乎逻辑的原因时才将其设置为 null.
No, and that could in fact be dangerous and bug-prone (consider the possibility that someone might try to use it later on, not realizing it had been set to null). Only set something to null if there's a logical reason to set it to null.
这篇关于C#:应该将对象变量分配给 null 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何检查字符串是否为空How to check if String is null(如何检查字符串是否为空)
Equals(item, null) 或 item == nullEquals(item, null) or item == null(Equals(item, null) 或 item == null)
覆盖 == 运算符.如何与空值进行比较?Overriding == operator. How to compare to null?(覆盖 == 运算符.如何与空值进行比较?)
成员访问中的问号在 C# 中是什么意思?What does the question mark in member access mean in C#?(成员访问中的问号在 C# 中是什么意思?)
||(或)C# 中的 Linq 运算符The || (or) Operator in Linq with C#(||(或)C# 中的 Linq 运算符)
C# 空合并运算符等效于 C++C# null coalescing operator equivalent for c++(C# 空合并运算符等效于 C++)