using System;
class Program {
public static void Main() {
Console.WriteLine("Hello World!");
Console.ReadLine();
}
}
I save the file as 1.java
, 2.obj
and 3.txt
.
I then use the Visual Studio Command Prompt to compile the file: csc 1.java csc 2.obj csc 3.txt
Surprisingly, it compiles all the 3 files into an executable and executes it successfully.
Could anyone give me an explanation on this behavior?
Beginning C# Objects: From Concepts to Code - By Jacquie Barker, Grant Palmer
The recommended convention is to end source code file names with the extension .cs, but there is no requirement to do so; a source file could conceivably be named Person.boo, for example
Also from the same book.
Similarly, the name of a C# source file doesn't have to match the name of the class or interface defined within that file. For example, the code defining the Professor class could be placed in a file named Blah.cs, but it's considered good practice for a source file name to match the name of the class or interface declared within the file.
File extension does not matter to the C# compiler: as long as it gets the text of your program correctly, the compiler does not look at the name or the extension of the file. The documentation for the C# compiler does not mention naming requirements*.
Contrast this with Java, where file names and locations are important. Java compiler expects only *.java
source files, with file names matching the names of public classes inside them:
Source code file names must have .java suffixes, class file names must have .class suffixes, and both source and class files must have root names that identify the class.
*.cs
remains the commonly accepted naming convention for C# source files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With