Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# compiler compiles .txt .obj .java files

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?

like image 645
Prakash Bhatia Avatar asked Dec 11 '12 04:12

Prakash Bhatia


2 Answers

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.

like image 35
Habib Avatar answered Sep 22 '22 03:09

Habib


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.


* Even though the compiler does not require it, *.cs remains the commonly accepted naming convention for C# source files.
like image 95
Sergey Kalinichenko Avatar answered Sep 20 '22 03:09

Sergey Kalinichenko