C# Tutorials - Herong's Tutorial Examples - v3.32, by Herong Yang
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
Logical Expressions and Conditional Statements
Visual C# 2010 Express Edition
C# Compiler and Intermediate Language
►Compiling C# Source Code Files
Multiple Classes in a Single Source File
►Compiling Multiple Source Code Files Together
Generating and Using .NET Library Files
MSBuild - Microsoft Build Engine
System.Diagnostics.FileVersionInfo Class
WPF - Windows Presentation Foundation