6

this is basically a tutorial question to ask since am a beginner I would like to what is a difference between the using statement we use at start of our C# code to include assembly and namespaces

like this:

using System.Web.Services; 

and when we write inside the code within the method or code. like this:

using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)) 

is there any difference or they both are same, any guidance would be helpful and appreciated.

7
  • 4
    Surprisingly difficult to find a duplicate for this question. Commented Aug 3, 2011 at 8:19
  • 1
    The first is the using directive allowing you to access types defined in a specific namespace without using the fully qualified names. The second is the using statement allowing you to dispose disposable objects easily. Commented Aug 3, 2011 at 8:20
  • Note the using namespace is a "using directive" rather than a statement. This makes a difference when you move from being a beginner and are interested in the precise definition (eg. in the language specification). Commented Aug 3, 2011 at 8:21
  • @user64: Do they even look like they could be doing the same thing? Commented Aug 3, 2011 at 8:21
  • possible duplicate of use of "using" keyword in c# Commented Aug 3, 2011 at 8:22

5 Answers 5

13

The first (Using Directive) is to bring a namespace into scope.

This is for example, so you can write

StringBuilder MyStringBuilder = new StringBuilder(); 

rather than

System.Text.StringBuilder MyStringBuilder = new System.Text.StringBuilder(); 

The second (Using Statement) one is for correctly using (creating and disposing) an object implementing the IDisposable interface.

For example:

using (Font font1 = new Font("Arial", 10.0f)) { byte charset = font1.GdiCharSet; } 

Here, the Font type implements IDisposable because it uses unmanaged resources that need to be correctly disposed of when we are no-longer using the Font instance (font1).

Sign up to request clarification or add additional context in comments.

2 Comments

No need to thank, accepting an answer is thanks enough on this site. :)
using is also used to explicitly use a class or interface when there are more than one class or interface with the same name on different namespaces. Like mynamespace.TimeZone for instance, if you are using the namespace the framework's TimeZone and also mynamespace and you need to use a TimeZone object then you have to do using TimeZone = mynamespace.TimeZone
2

using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))

This using disposes the adapter object automatically once the control leaves the using block.

This is equivalent to the call

SqlDataAdapter adapter = new SqlDataAdapter(cmd) adapter.dispose(); 

See official documentation on this: http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.71).aspx

3 Comments

That's not directly equivalent. More equivalent would be the use of try/finally.
no, this is equivalent to: try { SqlDataAdapter adapter = new SqlDataAdapter(cmd); } finally { adapter.dispose(); }
And it's Dispose with a capital D.
0

They are about as different as you can get.

The first shows intent to use things within a namespace.

The second takes a reference to a disposable object and ensures it is disposed, no matter what happens (like implementing try/finally)

Comments

0

The first allows you to use types that are not defined in your code (tells the compiler where to find the code it needs to reference. REF: http://msdn.microsoft.com/en-us/library/sf0df423(v=VS.100).aspx

The second using makes sure that the memory is released upon the end of the code block, or in the case of an exception. REF: http://msdn.microsoft.com/en-us/library/yh598w02.aspx

Please see the links above for detailed documentation on each.

Comments

0

I'm sure someone will spend a great deal of time answering what amounts to a Google search but here are a couple of links to get you started.

The using Statement (C# Reference) ensures that Dispose is called even if an exception occurs while you are calling methods on the object.

To allow the use of types in a namespace so that you do not have to qualify the use of a type in that namespace use using Directive (C# Reference).

You may find that MSDN is a great resource to spend some time browsing.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.