Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include multiple source files in C#

Tags:

c#

I am trying to learn C# coming from a C++ background, but I cannot figure out how to link two source files together. I have a relatively simple program called test.cs and a main.cs. All I want to do is in main.cs say:
#include <"test.cs">.

The closest I've been able to come up with is:

<Compile Include="test.cs"/Compile>

However the compiler does not recognize this. So how can I include multiple source files in my main?

like image 783
Brandon Kimbrough Avatar asked Nov 01 '10 15:11

Brandon Kimbrough


People also ask

Can you include a source file in C?

You can properly include . C or . CPP files into other source files.

Can you have multiple C files?

A large C or C++ program should be divided into multiple files. This makes each file short enough to conveniently edit, print, etc. It also allows some of the code, e.g. utility functions such as linked list handlers or array allocation code, to be shared with other programs.


2 Answers

You pass the list of source files to the compiler:

csc.exe /target:library source1.cs source2.cs

If you use Visual Studio when you create a new .NET project you can add as many source files as you like and they will automatically be compiled.

like image 64
Darin Dimitrov Avatar answered Oct 07 '22 16:10

Darin Dimitrov


If you are building this in Visual Studio, then simply having the 2 files in the same project is all you need to do.

If you are compiling on the command line using csc you ned to reference both files in the call to csc. See Darin's response for this.

There is no need to reference one file from the other, but the easiest way to make types to be visibile to each other would be to add the classes in each file to the same namespace.

like image 31
Steve Avatar answered Oct 07 '22 16:10

Steve