0
点赞
收藏
分享

微信扫一扫

C#使用using代替try-finally

滚过红尘说红尘 2022-07-27 阅读 68

// This try-finally statement only calls Dispose in the finally block.
Font font1 = new Font("Arial", 10.0f);
try
{
byte charset = font1.GdiCharSet;
}
finally
{
if (font1 != null)
{
((IDisposable)font1).Dispose();
}
}

// You can do the same thing with a using statement.
using (Font font2 = new Font("Arial", 10.0f))
{
byte charset = font2.GdiCharSet;
}



举报

相关推荐

0 条评论