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. .NET (Core and Framework)
  4. What does the using System; line do ?

What does the using System; line do ?

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpquestioncollaborationperformancediscussion
8 Posts 4 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.
  • S Offline
    S Offline
    S 53K S
    wrote on last edited by
    #1

    Hi Guys, I have a small big question. What does the

    using System

    using System.Data

    actually do when a program is executing? To the best of my knowledge it loads a DLL related to those namespaces into the memory and then accesses the methods,but recently I came to know that <pre lang="c#">using</pre> tells the system to use the namespace and access the methods,but how is it possible to use the namespace and access the methods without actually loading the DLL into program execution memory?This recently came up in a discussion with a team member and he shattered my belief in .NET

    using

    :(

    Sastry

    T R 2 Replies Last reply
    0
    • S S 53K S

      Hi Guys, I have a small big question. What does the

      using System

      using System.Data

      actually do when a program is executing? To the best of my knowledge it loads a DLL related to those namespaces into the memory and then accesses the methods,but recently I came to know that <pre lang="c#">using</pre> tells the system to use the namespace and access the methods,but how is it possible to use the namespace and access the methods without actually loading the DLL into program execution memory?This recently came up in a discussion with a team member and he shattered my belief in .NET

      using

      :(

      Sastry

      T Offline
      T Offline
      Tony Richards
      wrote on last edited by
      #2

      The using keyword just makes your code a little simpler by allowing you to not specify namespaces when accessing classes. For example, if you have this class:

      namespace Tester
      {
      class Test
      {
      private void TestMethod()
      {
      // Do Something
      }
      }
      }

      Without a using statement, you would use it like this:

      var x = new Tester.Test();

      With a using statement, you can have:

      // At the top of your file, with the rest of the using statements
      using Tester;

      // In the function
      var x = new Test();

      Provided you don't have a class name collision (which namespaces are used to avoid in the first place), you end up with 'simpler' code (for a given value of simpler). However, they aren't essential. The DLL references are managed by the project file and then passed directly to the compiler, not using these statements. Incidently, this isn't a specifically C# thing, the using namespace construct in C++ works in the same way. Does that answer your question?

      S 1 Reply Last reply
      0
      • T Tony Richards

        The using keyword just makes your code a little simpler by allowing you to not specify namespaces when accessing classes. For example, if you have this class:

        namespace Tester
        {
        class Test
        {
        private void TestMethod()
        {
        // Do Something
        }
        }
        }

        Without a using statement, you would use it like this:

        var x = new Tester.Test();

        With a using statement, you can have:

        // At the top of your file, with the rest of the using statements
        using Tester;

        // In the function
        var x = new Test();

        Provided you don't have a class name collision (which namespaces are used to avoid in the first place), you end up with 'simpler' code (for a given value of simpler). However, they aren't essential. The DLL references are managed by the project file and then passed directly to the compiler, not using these statements. Incidently, this isn't a specifically C# thing, the using namespace construct in C++ works in the same way. Does that answer your question?

        S Offline
        S Offline
        S 53K S
        wrote on last edited by
        #3

        Got it,but does that load the DLL into the program memory while it is executing ? for example in DataAccess.dll I have:

        Using System.Data;

        namespace A
        {
        Class DataAccess
        {
        }
        }

        in business layer/application project I refer the DataAccess DLL and I say

        using A;
        using System;

        namespace B
        {
        Class c
        {
        DataAccess daObj = new DataAccess();
        }
        }

        does the above line load the dll System.Data into the execution of the program when class C instance is accessed as we are accessing DataAccess class which refers to System.Data?

        5@S3

        L 1 Reply Last reply
        0
        • S S 53K S

          Got it,but does that load the DLL into the program memory while it is executing ? for example in DataAccess.dll I have:

          Using System.Data;

          namespace A
          {
          Class DataAccess
          {
          }
          }

          in business layer/application project I refer the DataAccess DLL and I say

          using A;
          using System;

          namespace B
          {
          Class c
          {
          DataAccess daObj = new DataAccess();
          }
          }

          does the above line load the dll System.Data into the execution of the program when class C instance is accessed as we are accessing DataAccess class which refers to System.Data?

          5@S3

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Generally speaking a DLL will be loaded at the first method call whose code is part of the DLL. Thus in your example above it will be loaded at the call to the DataAccess() constructor. However, there is lots of optimisation in .NET so it is quite possible it gets loaded through some 'look ahead' code to speed up the program.

          Binding 100,000 items to a list box can be just silly regardless of what pattern you are following. Jeremy Likness

          S 2 Replies Last reply
          0
          • L Lost User

            Generally speaking a DLL will be loaded at the first method call whose code is part of the DLL. Thus in your example above it will be loaded at the call to the DataAccess() constructor. However, there is lots of optimisation in .NET so it is quite possible it gets loaded through some 'look ahead' code to speed up the program.

            Binding 100,000 items to a list box can be just silly regardless of what pattern you are following. Jeremy Likness

            S Offline
            S Offline
            S 53K S
            wrote on last edited by
            #5

            Ok,so that means even if refer to the name spaces in multiple project also the DLL gets loaded only once. For example referring to the Microsoft.Practices.EnterpriseLibrary.Data in business layer and data access layer will ensure that DLL having the namespace is loaded only once through out the entire program execution.

            Sastry

            L 1 Reply Last reply
            0
            • L Lost User

              Generally speaking a DLL will be loaded at the first method call whose code is part of the DLL. Thus in your example above it will be loaded at the call to the DataAccess() constructor. However, there is lots of optimisation in .NET so it is quite possible it gets loaded through some 'look ahead' code to speed up the program.

              Binding 100,000 items to a list box can be just silly regardless of what pattern you are following. Jeremy Likness

              S Offline
              S Offline
              S 53K S
              wrote on last edited by
              #6

              So referring the namespace in multiple project/class libraries does not affect the number of times a DLL into memory. Something like referring to

              Microsoft.Practices.EnterpriseLibrary.Data

              in BusinessLayer.DLL and DataAccess.DLL,will ensure that .NET loads the

              Microsoft.Practices.EnterpriseLibrary.Data.dll

              only once ?

              Sastry

              1 Reply Last reply
              0
              • S S 53K S

                Ok,so that means even if refer to the name spaces in multiple project also the DLL gets loaded only once. For example referring to the Microsoft.Practices.EnterpriseLibrary.Data in business layer and data access layer will ensure that DLL having the namespace is loaded only once through out the entire program execution.

                Sastry

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Sastry_kunapuli wrote:

                so that means even if refer to the name spaces in multiple project also the DLL gets loaded only once.

                This has nothing to do with the using statement, or with the number of projects that it is used in. A DLL is a code library that is used by an executable program, and is loaded into memory when some code in the executable makes a call to one of its methods. Having a using statement in your source code merely allows the compiler to satisfy references to methods or classes that are not defined within any of the source modules of the project. However, if no direct references are made from the program that contains the using statement, then the associated DLL will not be loaded.

                Binding 100,000 items to a list box can be just silly regardless of what pattern you are following. Jeremy Likness

                1 Reply Last reply
                0
                • S S 53K S

                  Hi Guys, I have a small big question. What does the

                  using System

                  using System.Data

                  actually do when a program is executing? To the best of my knowledge it loads a DLL related to those namespaces into the memory and then accesses the methods,but recently I came to know that <pre lang="c#">using</pre> tells the system to use the namespace and access the methods,but how is it possible to use the namespace and access the methods without actually loading the DLL into program execution memory?This recently came up in a discussion with a team member and he shattered my belief in .NET

                  using

                  :(

                  Sastry

                  R Offline
                  R Offline
                  Ron Beyer
                  wrote on last edited by
                  #8

                  It doesn't "do" anything, it simply instructs the compiler where to look (in referenced DLL's or in .NET language, "assemblies"). They aren't evaluated during runtime at all, it's only used in the preprocessor during the linking phase of the compilation process. Like the other poster said, if you don't use the "using" statement, you have to explicitly identify where to find certain functions in the included assemblies. It essentially just saves a lot of typing as stated. Oddly enough, the "using" statement can also be used to re-assign namespaces and class names. You can use it to "alias" namespaces. Another "using" keyword is to limit the scope of a variable and do automatic clean-up, like: using (SolidBrush sb = new SolidBrush(Color.Red)) { //Use the solid brush here } //Here the solid brush no longer exists, and the object is correctly disposed.

                  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