用户登录
用户注册

分享至

C#:应该将对象变量分配给 null 吗?

  • 作者: 袅男
  • 来源: 51数据库
  • 2022-10-20

问题描述

在 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.

软件
前端设计
程序设计
Java相关