我可以在 C# *using* 块中拥有不同类型的对象吗?

发布于 2024-09-06 17:23:34 字数 365 浏览 5 评论 0原文

using (Font font3 = new Font("Arial", 10.0f), 
           font4 = new Font("Arial", 10.0f))
{
    // Use font3 and font4.
}

我知道可以在 using 子句中使用相同类型的多个对象。

我不能在 using 子句中使用不同类型的对象吗?

好吧,我尝试过,但尽管它们是不同的名称和不同的对象,但它们的行为相同=具有相同的方法集

是否有其他方法可以使用不同类型的 using 类?

如果没有,最合适的使用方式是什么?

using (Font font3 = new Font("Arial", 10.0f), 
           font4 = new Font("Arial", 10.0f))
{
    // Use font3 and font4.
}

I know that multiple objects of same type can be used inside a using clause.

Cant i use different types of objects inside the using clause?

Well i tried but although they were different names and different objects, they acted the same = had the same set of methods

Is there any other way to use the using class with different types?

If not, what is the most appropriate way to use it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

墟烟 2024-09-13 17:23:34
using(Font f1 = new Font("Arial",10.0f))
using (Font f2 = new Font("Arial", 10.0f))
using (Stream s = new MemoryStream())
{

}

像这样吗?

using(Font f1 = new Font("Arial",10.0f))
using (Font f2 = new Font("Arial", 10.0f))
using (Stream s = new MemoryStream())
{

}

Like so?

忘年祭陌 2024-09-13 17:23:34

不,您不能这样做,但您可以嵌套 using 块。

using (Font font3 = new Font("Arial", 10.0f))
{ 
    using (Font font4 = new Font("Arial", 10.0f))
    {
        // Use font3 and font4.
    }
}

或者正如其他人所说,但由于可读性,我不推荐这样做。

using(Font font3 = new Font("Arial", 10.0f))
using(Font font4 = new Font("Arial", 10.0f))
{
    // use font3 and font4
}

No you can't do it this way, but you can nest the using blocks.

using (Font font3 = new Font("Arial", 10.0f))
{ 
    using (Font font4 = new Font("Arial", 10.0f))
    {
        // Use font3 and font4.
    }
}

or as others said, but I'd not recommend it that way because of readability.

using(Font font3 = new Font("Arial", 10.0f))
using(Font font4 = new Font("Arial", 10.0f))
{
    // use font3 and font4
}
原野 2024-09-13 17:23:34

您可以堆叠 using 语句来完成此操作:

using(Font font3 = new Font("Arial", 10.0f))
using(Font font4 = new Font("Arial", 10.0f))
{
    // use font3 and font4
}

You can stack using statements to accomplish this:

using(Font font3 = new Font("Arial", 10.0f))
using(Font font4 = new Font("Arial", 10.0f))
{
    // use font3 and font4
}
梅倚清风 2024-09-13 17:23:34

using 语句的目的是保证通过调用IDisposable 接口提供的Dispose 方法来显式释放所获取的资源。该规范不允许您在单个 using 语句中获取不同类型的资源,但记住第一句话,您可以根据编译器编写这段完全有效的代码。

using (IDisposable d1 = new Font("Arial", 10.0f),
    d2 = new Font("Arial", 10.0f), 
    d3 = new MemoryStream())
{
    var stream1 = (MemoryStream)d3;
    stream1.WriteByte(0x30);
}

但是,我不推荐这样做,并且我认为这是滥用行为,因此这个答案只是表明您可以绕过它,但您可能不应该这样做。

The using statement purpose is to guarantee that the acquired resources are explicitly disposed by a call to Dispose method provided by the IDisposable interface. The specification does not allow you to acquire resources of different types inside a single using statement but having the first sentence in mind you can write this perfectly valid code in terms of the compiler.

using (IDisposable d1 = new Font("Arial", 10.0f),
    d2 = new Font("Arial", 10.0f), 
    d3 = new MemoryStream())
{
    var stream1 = (MemoryStream)d3;
    stream1.WriteByte(0x30);
}

However, I'm not recommending this and I consider it abusive, so this answer is just to state that you can hack around it but you probably should not.

歌入人心 2024-09-13 17:23:34

您可以用逗号分隔相同类型的项目 - 好吧,我所知道的是编译器不会抱怨。您还可以堆叠不同类型的 using () 语句(使用一组括号 {})。

http://adamhouldsworth.blogspot.com/2010/02/你不知道的事情.html

You can comma-delimit items of the same type - well, all I know is the compiler doesn't complain. You can also stack using () statements (use one set of brackets {}) of different types.

http://adamhouldsworth.blogspot.com/2010/02/things-you-dont-know.html

弱骨蛰伏 2024-09-13 17:23:34

您只能在每个 using 块中初始化单一类型的对象。但是,您可以根据需要嵌套它们:

using (Font font3 = new Font("Arial", 10.0f))
{
    using (Brush b4 = new Brush())
    {

    }
}

You can only have a single type of object initialized in each using block. You can nest those as much as you want, however:

using (Font font3 = new Font("Arial", 10.0f))
{
    using (Brush b4 = new Brush())
    {

    }
}
—━☆沉默づ 2024-09-13 17:23:34

您可以嵌套它们:

using (Font font3 = new Font("Arial", 10.0f))
using (font4 = new Font("Arial", 10.0f))
{
    // Use font3 and font4.
}

它们应该以相反的顺序放置(首先是 font4)。

编辑:

这与:

using (Font font3 = new Font("Arial", 10.0f))
{
    using (font4 = new Font("Arial", 10.0f))
    {
        // Use font3 and font4.
    }
}

You can nest them:

using (Font font3 = new Font("Arial", 10.0f))
using (font4 = new Font("Arial", 10.0f))
{
    // Use font3 and font4.
}

They should dispose in reverse order (font4 first).

EDIT:

This is exactly the same as:

using (Font font3 = new Font("Arial", 10.0f))
{
    using (font4 = new Font("Arial", 10.0f))
    {
        // Use font3 and font4.
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文