Using "using"

In C#, when instantiating objects, it seems like there are two choices; 1) fully specify the hierarchy (i.e. System.Data.SqlClient.SqlConnection) or 2) placing a using statement (i.e using System.Data.SqlClient;) so you can using the more direct reference (in this case "SqlConnection").

What are the factors involved with deciding to use a using statement versus fully referencing everything in the code?

0
stevenjamesfrank65
4/1/2008 3:24:59 PM
asp.net.getting-started 91979 articles. 3 followers. Follow

8 Replies
672 Views

Similar Articles

[PageSpeed] 30

using is just a shortcut so that you don't have to write the entire namespace out for each instantiation.  The drawback is that someone reading your code may not immediately know the namespace to which a class belongs.


Community Coder
0
jamesqua
4/1/2008 3:35:18 PM

With 'using' if the object you are using implements IDisposable...the object is automatically disposed of at the end of the using statement....if you do not use 'using' you need to explicitly dispose of the object like do a Conn.Dispose()....or whatever your object name is.

Ryan


Flames are for BBQ'ing.
http://weblogs.asp.net/RyanSmith
0
RyanSJedi
4/1/2008 3:36:58 PM

Personally,

I use using <namespace> at the top of the page so that I don't have to type the full name again and again, its just a shortcut method. There is no performance gain or loss (ASAIK) of using either. Sometimes, when two namespaces have classes with same names, then there can be a situation of ambiguity. At that time, use the fully qualified name in the code (if you are using VS, VWB etc, the editor will show you the ambiguity).


Thanks,
Max
Let Me Google That For You!
0
bullpit
4/1/2008 3:37:28 PM

RyanSJedi:

With 'using' if the object you are using implements IDisposable...the object is automatically disposed of at the end of the using statement....if you do not use 'using' you need to explicitly dispose of the object like do a Conn.Dispose()....or whatever your object name is.

Ryan

I think the OP is just talking about "using <Namespace>"


Thanks,
Max
Let Me Google That For You!
0
bullpit
4/1/2008 3:38:25 PM

It just saves your time specifying the entire namespace once at the top using using rather than specifying it everytime. 


Thanks,
Karan

~ Remember To Mark The Post(s) That Helped You As The ANSWER ~
0
karan
4/1/2008 3:40:40 PM

Also, as RyanSJedi is pointing out, there are two types of usings  in c#.

The using statement is to dispose of objects http://msdn2.microsoft.com/en-us/library/yh598w02(VS.80).aspx

The using directive is for namespace declaration http://msdn2.microsoft.com/en-us/library/sf0df423(VS.71).aspx


Community Coder
0
jamesqua
4/1/2008 3:41:31 PM

You're right...I saw using and SQLConnection and went there...my bad.

Then my answer was a bonus. Smile

Ryan


Flames are for BBQ'ing.
http://weblogs.asp.net/RyanSmith
0
RyanSJedi
4/1/2008 3:44:46 PM

RyanSJedi:
Then my answer was a bonus. Smile

Yup...

 


Thanks,
Max
Let Me Google That For You!
0
bullpit
4/1/2008 3:46:14 PM
Reply: