Jump to content

C# include file

- - - - -

  • Please log in to reply
7 replies to this topic

#1
asafe

asafe

    Programmer

  • Members
  • PipPipPipPip
  • 107 posts
What is C# equivalent of c++'s #include ? How do I reuse .cs files? What is a assembly reference and how do I make one?

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
1) There is no include statement in C#. Like C++; C# goes by namespaces. Well, there is an <include>, but it was meant for accessing XML comments in another file.

2) I assume you mean a reference to a COM/DLL assembly, you only need to include the assembly within your project,
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
asafe

asafe

    Programmer

  • Members
  • PipPipPipPip
  • 107 posts
And if I have a file and I want to use the code of such file in another?
What I mean is: C++ has files .h to reuse code, what does C# has ?

#4
dbug

dbug

    Programmer

  • Members
  • PipPipPipPip
  • 155 posts
You don't need to add any reference to other files. If both files are in the same namespace (and are compiled together), the compiler will find any needed declaration, whatever the file. If files are in different namespaces, then you must add a reference in one file to the namespace of the other file using the directive 'using <namespace>', but you never add references to specific files.

#5
asafe

asafe

    Programmer

  • Members
  • PipPipPipPip
  • 107 posts

dbug said:

You don't need to add any reference to other files. If both files are in the same namespace (and are compiled together), the compiler will find any needed declaration, whatever the file. If files are in different namespaces, then you must add a reference in one file to the namespace of the other file using the directive 'using <namespace>', but you never add references to specific files.
Now I get it, thank you.

#6
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
Keyword USING is equivalent for #Include that is used to import external classes and libraries in C++
In C# you can either create a object for the class you defined in the external file or Create a Assembly(.dll) file and then add it to the [Bin] folder of your application and reference it by dragging it to the using statements in your editor [Visual Studio 2008/2010 ]

#7
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts

gokuajmes said:

Keyword USING is equivalent for #Include that is used to import external classes and libraries in C++
Actually it's not. In the usage you are referring to it is just a way to shorten the name you have to use to access an object. "using System.Collections.Generic ;" allows you to reference, for example "List<T>" without having to type "System.Collections.Generic.List<T>".

Including a reference to the DLL (right click on References, select Add in the VS IDE) is how you import external classes.

Refer to Using Directive for more information about using.

#8
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
well i was being a bit general out there, When someone asks he wants to learn C#, you should always explain it from his point of view and not scare him with intricacies of C# [and make him cry mommy :D ] . That is how I teach people new to technology but it differs from people to people




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users