使用声明和表格
我有一个基本表单,将尝试加载和播放用户的视频。可以从公共静态 ShowDialogForm 方法调用该表单。该方法将尝试初始化视频,如果成功则返回表单,否则操作将被取消。在我的办公室,我们使用重构工具,它抱怨我的方法中缺少 using 语句。所以我的问题是,在这种情况下 using 语句是否提供了任何好处。
这是原始代码
public static DialogResult ShowDialogForm(VideoNames videoName, Course course, IWin32Window parent)
{
FlashPlayer form = new FlashPlayer();
if (form.Initialize(videoName, course))
{
return form.ShowDialog(parent);
}
else
{
return DialogResult.Cancel;
}
}
这是重构工具建议的代码
public static DialogResult ShowDialogForm(VideoNames videoName, Course course, IWin32Window parent)
{
using (FlashPlayer form = new FlashPlayer())
{
if (form.Initialize(videoName, course))
{
return form.ShowDialog(parent);
}
else
{
return DialogResult.Cancel;
}
}
}
I have a basic form that will attempt to load and play a video of the user. The form can be called from a public static ShowDialogForm method. That method will attempt to initialize a video if successful then the form is returned otherwise the action is canceled. At my office we use a refactoring tool, and it is complaining about the lack of a using statement in my method. So my question is benifit, if any does the using statement offer in this instance.
This is the original code
public static DialogResult ShowDialogForm(VideoNames videoName, Course course, IWin32Window parent)
{
FlashPlayer form = new FlashPlayer();
if (form.Initialize(videoName, course))
{
return form.ShowDialog(parent);
}
else
{
return DialogResult.Cancel;
}
}
This is the code suggested by the refactoring tool
public static DialogResult ShowDialogForm(VideoNames videoName, Course course, IWin32Window parent)
{
using (FlashPlayer form = new FlashPlayer())
{
if (form.Initialize(videoName, course))
{
return form.ShowDialog(parent);
}
else
{
return DialogResult.Cancel;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,您应该将任何实现 IDisposable 的对象封装在
using
块中,以确保它被 GC 正确处置。您应该在 winforms 或 wpf 应用程序中特别意识到这一点,其中内存和进程需要更严格的控制。Yes, you should encapsulate any objects that implement IDisposable within a
using
block to ensure that it is properly disposed of by the GC. You should be especially cognizant of this within a winforms or wpf application where the memory and processes need to be more tightly controlled.嗯,该工具的投诉是有效的。当您使用 ShowDialog() 显示表单时,表单对象不会像使用 Show() 时那样自动释放。这很重要,您通常使用对话框让用户输入值,然后在 ShowDialog 返回后检索这些值。处置表单控件会带来风险,可能会触发 ObjectDisposeException。
因此,您始终使用 using 语句包装对话框的创建、显示和结果检索,以便在完成所有操作后处理表单。
但请注意,您实际上并不使用此对话框来检索任何内容。这可能意味着它根本不应该是一个对话框。因此使用 Show() 就不必处置它。如果合适的话,用户可以自由地继续使用用户界面的其余部分。非模态用户界面始终是首选。
Well, the tool's complaint is valid. When you display a form with ShowDialog() then the form object doesn't automatically get disposed like it does when you use Show(). This is important, you normally use a dialog to let the user enter values that you then retrieve after ShowDialog returns. Having the form controls disposed makes that risky, likely to trigger an ObjectDisposedException.
So you always wrap the dialog creation, display and result retrieval with a using statement so the form gets disposed after everything is done.
Note however that you don't actually use this dialog to retrieve anything. Which probably means it shouldn't be a dialog at all. So use Show() and you don't have to dispose it. And the user gets the freedom to continue using the rest of your user interface, assuming that's appropriate. Non-modal user interfaces are always preferred.
using 语句会将您对“IDisposable”FlashPlayer 的使用包装在 try-finally 块中,这意味着 FlashPlayer 将在您完成后被释放。这被认为是良好的做法,因为它释放了资源。
通常,如果某些东西实现了 IDisposable,最好将其包装在 using 语句中,或者通过调用 flashPlayer.Dispose() 来手动清理。
The using statement will wrap your use of the 'IDisposable' FlashPlayer in a try-finally block, which means the FlashPlayer will be disposed when you are finished. This is considered good practice as it releases the resource.
Normally if something implements
IDisposable
, it is good practice to wrap it in a using statement, or manually clean up after yourself by callingflashPlayer.Dispose()
.好处可能并不显着,但在类上实现 IDisposable 的情况下,using 语句可确保代码安全执行。如果类实现了它,那么很可能是有原因的。
The benefit may not be significant, but where IDisposable is implemented on a class, the using statement ensures that the code will execute safely. If the class implements it, there is most likely a reason for it.
你的工具是正确的。由于表格是一次性的,一旦用完就可以了。你必须处置。使用语句会自动为您完成此操作。
Your tool is correct. Since forms are disposable, once you're done with it. You have to dispose. Using statements does this for you automatically.