Tra*_*y13 2 .net c# using-statement
我想知道我的使用using是否正确.在using统计中,我决定是否应该在游戏中使用图像.
Image imageOfEnemy;
using(imageOfEnemy=Bitmap.FromFile(path))
{
// some stuff with the imageOfEnemy variable
}
Run Code Online (Sandbox Code Playgroud)
根据我的理解,我现在不需要打电话Dispose.
是的,您正确使用它.您不需要显式处理Bitmap,因为它将由using语句处理.您可以通过在内部声明图像变量来进一步简化:
using(var imageOfEnemy = Bitmap.FromFile(path))
{
// some stuff with the imageOfEnemy variable
}
Run Code Online (Sandbox Code Playgroud)
这大致相当于:
{
var imageOfEnemy = Bitmap.FromFile(path);
try
{
// some stuff with the imageOfEnemy variable
}
finally
{
((IDisposable)imageOfEnemy).Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1436 次 |
| 最近记录: |