How to include multiple source files in C#

2020-07-05 06:26发布

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?

标签: c#
6条回答
家丑人穷心不美
2楼-- · 2020-07-05 06:29

If you like to split your class to two (or more) source files, you can use partial class definitions. So in your case, you have "class TheClass" in the main.cs and "partial class TheClass" in the test.cs. This is not same as #include, but I guess close enough.

查看更多
做个烂人
3楼-- · 2020-07-05 06:38

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.

查看更多
我想做一个坏孩纸
4楼-- · 2020-07-05 06:38

I am also new to C#. I found this thread while looking for an answer on how to include multiple source files when compiling C# from the command-line. But the current answers are incomplete in my opinion. Here is something that would have made sense to me this morning:

hello.cs

using System;

namespace PartialClasses
{
    class Program
    {
    static void Main(string[] args)
    {
        PartialClass pc = new PartialClass();
        pc.HelloWorld();
        pc.HelloUniverse();
    }
    }
}

PartialClass1.cs

using System; 

namespace PartialClasses
{
    public partial class PartialClass
    {
        public void HelloWorld()
        {
            Console.WriteLine("Hello, world!");
        }
    }
}

PartialClass2.cs

using System;

namespace PartialClasses
{
    public partial class PartialClass
    {
    public void HelloUniverse()
    {
        Console.WriteLine("Hello, universe!");
    }
    }
}

I am using Mono to compile my source file(s). And, so far, nothing I've come across informs what I need to do.

After some digging, I found the following syntax.

First, I want to create a library of my partial classes.

csc -target:library -out:myLibrary.dll Partial*.cs

Then I want to reference that library I just made when compiling my main program. I was fortunate to find an example demonstrating the syntax I needed.

csc -reference:.\myLibrary.dll hello.cs

That is what worked for me.

mono .\hello.exe
Hello, world!
Hello, universe!

With respect to the #include directive, it is not needed in C#. And I refer the reader to Dan Bryant's comment above. Instead, keep your class names and namespaces straight and tell the compiler where to find the library (that you created). That is how the connection between source files is made in C#.

There is also MSBuild for .csproj files and more complicated projects. But, as a beginner, that is beyond my ability to explain.

Credit goes to The Complete C# Tutorial for providing the code I used.

For reference, the compiler error I was getting for csc hello.cs was CS0246. That Microsoft article helped a little. So did -reference (C# Compiler Options).

查看更多
我命由我不由天
5楼-- · 2020-07-05 06:47

The source files don't need to know about each other. The options are:

  • Compile both files together, as per Darin's answer
  • Compile one file into a class library, and add a reference to that library when compiling the other

It depends on whether you want the result to be one assembly or two. Usually the answer would be to compile the two together into the same assembly though.

查看更多
叛逆
6楼-- · 2020-07-05 06:48

I often use the command

csc *.cs

It attempts to compile all the .cs files in the current directory. I find it the simplest and most generally useful way for these kind of command line compilations, like for javas

javac *.java

Additionally you dont have to worry about the file that Main() resides, it automatically detects the entry point.

查看更多
等我变得足够好
7楼-- · 2020-07-05 06:51

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.

查看更多
登录 后发表回答