我什么时候应该使用'使用'? [英] When should I use 'using' ?

查看:76
本文介绍了我什么时候应该使用'使用'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


我想知道使用''使用''语句。

在一个程序中,我应该声明所有使用

子句中的实例变量?


在下面的代码中,如果MyFont& MyFont2将在Main()的主体中声明为
而不是在使用中(甚至不是

在使用中引用)?会有什么东西要求处理它们吗?


我以为垃圾收集器本身就是在做这一切...


***********

代码示例

***********

使用System.Drawing;

class a

{

public static void Main()

{

使用(Font MyFont = new Font(" Arial",10.0f),MyFont2 = new Font

(" Arial",10.0f))

{

//使用MyFont和MyFont2

} //编译器将在MyFont和MyFont2上调用Dispose


字体MyFont3 =新字体(" Arial",10.0f);

使用(MyFont3)//如果没有在这里使用会发生什么?

{

//使用MyFont3

} //编译器将调用MyFont3上的Dispose


}

}


问候,

Cyber​​tof。

Hi !

I''m wondering about the use of the ''using'' statement.
In a procedure, should i declare all instance variables in a using
clause ?

In the below code, what would happen if MyFont & MyFont2 would be
declared in the body of the Main() and not inside the using (even not
referenced in the using) ? Would be there something to call to dispose
them ?

I thought the garbage collector was doing all this by itself...

***********
code sample
***********
using System.Drawing;
class a
{
public static void Main()
{
using (Font MyFont = new Font("Arial", 10.0f), MyFont2 = new Font
("Arial", 10.0f))
{
// use MyFont and MyFont2
} // compiler will call Dispose on MyFont and MyFont2

Font MyFont3 = new Font("Arial", 10.0f);
using (MyFont3) // WHAT HAPPEN IF no using here ???
{
// use MyFont3
} // compiler will call Dispose on MyFont3

}
}

Regards,
Cybertof.

推荐答案

Cyber​​tof,如果你是在这种情况下使用块是ca如你所说,调用

Dispose方法。如果你没有在你的第二个代码部分中使用using语句

,你只需要自己调用Dispose。

在这种情况下你应该包装Font创建和尝试使用...

finally ...阻止确保你的Font被正确处置。我会

推荐使用声明,因为它会确保你的

对象得到清理。这回答了你的问题吗?


-

Greg Ewing [MVP]
http://www.claritycon.com/

" Cyber​​tof" < CY **************** @ gmx.net>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...
Cybertof, if you the using block in this case is taking care of calling the
Dispose method as you have indicated. If you didn''t use the using statement
in your second code section you would just have to call Dispose yourself.
In that case you should wrap the Font creation and use in a try...
finally... block to ensure that your Font is Disposed properly. I would
recommend the using statement though since it takes care of making sure your
object is cleaned up. Does that answer your question?

--
Greg Ewing [MVP]
http://www.claritycon.com/

"Cybertof" <cy****************@gmx.net> wrote in message
news:MP************************@msnews.microsoft.c om...
嗨!

我想知道使用''using''语句。
在一个过程中,我应该在一个使用中声明所有实例变量
条款?

在下面的代码中,如果MyFont& MyFont2将在Main()的主体中声明,而不是在使用中(甚至在使用中没有引用)?会有什么东西要求处理它们吗?

我以为垃圾收集器本身就是在做这一切...

****** *****
代码示例
***********
使用System.Drawing;
class a
{
public static void Main()
{
使用(Font MyFont = new Font(" Arial",10.0f),MyFont2 = new Font
(" Arial",10.0f))
//使用MyFont和MyFont2
} //编译器将在MyFont和MyFont2上调用Dispose


字体MyFont3 =新字体(" Arial",10.0 f);
使用(MyFont3)//如果没有在这里使用会发生什么???
//使用MyFont3
} //编译器会在MyFont3上调用Dispose />
}
}

问候,
Cyber​​tof。
Hi !

I''m wondering about the use of the ''using'' statement.
In a procedure, should i declare all instance variables in a using
clause ?

In the below code, what would happen if MyFont & MyFont2 would be
declared in the body of the Main() and not inside the using (even not
referenced in the using) ? Would be there something to call to dispose
them ?

I thought the garbage collector was doing all this by itself...

***********
code sample
***********
using System.Drawing;
class a
{
public static void Main()
{
using (Font MyFont = new Font("Arial", 10.0f), MyFont2 = new Font
("Arial", 10.0f))
{
// use MyFont and MyFont2
} // compiler will call Dispose on MyFont and MyFont2

Font MyFont3 = new Font("Arial", 10.0f);
using (MyFont3) // WHAT HAPPEN IF no using here ???
{
// use MyFont3
} // compiler will call Dispose on MyFont3

}
}

Regards,
Cybertof.



是的,但我有另一个问题:

如果我根本不使用''使用''会发生什么? ollowing code:


public void Test()

{

MessageBox.Show(" Before");

字体MyFont =新字体(" Arial",10.0f);

MessageBox.Show(" After");

}


MyFont永远不会发布?

这是否意味着如果你在一个

过程体中的类中实例化一个对象,那么该对象是从来没有清理过吗?

我以为垃圾收集器正在这样做...

