How does C# compilation get around needing header files?

后端 未结 5 672
时光取名叫无心
时光取名叫无心 2020-12-07 09:58

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 a

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-07 10:26

    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.

提交回复
热议问题