#include

  • Thread starter Thread starter Matt Wisner
  • Start date Start date
M

Matt Wisner

Hi,
I'm new to C# and was wondering what the best practice is for including text
in source files. Specifically, I have 2 "using x = y;" statements hard-coded
at the top of all my (13) source files. What is/are the Visual Studio/C#
Best-Practices for this scenario?
Is there a C# equivalent to the C/C++ #include directive?

TIA,
Matt
 
Matt Wisner said:
I'm new to C# and was wondering what the best practice is for including text
in source files. Specifically, I have 2 "using x = y;" statements hard-coded
at the top of all my (13) source files. What is/are the Visual Studio/C#
Best-Practices for this scenario?

Just include those using statements you need at the top of each file,
and grin and bear it. Don't forget that even if you had include files,
you'd only go down from 2 lines to 1.

As a sidenote, I'd personally recommend just "using Foo;" statements
rather than "using Foo=Bar;" for the sake of readability.
Is there a C# equivalent to the C/C++ #include directive?

No.
 
The placement of the #include statements in C/C++ is the same as the
using directives i.e., usually the using statements are placed at the
top of the files as in C/C++.

The using directive makes use of the classes within a namespace by using
the namespace. This can be understood as 'importing' a functionality
from another component just as the #include used to do. #include used to
provide methods like getch() from conio.h and when the conio.h was not
#include-d the compiler would generate an error. The same happens when
you do not use 'using System.Data.SqlClient' namespace importing
statement but wish to obtain the functionality of the SqlConnection
class.

But, where #include used to import inline the whole header file, 'using'
does not.

I hope the above few lines are helpful pointers for you.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
The using directive, used in the way described here, does not "import" any
functionality. It is merely a way to allow types to be used without using
their full names. E.g. "System.Xml.XmlDocument" can be shortened to
"XmlDocument" if "using System.Xml" is included at the top of the source
file.

The only way make types avaliable to a project is to add a reference to
them.

Steve James
 
Back
Top