What does the using System; line do ?
-
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
-
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
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? -
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?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
-
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
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
-
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
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
-
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
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
-
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
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 ausing
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 theusing
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
-
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
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.