我想知道我对 using
的使用是否正确。在 using
语句中,我决定是否应该在游戏中使用图像。
Image imageOfEnemy;
using(imageOfEnemy=Bitmap.FromFile(path))
{
// some stuff with the imageOfEnemy variable
}
据我了解,我现在不需要调用 Dispose
。
最佳答案
是的,您正在正确使用它。您不需要显式处理位图,因为它会由 using
语句处理。您可以通过在内部声明图像变量来进一步简化:
using(var imageOfEnemy = Bitmap.FromFile(path))
{
// some stuff with the imageOfEnemy variable
}
大致相当于:
{
var imageOfEnemy = Bitmap.FromFile(path);
try
{
// some stuff with the imageOfEnemy variable
}
finally
{
((IDisposable)imageOfEnemy).Dispose();
}
}
关于c# - 我是否正确使用了 "using"语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4966931/