المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Is it OK to return reference from an USING (DIsposable object)



C# Programming
04-02-2010, 08:31 AM
Hi,

I am wondering if it is ok to use the following snippet ? I am suspecting that, as the object license will be disposed after this block, the caller of GetLicense method will get null object all the time, right ?

public virtual EncryptedLicense GetLicense(LicenseContext context, Assembly assembly, Type type)
{
..........
using (EncryptedLicense license = LoadLicense(context, null, licenseKey))
{
return license;
}
}
As my class EncryptedLicense is Disposable, my refactor tool (CodeRush and ReSharper) is asking me to place the statement EncryptedLicense license = LoadLicense(context, null, licenseKey) within using. But, I am getting worried if it is a correct practice or not. Would you please suggest!.