Using Namespace

  • Thread starter Thread starter www.dir
  • Start date Start date
W

www.dir

Hi,

I am interested whether there is a significant differense if I put on
top of my class using CustomNameSpace or If I call methods in my code
by CustomNameSpace.MyMethod()

I am interested because I have to use only one method from some
namespace and I do not want to make my program heavy.

Thanks
 
No, there is no difference. Once it is compiled into IL (intermediate
language), the fully qualified name is used, so:

this:

using System.Data.SqlClient;

void SomeFunc() {
SqlCommand cmd = new SqlCommand();
}

is the same as:

void SomeFunc() {
System.Data.SqlClient.SqlCommand cmd
= new System.Data.SqlClient.SqlCommand();
}

....except that one is longer to type. A reason not to use the "using"
construction is to avoid namespace clashes. Importing System.Windows.Forms
and System.Web.Forms (?) would have lots of clashes (think TextBox).

Scott
 
Hi,

I am interested whether there is a significant differense if I put on
top of my class using CustomNameSpace or If I call methods in my code
by CustomNameSpace.MyMethod()

I am interested because I have to use only one method from some
namespace and I do not want to make my program heavy.

Thanks

They compile to exactly the same thing. Using in that respect is pure
syntactic sugar to save you some typing. This is very diffferent from

using(Foo f = new Foo() )
{
}

btw

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 
The name spaces:
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
have the same FILETIME type.
How can I select whitch one I want to use ?
FILETIME timespamp;
Gives a error in 2.0:
error CS0104: 'FILETIME' is an ambiguous reference between
'System.Runtime.InteropServices.FILETIME' and
'System.Runtime.InteropServices.ComTypes.FILETIME'
 
GTi said:
The name spaces:
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
have the same FILETIME type.
How can I select whitch one I want to use ?
FILETIME timespamp;
Gives a error in 2.0:
error CS0104: 'FILETIME' is an ambiguous reference between
'System.Runtime.InteropServices.FILETIME' and
'System.Runtime.InteropServices.ComTypes.FILETIME'

You either only import one namespace, you fully qualify it, or you create an
alias:

using InteropFILETIME = System.Runtime.InteropServices.FILETIME;

You could also use a full namespace alias:

using Interop = System.Runtime.InteropServices;

....
Interop.FILETIME filetime;

or if you are using 2.0 you can use the new namespace alias qualifier
operator(::):

Interop::FILETIME filetime;
 
I solved it (rare bug)

My name space name is:
namespace MyLib.System

So trying to spesify the correct type using:
System.Runtime.InteropServices.ComTypes.FILETIME timestamp
gives me an error saying that Runtime was unknown..

Renaming my namespace to:
namespace MyLib.Lib
solved the problem....

(And I was almost giving up C# for this!)
 
GTi said:
I solved it (rare bug)

My name space name is:
namespace MyLib.System

So trying to spesify the correct type using:
System.Runtime.InteropServices.ComTypes.FILETIME timestamp
gives me an error saying that Runtime was unknown..

Renaming my namespace to:
namespace MyLib.Lib
solved the problem....

(And I was almost giving up C# for this!)

Ahh. C# 2.0 offers a little get around for that, the global namespace alias:

global::System.Runtime.InteropServices.ComTypes.FILETIME timestamp;

should have worked
 
Back
Top