C# 将实例变量复制到同一类函数中的局部变量
我最近一直在浏览一个开源项目上的一些代码,发现这种代码出现很多次:
class SomeClass
{
private int SomeNumber = 42;
public ReturnValue UseSomeNumber(...)
{
int someNumberCopy = this.SomeNumber;
if (someNumberCopy > ...)
{
// ... do some work with someNumberCopy
}
else
{
// ... do something else with someNumberCopy
}
}
}
制作实例变量的副本有什么真正的好处吗?
I have been looking through some code on an open source project recently and found many occurrences of this kind of code:
class SomeClass
{
private int SomeNumber = 42;
public ReturnValue UseSomeNumber(...)
{
int someNumberCopy = this.SomeNumber;
if (someNumberCopy > ...)
{
// ... do some work with someNumberCopy
}
else
{
// ... do something else with someNumberCopy
}
}
}
Is there any real benefit to making a copy of the instance variable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这可能是多线程程序的一部分。虽然这段代码不是线程安全的,但它确保一旦分配了复制变量,它就不会被其他线程更改,并且此后的所有函数代码都一致运行。
带有事件的类似代码在多线程环境中变得至关重要:
这里,如果在测试空值之后且在调用之前事件变为空,则在不制作本地副本的情况下,可能会出现空指针异常。
Possibly this is part of multi-threaded program. Though this code is not thread-safe, it ensures that once copy variable is assigned, it is not changed by another threads, and all function code after this runs consistently.
Similar code with events becomes critical in multi-threaded environment:
Here, without making a local copy, it is possible to get null-pointer exception, if event becomes null after testing for null, and before invoking.
否,除非您不想更改 SomeNumber 的值并且打算更新 someNumberCopy。就像您要循环次数并将 someNumberCopy 递减到零以跟踪计数一样。
我想像这样复制变量可以保护您免受某些外部函数更改 SomeNumber 的影响,并在执行操作时在您不知情的情况下更改它。如果该类应该在多线程应用程序中使用,我可能会看到这一点。也许我不会这样做,但这可能是作者的意图。
No unless you don't want to change the value of SomeNumber and you intend on updating someNumberCopy. Like if you were going to loop the number of times and were going to decrement someNumberCopy down to zero to keep track of the count.
I suppose copying the variable like that could protect you from some outside function altering SomeNumber and changing it without your knowledge while performing an operation. I could potentially see this if the class was supposed to be used in a multi-threaded application. Maybe not he way I would go about it, but that could have been the author's intent.
// ... do some work with someNumberCopy
是用 someNumberCopy 做某事吗?它改变了价值吗?它是否被传递给可能改变值的方法?如果不是,那就不,没有任何好处。
Is the
// ... do some work with someNumberCopy
doing something with someNumberCopy? Is it changing the value? Is it being passed to a method that might change the value?If not then no, there is no benefit.
您遗漏了一些重要的细节。
如果您有多个线程访问 SomeNumber 并且出现这种情况:
那么制作副本很重要。
You left out some important details.
If you have multiple threads accessing SomeNumber and this scenario:
Then it is important to make a copy.
如果
this.SomeNumber
可以被另一个线程修改,那么它可能会很有用,但是在该方法的持续时间内,至关重要的是someNumberCopy
不能被更改(它可以被更改)与SomeNumber
同步),那么这可能是可行的,否则我看不出它的原因。It could be useful if
this.SomeNumber
can be modified by another thread, but for the duration of this method it is vital that thesomeNumberCopy
cannot be changed (it can be out of sync withSomeNumber
) then this might be viable, otherwise I don't see a reason for it.我看到的唯一好处是为该方法提供变量的“只读版本”
The only benefit I see is for having a "readonly version" of the variable just for that method