所以我应该调用.Dispose()我将从系统创建的每个实例上

班级或自制班级?

问候,

Cyber​​tof。

文章< ei ******** ******@TK2MSFTNGP12.phx.gbl>,
gewing@_NO_SPAM_claritycon.com 说...
Yes, but so i have another question :
What happens if i don''t use ''using'' at all in the following code :

public void Test()
{
MessageBox.Show("Before");
Font MyFont = new Font("Arial", 10.0f);
MessageBox.Show("After");
}

MyFont is never released ?
Does it mean that if you instanciate an object from a class in a
procedure body, the object is never cleaned ?
I thought the garbage collector was doing it...
So should i call .Dispose() on every instance i would create from system
classes or homemade classes ?
Regards,
Cybertof.
In article <ei**************@TK2MSFTNGP12.phx.gbl>,
gewing@_NO_SPAM_claritycon.com says...
Cyber​​tof,如果您在这种情况下使用块正在处理如您所指示的那样调用
Dispose方法。如果您没有在第二个代码部分中使用using语句
,则只需要自己调用Dispose。
在这种情况下,您应该包装Font创建并在try中使用...
finally ...阻止以确保您的Font正确处置。我会推荐使用using语句,因为它会确保你的
对象被清理干净。这是否回答了你的问题?
Cybertof, if you the using block in this case is taking care of calling the
Dispose method as you have indicated. If you didn''t use the using statement
in your second code section you would just have to call Dispose yourself.
In that case you should wrap the Font creation and use in a try...
finally... block to ensure that your Font is Disposed properly. I would
recommend the using statement though since it takes care of making sure your
object is cleaned up. Does that answer your question?



MyFont将在GC的闲暇时间发布。可能当程序

关闭时。你不会得到泄漏,例如你引用性能将会很重要。


如果你做了大量的绘图或使用很多字体或只是想成为一个好的

程序员在所有支持IDisposable的对象上调用Dispose()。


由对象调用的对象终结器GC将(*应该*)调用

收集时处理,但除非你专门调用GC.Collect,否则你无法控制这个过程。


就个人而言,我从不使用{....}我猜它是一种风格选择。


-

鲍勃鲍威尔[MVP]

C#,System.Drawing


九月的Well Formed版本现已推出。
http://www.bobpowell.net/currentissue.htm


用GDI + FAQ回答那些GDI +问题
http://www.bobpowell.net/gdiplus_faq.htm


" Cyber​​tof" < CY **************** @ gmx.net>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...
MyFont will be released at the leisure of the GC. Possibly when the program
closes. You won''t get a leak and for the example you cite performance will
not be important.

If you do a lot of drawing or use a lot of fonts or just want to be a good
programmer call Dispose() on all objects that support IDisposable.

The object finalizer which is called by the GC will ( *should* ) call
Dispose when it gets collected but unless you call GC.Collect specifically,
you have no control over the process.

Personally, I never use using{ .... } I guess its a style choice.

--
Bob Powell [MVP]
C#, System.Drawing

September''s edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

"Cybertof" <cy****************@gmx.net> wrote in message
news:MP************************@msnews.microsoft.c om...
是的,但我有另一个问题:
如果我在以下代码中根本不使用''使用',会发生什么:

public void测试()
{MessageBox.Show(" Before");
Font MyFont = new Font(" Arial",10.0f);
MessageBox.Show(" ;之后);
}

MyFont永远不会被释放?
这是否意味着如果你从一个类中的一个类实例化一个对象,那么该对象就是从来没有清理过?
我以为垃圾收集器正在这样做...
我应该调用.Dispose()我会从系统类或自制类创建的每个实例吗? />
问候,
Cyber​​tof。

文章< ei ************** @ TK2MSFTNGP12.phx.gbl>,
gewing@_NO_SPAM_claritycon.com 说...
Yes, but so i have another question :
What happens if i don''t use ''using'' at all in the following code :

public void Test()
{
MessageBox.Show("Before");
Font MyFont = new Font("Arial", 10.0f);
MessageBox.Show("After");
}

MyFont is never released ?
Does it mean that if you instanciate an object from a class in a
procedure body, the object is never cleaned ?
I thought the garbage collector was doing it...
So should i call .Dispose() on every instance i would create from system
classes or homemade classes ?
Regards,
Cybertof.
In article <ei**************@TK2MSFTNGP12.phx.gbl>,
gewing@_NO_SPAM_claritycon.com says...
C ybertof,如果你在这种情况下使用块正在处理如你所指示的那样调用
Dispose方法。如果你没有在你的第二个代码部分使用
语句,你只需要自己调用Dispose
。在这种情况下,您应该包装字体创建并在尝试中使用...
finally ...阻止以确保您的字体正确处置。我会推荐使用using语句,因为它会确保你的对象被清理掉
。这是否回答了你的问题?
Cybertof, if you the using block in this case is taking care of calling the Dispose method as you have indicated. If you didn''t use the using statement in your second code section you would just have to call Dispose yourself. In that case you should wrap the Font creation and use in a try...
finally... block to ensure that your Font is Disposed properly. I would
recommend the using statement though since it takes care of making sure your object is cleaned up. Does that answer your question?



这篇关于我什么时候应该使用'使用'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