“使用”的用法在.NET中
在下面的示例中,返回的pen
是否会被销毁(处置)?
' VB'
Public Function GetPen() As System.Drawing.Pen
Using pen As New System.Drawing.Pen(_Color, _Width)
pen.DashStyle = _DashStyle
Return pen
End Using
End Function
// C#
public System.Drawing.Pen GetPen()
{
using (System.Drawing.Pen pen = new System.Drawing.Pen(_Color, _Width))
{
pen.DashStyle = _DashStyle;
return pen;
}
}
[编辑]
再精确一点... Pen 对象是通过引用发送给 GetPen 的调用者还是像结构一样“克隆”?我知道,这是一个类,但对于 GDI 对象,我永远不确定...
当外部方法将处理通过 GetPen()
获得的 pen
吗?
In the example below, will the returned pen
be destroyed(disposed) or not?
' VB'
Public Function GetPen() As System.Drawing.Pen
Using pen As New System.Drawing.Pen(_Color, _Width)
pen.DashStyle = _DashStyle
Return pen
End Using
End Function
// C#
public System.Drawing.Pen GetPen()
{
using (System.Drawing.Pen pen = new System.Drawing.Pen(_Color, _Width))
{
pen.DashStyle = _DashStyle;
return pen;
}
}
[EDIT]
Just one more precision... Is the Pen object sent to the caller of GetPen by reference or 'cloned' like a structure? I know, this is a class, but with GDI objects I am never sure...
Will it be destroyed(disposed) the pen
created in GetPen()
when the external method will Dispose its pen
obtained with GetPen()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
是的,它将被处置。然后你会以废弃的状态返回它,所以它不会有任何好处。如果您想使用这样的工厂方法来返回
Pen
实例,您需要在方法外部自行处理它,而不是使用using
块在这样的方法内。Yes, it will be disposed. You are then returning it in a disposed state, so it won't be any good for anything. If you want to use a factory method like this to return a
Pen
instance, you'll need to deal with disposing it yourself externally to the method, not using ausing
block within the method like this.Pen 不一定会被垃圾回收,但会调用其 Dispose() 方法。
Pen will not necessarily be garbage collected, but it will have it's Dispose() method called.
是的,笔将被丢弃。但这确实是一个坏主意;您归还一支已经废弃的笔!
您要做的就是从 GetPen 中删除 using 语句。 GetPen 调用者应该使用Using 语句:
或者在C# 中:
[EDIT]
是的,返回的是调用方法的引用,而不是副本。这就是为什么如果你在 GetPen 中释放了笔,你就不能在调用方法中使用该笔;-)
因为 GetPen 和调用方法指向同一个 Pen 对象,所以你只需要在调用方法中调用 Dispose调用方法。
Yes, pen will be disposed. It's really a bad idea though; you return a pen that's already disposed!
What you want to do is to remove the Using statement from GetPen. The Using statement should be used by the GetPen callers:
Or in C#:
[EDIT]
Yes, a reference is returned to the calling method, not a copy. That's why if you dispose of the pen in GetPen, you can't use that pen in the calling method ;-)
Because of the fact that GetPen and the calling method point to the same Pen object, you just need to call Dispose in the calling method.
笔将在归还前被处理掉。它相当于
The pen will be disposed before returning. Its the equivalent of
是的,它将被处置。
Yes it will be disposed.
退货完成后,它将被自动处理。使用强制隐式使用 Dispose()
It will be automatically disposed when the return is done. Using forces the use of Dispose() implicitely
是。
返回的 Pen 对象将在调用者收到它之前被 Dispose。
在
Get()
风格的方法中,您不想处理对象的处理。允许调用者在使用完对象后处置该对象。我希望调用函数如下所示:Yes.
The returned Pen object will be Disposed before the caller receives it.
In
Get()
style methods, you don't want to deal with Disposing of objects. Allow the caller to Dispose of the object AFTER they are done with it. I would expect the calling function to look like this:您的代码需要采用不同的结构,因为您返回的对象随后会立即被处理。
然后使用以下方式调用它:
Your code needs to be structured differently because you're returning an object that's then immediately disposed of.
Then call it using:
您的
Pen
对象将在返回完成之前被处置,并且您将留下一个处于无效状态的笔对象。Your
Pen
object will be disposed before the return completes and you will be left with a pen object in an invalid state.是的。在代码的 C# 和 VB.net 版本中,.Dispose() 方法在函数返回之前调用。这与销毁它不同(该对象仍然存在),但对于所有实际目的,该对象都是无用的,因为使它变得有趣和有用的非托管资源现在被释放了。
在函数内构建 IDisposable 资源时使用的正确模式是仅返回它,并使用 using 声明来调用该函数,如下所示:
Yes. In both the C# and VB.net versions of your code, the .Dispose() method is called before the function returns. That is not the same thing as destroying it (the object still exists) but for all practical purposes the object is useless, because the unmanaged resource that made it interesting and useful is now released.
The correct pattern to use when building an IDisposable resource inside a function is to just return it, and use a using declaration to call the function, like this:
using 大致等于以下语句:
try
{
}
最后
{
}
using is equals to the following statement roughly:
try
{
}
finally
{
}