Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Using(.....){...} mean [duplicate]

Possible Duplicates:
Using the using statment in c#
What is the C# Using block and why should I use it?

Just wondering what this means? I've seen lots of tutorials online that have the syntax:

using (SqlCeCommand cmd2 = new SqlCeCommand("SELECT city FROM cities JOIN states ON states.id=cities.state WHERE states.state='" + read.GetString(0) + "'", con))
{
  SqlCeDataReader readCities = cmd2.ExecuteReader();
  while (readCities.Read())
  {
    parent.Nodes.Add(readCities.GetString(0));
  }
}

Why is it used? I tried searching Google, but it comes up with the 'using' keyword which is used for including dll's and other files.

like image 879
James Jeffery Avatar asked Jan 25 '10 23:01

James Jeffery


People also ask

What is that term that means a duplicate?

Some common synonyms of duplicate are copy, facsimile, replica, and reproduction. While all these words mean "a thing made to closely resemble another," duplicate implies a double or counterpart exactly corresponding to another thing.

What does it mean to do something in duplicate?

Definition of in duplicate 1 : so that there are two copies We were required to fill out the paperwork in duplicate. 2 : with an exact copy Please send the contract in duplicate.

Is duplicate copy is same as original?

Duplicate creates a copy of an item in the same location as the original. Copying (or “Copy To”) creates a copy of an item in a different location that you specify.

What is the purpose of using duplicate?

Duplicate is used to describe things that have been made as an exact copy of other things, usually in order to serve the same purpose.


2 Answers

The using statement

using(var disposableObject = new object_that_implements_IDisposable()) { ... }

is syntactic sugar for code similar to following:

var disposableObject = new object_that_implements_IDisposable()
try
{
    ...
}
finally
{
    if(disposableObject != null)
    {
        ((IDisposable)your_object).Dispose();
    }
}

This is only applicable for classes that implement IDisposable. It is helpful for cleaning up code where you have objects that take, for example, system resources (file handles, database connections, sockets, etc.) that need to be cleaned up after you are done to free the resource for the rest of the system.

In theory, you could leave out the .Dispose() call, but then you would have to wait for the Garbage Collector to free the kept resources. The GC is awesome at knowing when to free objects to reclaim their memory usage, but it has no idea that it needs to free objects to have them give up other system resources. Thus, these critical resources might not be given up until after the GC decides it needs the memory used by the owner. Thus, you definitely want to dispose of your objects when you are done with them (if they are disposable)!

As to why you'd use using over try/finally, it is purely a coder's preference. I prefer using because you can cascade them:

using(var a = new class())
using(var b = new class())
using(var c = new class())
using(var d = new class())
{
    ...
}

You'd need quite a few more lines of code to do that with try/finally.

using has additional advantages as well. For example, whereas calling x.Dispose directly might throw a NullReferenceException if x is null, using(x) will not.

See also:

  • http://blogs.msdn.com/b/shawnfa/archive/2004/03/29/101466.aspx
  • Using the using statment in c#
  • What is the C# Using block and why should I use it?
  • http://msdn.microsoft.com/en-us/library/yh598w02%28VS.80%29.aspx
like image 159
David Pfeffer Avatar answered Sep 21 '22 15:09

David Pfeffer


The using just instructs the compiler to write code that will call the Dispose method on the variable you're using. Only types that implement 'IDisposable' can be used with using statements.

In your example, cmd2 will be disposed when the code in the {} finishes.

like image 24
Esteban Araya Avatar answered Sep 18 '22 15:09

Esteban Araya