Compiling Multiple Source Code Files Together

This section provides a tutorial example on how to compile multiple C# source code files together.

If you want to write multiple classes in separate source code files, you can compile them together as shown in this tutorial.

1. Write the "Util" class in a source code file called "Util.cs":

// Util.cs
// Copyright (c) 2010 HerongYang.com. All Rights Reserved.

public class Util {
   public static void Swap(ref string x, ref string y) {
      string o = x;
      x = y;
      y = o;
   }
}

2. Write the "SwapTest" class in a source code file called "SwapTest.cs":

// SwapTest.cs
// Copyright (c) 2010 HerongYang.com. All Rights Reserved.

using System;
public class SwapTest {
   public static void Main() {
      string a = "Herong";
      string b = "Yang";
      Console.WriteLine("{0} {1}", a, b);
      Util.Swap(ref a, ref b);
      Console.WriteLine("{0} {1}", a, b);
   }
}

3. If you compile "SwapTest.cs" by itself, you will get an error:

C:\herong>\Windows\Microsoft.NET\Framework\v4.0.30319\csc SwapTest.cs

SwapTest.cs(10,7): error CS0103: The name 'Util' does not exist in the
current context

4. If you compile "SwapTest.cs" with "Util.cs" together, you will get the executable file generated correctly:

C:\herong>\Windows\Microsoft.NET\Framework\v4.0.30319\csc
   SwapTest.cs Util.cs

5. Run the executable file "SwapTest.exe", you will get the program output:

C:\herong>SwapTest.exe
Herong Yang
Yang Herong

Table of Contents

 About This Book

 Introduction of C# (C Sharp)

 Data Type and Variables

 Logical Expressions and Conditional Statements

 Arrays and Loop Statements

 Data Type Features

 Floating-Point Data Types

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

 Class Features

 C# Compiler and Intermediate Language

Compiling C# Source Code Files

 C# Compiler "csc" Options

 Multiple Classes in a Single Source File

Compiling Multiple Source Code Files Together

 Generating and Using .NET Library Files

 MSBuild - Microsoft Build Engine

 Memory Usages of Processes

 Multithreading in C#

 Async Feature from C# 5

 System.IO.FileInfo Class

 System.Diagnostics.FileVersionInfo Class

 WPF - Windows Presentation Foundation

 Partial Classes and Partial Methods

 Archived Tutorials

 References

 Full Version in PDF/ePUB