Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Using... before or after the Namespace

Using... before or after the Namespace

Scheduled Pinned Locked Moved C#
performance
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    v3ct0r
    wrote on last edited by
    #1

    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.

    W C M 3 Replies Last reply
    0
    • V v3ct0r

      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.

      W Offline
      W Offline
      Werdna
      wrote on last edited by
      #2

      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. }

      V M D 3 Replies Last reply
      0
      • W Werdna

        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. }

        V Offline
        V Offline
        v3ct0r
        wrote on last edited by
        #3

        Sweet thanx. I don't know why I've adopted putting them inside the namespace, I was happy with them outside and then suddenly I decided to put them in for no apparent reason:rolleyes:.

        1 Reply Last reply
        0
        • V v3ct0r

          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.

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          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 like System.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

          1 Reply Last reply
          0
          • V v3ct0r

            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.

            M Offline
            M Offline
            Michael Flanakin
            wrote on last edited by
            #5

            As you've seen from everyone else's comments, you can put your using statements in or outside of the namespace 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 the using 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

            V 1 Reply Last reply
            0
            • W Werdna

              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. }

              M Offline
              M Offline
              Michael Flanakin
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • M Michael Flanakin

                As you've seen from everyone else's comments, you can put your using statements in or outside of the namespace 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 the using 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

                V Offline
                V Offline
                v3ct0r
                wrote on last edited by
                #7

                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.

                M 1 Reply Last reply
                0
                • V v3ct0r

                  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.

                  M Offline
                  M Offline
                  Michael Flanakin
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • W Werdna

                    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. }

                    D Offline
                    D Offline
                    Duarte Cunha Leao
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups