*使用*块,我可以有不同类型的对象在C#? [英] Can i have different type of objects in a C# *using* block?

查看:101
本文介绍了*使用*块,我可以有不同类型的对象在C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用(字体font3 =新的字体(宋体,10.0f),
font4 =新的字体(宋体,10.0f))
{
//使用font3和font4。
}



我知道,相同类型的多个对象可以在使用使用条款。



斜面我使用不同类型的对象的子句中?



嗯,我尝试,但尽管他们不同的名称和不同的对象,他们的行为同样=有相同的方法集



是否有任何其他方式与不同类型的使用使用类?



如果不是,那么就是用它最合适的方式?


解决方案

 宋体使用(字体F2 =新字体,10.0f))
(宋体,10.0f))使用
(流S =新的MemoryStream())
{

}

像这样?


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?

解决方案

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

}

Like so?

这篇关于*使用*块,我可以有不同类型的对象在C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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