使用创建对象使用 [英] Create object use using

查看:100
本文介绍了使用创建对象使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用(Idata Data = GetData()){} 



使用这样创建对象可以帮助自动处理对象,

有没有办法一次创建这样的多个对象?



我尝试了什么:



使用(Idata Data = GetData(),Idata Data1 = GetData1()){} 
using(Idata Data = GetData(); Idata Data1 = GetData1()){}





这不起作用。

解决方案

对象在不使用时自动处理,即垃圾收集器的工作。



使用的工作是处理具有非托管部分的对象,例如对底层操作系统句柄的引用,这些操作占用非托管内存,并且可以在之外使用块时使用。



一般来说你不需要使用为你自己的类,需要它的对象通常是sp在他们的文档中加以验证。


一个接一个:

 使用(Idata Data = GetData())
{
使用(Idata Data1 = GetData1())
{
// 您的代码
}
}


using (Idata Data = GetData())  {}


create object use using like this can help dispose object automatically,
are there any way to create multiple object like this at one time ?

What I have tried:

using (Idata Data = GetData(),   Idata Data1 = GetData1()  )  {}
using (Idata Data = GetData(); Idata Data1 = GetData1()  )  {}



this doesn't work.

解决方案

Objects are automatically disposed of when not used, that is the job of the garbage collector.

usings job is for disposing objects which have non managed parts like references to underlying OS handles which take up non managed memory and can be released when not used outside the using block.

Generally you don't need using for your own classes, the objects which need it are usually specified in the documentation for them.


One after the other:

using (Idata Data = GetData())
{
    using (Idata Data1 = GetData1())
    {
        // your code here
    }
}


这篇关于使用创建对象使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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