Using... before or after the Namespace
-
This is a bit confusing, I've seen the using keyword used both before and after the Namespace. using System; using System.Data; namespace MyCompany { public MyClass {} } as well as namespace MyCompany { using System; using System.Data; public MyClass {} } I've used both and cannot find a difference in actual performance. Is this something that is purely for personal choice or is there some sore of advantage to using one over the other.
-
This is a bit confusing, I've seen the using keyword used both before and after the Namespace. using System; using System.Data; namespace MyCompany { public MyClass {} } as well as namespace MyCompany { using System; using System.Data; public MyClass {} } I've used both and cannot find a difference in actual performance. Is this something that is purely for personal choice or is there some sore of advantage to using one over the other.
-
it makes not different most of the time. however if you have more than one namespace in your file it makes different. namespace MyCompany { using System.Data; } namespace OtherCompany { ... can't use System.Data directly here. }
-
This is a bit confusing, I've seen the using keyword used both before and after the Namespace. using System; using System.Data; namespace MyCompany { public MyClass {} } as well as namespace MyCompany { using System; using System.Data; public MyClass {} } I've used both and cannot find a difference in actual performance. Is this something that is purely for personal choice or is there some sore of advantage to using one over the other.
v3ct0r wrote: I've used both and cannot find a difference in actual performance Its not a performance thing.
using
is mearly telling the compiler that you will be using stuff from other namespaces implicitly (i.e. without having to give a fully qualified name likeSystem.Data.SqlClient.SqlConnection
) If there is any performance gains to be had it will only be at compile time and not during runtime. The other reply you got explains the rest, so I won't repeat it.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
-
This is a bit confusing, I've seen the using keyword used both before and after the Namespace. using System; using System.Data; namespace MyCompany { public MyClass {} } as well as namespace MyCompany { using System; using System.Data; public MyClass {} } I've used both and cannot find a difference in actual performance. Is this something that is purely for personal choice or is there some sore of advantage to using one over the other.
As you've seen from everyone else's comments, you can put your
using
statements in or outside of thenamespace
declaration. Now, to caveat that, I'd like to highly suggest that you put them outside of it. Most code that you see should have theusing
statements at the top of the class file, so I'd just stick with this standard. As if industry use wasn't enough, it's actually Microsoft's suggested method. Flexibility is nice, but there are times when you should choose standards over that flexibility. I think that this is one of those times. Michael Flanakin Web Log -
it makes not different most of the time. however if you have more than one namespace in your file it makes different. namespace MyCompany { using System.Data; } namespace OtherCompany { ... can't use System.Data directly here. }
Not that you are or aren't suggesting the practice of having more than one namespace in a single class file, but I'd have to say...DON'T!!! You should never have more than one namespace within a class file. Honestly, you should never have more than one class in a class file. This is why it's referred to as a class file. Of course, this is just an ideal. But, sticking to it will lessen problems in the future...trust me. Michael Flanakin Web Log
-
As you've seen from everyone else's comments, you can put your
using
statements in or outside of thenamespace
declaration. Now, to caveat that, I'd like to highly suggest that you put them outside of it. Most code that you see should have theusing
statements at the top of the class file, so I'd just stick with this standard. As if industry use wasn't enough, it's actually Microsoft's suggested method. Flexibility is nice, but there are times when you should choose standards over that flexibility. I think that this is one of those times. Michael Flanakin Web LogThank you. I agree with you, I'll be pulling my using statements to the top of the page. Funny thing however, Microsoft had a Direct X 9 sample that had the using statments inside the namespace, this is actually where I picked it up from. Perhaps there was a specific purpose that I overlooked. Anyway, thanks everyone else as well.
-
Thank you. I agree with you, I'll be pulling my using statements to the top of the page. Funny thing however, Microsoft had a Direct X 9 sample that had the using statments inside the namespace, this is actually where I picked it up from. Perhaps there was a specific purpose that I overlooked. Anyway, thanks everyone else as well.
I'm sure this is a case of every developer has their way. What "Microsoft" suggests isn't always the same as what their developers do. Michael Flanakin Web Log
-
it makes not different most of the time. however if you have more than one namespace in your file it makes different. namespace MyCompany { using System.Data; } namespace OtherCompany { ... can't use System.Data directly here. }
Plesase refer to this as well http://stackoverflow.com/questions/125319/should-usings-be-inside-or-outside-the-namespace
This could be heaven for everyone