“使用”的用法在.NET中

发布于 2024-08-18 05:56:59 字数 631 浏览 10 评论 0原文

在下面的示例中,返回的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

东京女 2024-08-25 05:56:59

是的,它将被处置。然后你会以废弃的状态返回它,所以它不会有任何好处。如果您想使用这样的工厂方法来返回 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 a using block within the method like this.

ㄟ。诗瑗 2024-08-25 05:56:59

Pen 不一定会被垃圾回收,但会调用其 Dispose() 方法。

Pen will not necessarily be garbage collected, but it will have it's Dispose() method called.

維他命╮ 2024-08-25 05:56:59

是的,笔将被丢弃。但这确实是一个坏主意;您归还一支已经废弃的笔!

您要做的就是从 GetPen 中删除 using 语句。 GetPen 调用者应该使用Using 语句:

Using pen As Pen = GetPen()
    ''// Draw with this pen
End Using

或者在C# 中:

using(Pen pen = GetPen())
{
    // Draw with this pen
}

[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:

Using pen As Pen = GetPen()
    ''// Draw with this pen
End Using

Or in C#:

using(Pen pen = GetPen())
{
    // Draw with this pen
}

[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.

梦开始←不甜 2024-08-25 05:56:59

笔将在归还前被处理掉。它相当于

public System.Drawing.Pen GetPen()
{
    try
    {
      System.Drawing.Pen pen = new System.Drawing.Pen(_Color, _Width);

      pen.DashStyle = _DashStyle;
    }
    finally
    {
      pen.Dispose();
    }
    return pen;

}

The pen will be disposed before returning. Its the equivalent of

public System.Drawing.Pen GetPen()
{
    try
    {
      System.Drawing.Pen pen = new System.Drawing.Pen(_Color, _Width);

      pen.DashStyle = _DashStyle;
    }
    finally
    {
      pen.Dispose();
    }
    return pen;

}
冬天旳寂寞 2024-08-25 05:56:59

是的,它将被处置。

Yes it will be disposed.

相守太难 2024-08-25 05:56:59

退货完成后,它将被自动处理。使用强制隐式使用 Dispose()

It will be automatically disposed when the return is done. Using forces the use of Dispose() implicitely

指尖上的星空 2024-08-25 05:56:59

是。

返回的 Pen 对象将在调用者收到它之前被 Dispose。

Get() 风格的方法中,您不想处理对象的处理。允许调用者在使用完对象后处置该对象。我希望调用函数如下所示:

using(Pen myPen = GetPen())
{
    // My code here.
}

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:

using(Pen myPen = GetPen())
{
    // My code here.
}
一抹苦笑 2024-08-25 05:56:59

您的代码需要采用不同的结构,因为您返回的对象随后会立即被处理。

public System.Drawing.Pen GetPen() 
{ 
    System.Drawing.Pen pen = new System.Drawing.Pen(_Color, _Width)
    { 
        DashStyle = _DashStyle; 
    } 
    return pen; 
} 

然后使用以下方式调用它:

using (System.Drawing.Pen pen = GetPen())
{
    //Do stuff with your pen...
}

Your code needs to be structured differently because you're returning an object that's then immediately disposed of.

public System.Drawing.Pen GetPen() 
{ 
    System.Drawing.Pen pen = new System.Drawing.Pen(_Color, _Width)
    { 
        DashStyle = _DashStyle; 
    } 
    return pen; 
} 

Then call it using:

using (System.Drawing.Pen pen = GetPen())
{
    //Do stuff with your pen...
}
瑾兮 2024-08-25 05:56:59

您的 Pen 对象将在返回完成之前被处置,并且您将留下一个处于无效状态的笔对象。

Your Pen object will be disposed before the return completes and you will be left with a pen object in an invalid state.

桃扇骨 2024-08-25 05:56:59

是的。在代码的 C# 和 VB.net 版本中,.Dispose() 方法在函数返回之前调用。这与销毁它不同(该对象仍然存在),但对于所有实际目的,该对象都是无用的,因为使它变得有趣和有用的非托管资源现在被释放了。

在函数内构建 IDisposable 资源时使用的正确模式是仅返回它,并使用 using 声明来调用该函数,如下所示:

public System.Drawing.Pen GetPen()
{
    var pen = new System.Drawing.Pen(_Color, _Width)) 
    pen.DashStyle = _DashStyle;
    return pen;
}

public void FunctionThatCallsGetPen()
{
    using (System.Drawing.Pen pen = GetPen())
    {
       //use pen here
    }
}

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:

public System.Drawing.Pen GetPen()
{
    var pen = new System.Drawing.Pen(_Color, _Width)) 
    pen.DashStyle = _DashStyle;
    return pen;
}

public void FunctionThatCallsGetPen()
{
    using (System.Drawing.Pen pen = GetPen())
    {
       //use pen here
    }
}
一人独醉 2024-08-25 05:56:59

using 大致等于以下语句:

try
{

     var something = new something() ;

}

最后
{

 if (something != null)
 { something.Dispose();}   

}

using is equals to the following statement roughly:

try
{

     var something = new something() ;

}

finally
{

 if (something != null)
 { something.Dispose();}   

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文