using statmenet.

  • Thread starter Thread starter Mohd Ebrahim
  • Start date Start date
M

Mohd Ebrahim

Hello,

1. Is it necessary to use 'using statment'
2. where i can use it

can i use something like this

private void cmdCopy_Click(object sender, EventArgs e)
{
using (frmyCopy obj = new frmCopy())
{
obj.ShowDialog();
}
}

and

// in the loop
if (customer.customerName== 'xyz')
{
//create new customer
using (customer obj = new customer)
{
obj.CustomerName='new name'

}
}

Regards,
 
Hi,

The using statement was designed to help with disposing disposable objects,
so it won't be 'forgotten'.

Instead of doing this :

using (frmyCopy obj = new frmCopy())
{
obj.ShowDialog();
}

You could come up with something like this :

frmyCopy obj = new frmCopy();
try
{
obj.ShowDialog();
}
finally
{
if (frmyCopy != null)
frmyCopy.Dispose();
}

However both should do the same, obviously the first one is more compact :)

Since this construct is aimed to help You dispose IDisposable instances, You
must
use it with an IDisposable(or stg that is implicitly convertible to that),
so
this :
using (customer obj = new customer())
{
obj.CustomerName='new name'
}

only compiles if this customer class is IDisposable.

Also You can check out this msdn page:
http://msdn.microsoft.com/en-us/library/yh598w02.aspx

Hope You find this useful.
-Zsolt
 
Hi,

Thanks for the feedback, I got your point and will implement IDisposble
interface to use with the classes.

Regards,
 
Thanks for the feedback, I got your point and will implement IDisposble
interface to use with the classes.

Don't implement IDisposable JUST so you can use a using statement; implement
it if you actually need to dispose of something.
 
Thanks for the feedback, I got your point and will implement IDisposble
interface to use with the classes.

Hi,

Hope i wasn't misleading You. You dont have to implement IDisposable in all
of your classes. Implement IDisposable only if it is appropriate for the
given class.

-Zsolt
 
Hi,

Ok thanks, and how can i see the IL code for using block in debug mode?

I want to evaluate the IL code

Regards,
 
Mohd Ebrahim said:
Hi,

Ok thanks, and how can i see the IL code for using block in debug mode?

..NET Reflector (created by Lutz Roeder, now maintained by I think Red Gate).
 
Back
Top