Search code examples
c#ccompiler-constructionbuild-processclr

How does C# compilation get around needing header files?


I've spent my professional life as a C# developer. As a student I occasionally used C but did not deeply study it's compilation model. Recently I jumped on the bandwagon and have begun studying Objective-C. My first steps have only made me aware of holes in my pre-existing knowledge.

From my research, C/C++/ObjC compilation requires all encountered symbols to be pre-declared. I also understand that building is a two-step process. First you compile each individual source file into individual object files. These object files might have undefined "symbols" (which generally correspond to the identifiers declared in the header files). Second you link the object files together to form your final output. This is a pretty high-level explanation but it satisfies my curiosity enough. But I'd also like to have a similar high-level understanding of the C# build process.

Q: How does the C# build process get around the need for header files? I'd imagine perhaps the compilation step does two-passes?

(Edit: Follow up question here How do C/C++/Objective-C compare with C# when it comes to using libraries?)


Solution

  • I see that there are multiple interpretations of the question. I answered the intra-solution interpretation, but let me fill it out with all the information I know.

    The "header file metadata" is present in the compiled assemblies, so any assembly you add a reference to will allow the compiler to pull in the metadata from those.

    As for things not yet compiled, part of the current solution, it will do a two-pass compilation, first reading namespaces, type names, member names, ie. everything but the code. Then when this checks out, it will read the code and compile that.

    This allows the compiler to know what exists and what doesn't exist (in its universe).

    To see the two-pass compiler in effect, test the following code that has 3 problems, two declaration-related problems, and one code problem:

    using System;
    
    namespace ConsoleApplication11
    {
        class Program
        {
            public static Stringg ReturnsTheWrongType()
            {
                return null;
            }
    
            static void Main(string[] args)
            {
                CallSomeMethodThatDoesntExist();
            }
    
            public static Stringg AlsoReturnsTheWrongType()
            {
                return null;
            }
        }
    }
    

    Note that the compiler will only complain about the two Stringg types that it cannot find. If you fix those, then it complains about the method-name called in the Main method, that it cannot find.