返回使用创建的对象 [英] Return an object created by USING

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

问题描述

我在使用,并返回该对象的功能return.Will这个事业之前,我尝试在其他功能使用返回的值状物体的任何问题将得到处理的一部分创建一个对象(OBJ下)?

 使用(为MyObject OBJ =新MyObject来())
{



返回OBJ;
}


解决方案

这会不会导致类似对象中的任何问题都会进行处理之前,我尝试在其他功能使用返回的值?






您能解释一下你想要做什么这里?此代码没有任何意义。整个使用的的的是,你的使用的对象的这里只的,然后自动摆脱其稀缺的非托管资源,使其无法使用。有可能是一个更好的方法去做你想做的事情。


I am creating an object(obj below) in using and return that object as part of the function return.Will this cause any problem like object will be disposed before I try to use returned value in another function ?

using (MyObject obj = new MyObject())
{
   .
   .
   .
   return obj;
}

解决方案

Will this cause any problem like object will be disposed before I try to use returned value in another function?

Yes.

Can you explain what you're trying to do here? This code doesn't make any sense. The whole point of "using" is that you are using the object here only and then automatically getting rid of its scarce unmanaged resources, rendering it unusable. There is probably a better way to do what you want to do.

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

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